Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/QualificacaoRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 5.23 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/QualificacaoRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/QualificacaoRepository.cs
-
curl -L -o QualificacaoRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/QualificacaoRepository.cs
5.23 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Qualificacoes; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class QualificacaoRepository : IQualificacaoRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public QualificacaoRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| public async Task<IReadOnlyList<QualificacaoComContagem>> GetByObraAsync(Guid obraId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT q.id, q.codigo, q.descricao, q.tipo, q.obra_id, COUNT(qa.id) AS qtd | |
| FROM qualificacoes q | |
| LEFT JOIN qualificacao_agentes qa ON qa.qualificacao_id = q.id | |
| WHERE q.obra_id = @ObraId | |
| GROUP BY q.id, q.codigo, q.descricao, q.tipo, q.obra_id | |
| ORDER BY q.descricao | |
| """; | |
| var rows = await Conn.QueryAsync<QualificacaoRow>( | |
| new CommandDefinition(sql, new { ObraId = obraId }, Tx, cancellationToken: ct)); | |
| return rows.Select(r => new QualificacaoComContagem(r.ToDomain(), r.Qtd)).ToList(); | |
| } | |
| public async Task<Qualificacao?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT id, codigo, descricao, tipo, obra_id FROM qualificacoes WHERE id = @Id"; | |
| return await Conn.QueryFirstOrDefaultAsync<Qualificacao>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> ObraExistsAsync(Guid obraId, CancellationToken ct = default) | |
| { | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| "SELECT COUNT(*) FROM obras WHERE id = @Id", new { Id = obraId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<bool> CodigoExistsAsync(string codigo, Guid obraId, Guid? excludeId = null, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT COUNT(*) FROM qualificacoes | |
| WHERE codigo = @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> DescricaoExistsAsync(string descricao, Guid obraId, Guid? excludeId = null, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT COUNT(*) FROM qualificacoes | |
| WHERE descricao = @Descricao AND obra_id = @ObraId AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId) | |
| """; | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| sql, new { Descricao = descricao, ObraId = obraId, ExcludeId = excludeId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<int> ContarAgentesAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM qualificacao_agentes WHERE qualificacao_id = @Id"; | |
| return await Conn.ExecuteScalarAsync<int>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task AddAsync(Qualificacao qualificacao, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO qualificacoes (id, codigo, descricao, tipo, obra_id) | |
| VALUES (@Id, @Codigo, @Descricao, @Tipo, @ObraId) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(qualificacao), Tx, cancellationToken: ct)); | |
| } | |
| public async Task UpdateAsync(Qualificacao qualificacao, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| UPDATE qualificacoes | |
| SET codigo = @Codigo, descricao = @Descricao, tipo = @Tipo | |
| WHERE id = @Id | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(qualificacao), Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM qualificacoes WHERE id = @Id"; | |
| var affected = await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| private static object ToParameters(Qualificacao q) => new | |
| { | |
| q.Id, | |
| q.Codigo, | |
| q.Descricao, | |
| Tipo = (int)q.Tipo, | |
| q.ObraId | |
| }; | |
| private sealed class QualificacaoRow | |
| { | |
| public Guid Id { get; set; } | |
| public string Codigo { get; set; } = string.Empty; | |
| public string Descricao { get; set; } = string.Empty; | |
| public int Tipo { get; set; } | |
| public Guid ObraId { get; set; } | |
| public int Qtd { get; set; } | |
| public Qualificacao ToDomain() => new() | |
| { | |
| Id = Id, | |
| Codigo = Codigo, | |
| Descricao = Descricao, | |
| Tipo = (QualificacaoTipo)Tipo, | |
| ObraId = ObraId | |
| }; | |
| } | |
| } | |