Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/EapRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 4.18 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/EapRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/EapRepository.cs
-
curl -L -o EapRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/EapRepository.cs
4.18 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Medicao; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class EapRepository : IEapRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public EapRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| private const string Columns = "id, obra_id, codigo, descricao, criterio_eap_id, criterio_tipo_calculo, criterio_unidade_apropriacao, acumulado"; | |
| public async Task<IReadOnlyList<Eap>> GetByObraAsync(Guid obraId, CancellationToken ct = default) | |
| { | |
| const string sql = $""" | |
| SELECT {Columns} | |
| FROM eaps | |
| WHERE obra_id = @ObraId | |
| ORDER BY codigo | |
| """; | |
| var rows = await Conn.QueryAsync<Eap>( | |
| new CommandDefinition(sql, new { ObraId = obraId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<Eap?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = $"SELECT {Columns} FROM eaps WHERE id = @Id"; | |
| return await Conn.QueryFirstOrDefaultAsync<Eap>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> CodigoExistsAsync(string codigo, Guid obraId, Guid? excludeId = null, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT COUNT(*) FROM eaps | |
| WHERE lower(codigo) = lower(@Codigo) AND obra_id = @ObraId AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId) | |
| """; | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| sql, new { Codigo = codigo, ObraId = obraId, ExcludeId = excludeId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<bool> ObraExistsAsync(Guid obraId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM obras WHERE id = @Id"; | |
| var count = await Conn.ExecuteScalarAsync<long>( | |
| new CommandDefinition(sql, new { Id = obraId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<bool> CriterioExistsAsync(Guid criterioEapId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM criterios_eap WHERE id = @Id"; | |
| var count = await Conn.ExecuteScalarAsync<long>( | |
| new CommandDefinition(sql, new { Id = criterioEapId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task AddAsync(Eap e, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO eaps (id, obra_id, codigo, descricao, criterio_eap_id, criterio_tipo_calculo, criterio_unidade_apropriacao) | |
| VALUES (@Id, @ObraId, @Codigo, @Descricao, @CriterioEapId, @CriterioTipoCalculo, @CriterioUnidadeApropriacao) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(e), Tx, cancellationToken: ct)); | |
| } | |
| public async Task UpdateAsync(Eap e, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| UPDATE eaps | |
| SET codigo = @Codigo, descricao = @Descricao, criterio_eap_id = @CriterioEapId, | |
| criterio_tipo_calculo = @CriterioTipoCalculo, | |
| criterio_unidade_apropriacao = @CriterioUnidadeApropriacao | |
| WHERE id = @Id | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(e), Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM eaps WHERE id = @Id"; | |
| var affected = await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| private static object ToParameters(Eap e) => new | |
| { | |
| e.Id, | |
| e.ObraId, | |
| e.Codigo, | |
| e.Descricao, | |
| e.CriterioEapId, | |
| e.CriterioTipoCalculo, | |
| e.CriterioUnidadeApropriacao | |
| }; | |
| } | |