File size: 2,011 Bytes
de8b914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Data;
using Dapper;
using SCM.Application.Common;
using SCM.Domain.Qualidade;

namespace SCM.Infrastructure.Persistence.Repositories;

public sealed class CroquiRepository : ICroquiRepository
{
    private readonly IUnitOfWork _uow;

    public CroquiRepository(IUnitOfWork uow) => _uow = uow;

    private IDbConnection Conn => _uow.Connection;
    private IDbTransaction? Tx => _uow.CurrentTransaction;

    public async Task<bool> RelatorioExistsAsync(Guid relatorioTecnicoId, CancellationToken ct = default)
    {
        var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition(
            "SELECT COUNT(*) FROM relatorios_tecnicos WHERE id = @Id", new { Id = relatorioTecnicoId }, Tx, cancellationToken: ct));
        return count > 0;
    }

    public async Task<IReadOnlyList<Croqui>> GetByRelatorioAsync(Guid relatorioTecnicoId, CancellationToken ct = default)
    {
        const string sql = """
            SELECT id, relatorio_tecnico_id, titulo, ordem, nome_imagem, tamanho_imagem
            FROM croquis WHERE relatorio_tecnico_id = @Id ORDER BY ordem
            """;
        var rows = await Conn.QueryAsync<Croqui>(
            new CommandDefinition(sql, new { Id = relatorioTecnicoId }, Tx, cancellationToken: ct));
        return rows.ToList();
    }

    public async Task AddAsync(Croqui c, CancellationToken ct = default)
    {
        const string sql = """
            INSERT INTO croquis (id, relatorio_tecnico_id, titulo, ordem, nome_imagem, tamanho_imagem)
            VALUES (@Id, @RelatorioTecnicoId, @Titulo, @Ordem, @NomeImagem, @TamanhoImagem)
            """;
        await Conn.ExecuteAsync(new CommandDefinition(sql, c, Tx, cancellationToken: ct));
    }

    public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default)
    {
        var affected = await Conn.ExecuteAsync(new CommandDefinition(
            "DELETE FROM croquis WHERE id = @Id", new { Id = id }, Tx, cancellationToken: ct));
        return affected > 0;
    }
}