Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/ObraRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 3.97 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/ObraRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/ObraRepository.cs
-
curl -L -o ObraRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/ObraRepository.cs
3.97 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Obras; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class ObraRepository : IObraRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public ObraRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| // Soma de vínculos da obra nas tabelas que referenciam obra_id (critérios EAP, qualificações, itens de controle). | |
| private const string VinculosExpr = """ | |
| (SELECT COUNT(*) FROM criterios_eap c WHERE c.obra_id = o.id) | |
| + (SELECT COUNT(*) FROM qualificacoes q WHERE q.obra_id = o.id) | |
| + (SELECT COUNT(*) FROM itens_controle i WHERE i.obra_id = o.id) | |
| """; | |
| public async Task<IReadOnlyList<ObraComContagem>> GetAllAsync(CancellationToken ct = default) | |
| { | |
| var sql = $""" | |
| SELECT o.id, o.codigo, o.nome, {VinculosExpr} AS qtd | |
| FROM obras o | |
| ORDER BY o.codigo | |
| """; | |
| var rows = await Conn.QueryAsync<ObraRow>( | |
| new CommandDefinition(sql, transaction: Tx, cancellationToken: ct)); | |
| return rows.Select(r => new ObraComContagem(r.ToDomain(), r.Qtd)).ToList(); | |
| } | |
| public async Task<Obra?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT id, codigo, nome FROM obras WHERE id = @Id"; | |
| return await Conn.QueryFirstOrDefaultAsync<Obra>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> CodigoExistsAsync(string codigo, Guid? excludeId = null, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT COUNT(*) FROM obras | |
| WHERE lower(codigo) = lower(@Codigo) AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId) | |
| """; | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| sql, new { Codigo = codigo, ExcludeId = excludeId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<int> ContarVinculosAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT (SELECT COUNT(*) FROM criterios_eap c WHERE c.obra_id = @Id) | |
| + (SELECT COUNT(*) FROM qualificacoes q WHERE q.obra_id = @Id) | |
| + (SELECT COUNT(*) FROM itens_controle i WHERE i.obra_id = @Id) | |
| """; | |
| return await Conn.ExecuteScalarAsync<int>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task AddAsync(Obra obra, CancellationToken ct = default) | |
| { | |
| const string sql = "INSERT INTO obras (id, codigo, nome) VALUES (@Id, @Codigo, @Nome)"; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(obra), Tx, cancellationToken: ct)); | |
| } | |
| public async Task UpdateAsync(Obra obra, CancellationToken ct = default) | |
| { | |
| const string sql = "UPDATE obras SET codigo = @Codigo, nome = @Nome WHERE id = @Id"; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(obra), Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM obras WHERE id = @Id"; | |
| var affected = await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| private static object ToParameters(Obra o) => new { o.Id, o.Codigo, o.Nome }; | |
| private sealed class ObraRow | |
| { | |
| public Guid Id { get; set; } | |
| public string Codigo { get; set; } = string.Empty; | |
| public string Nome { get; set; } = string.Empty; | |
| public int Qtd { get; set; } | |
| public Obra ToDomain() => new() { Id = Id, Codigo = Codigo, Nome = Nome }; | |
| } | |
| } | |