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