Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/QualificacaoAgenteRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 3.22 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/QualificacaoAgenteRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/QualificacaoAgenteRepository.cs
-
curl -L -o QualificacaoAgenteRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/QualificacaoAgenteRepository.cs
3.22 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Qualidade; | |
| using SCM.Domain.Qualificacoes; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class QualificacaoAgenteRepository : IQualificacaoAgenteRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public QualificacaoAgenteRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| public async Task<IReadOnlyList<Agente>> ListarAgentesAsync(Guid qualificacaoId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT a.id, a.codigo, a.nome, a.tipo, a.cpf, a.email | |
| FROM qualificacao_agentes qa | |
| JOIN agentes a ON a.id = qa.agente_id | |
| WHERE qa.qualificacao_id = @QualificacaoId | |
| ORDER BY a.nome | |
| """; | |
| var rows = await Conn.QueryAsync<Agente>( | |
| new CommandDefinition(sql, new { QualificacaoId = qualificacaoId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<bool> VinculoExistsAsync(Guid qualificacaoId, Guid agenteId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM qualificacao_agentes WHERE qualificacao_id = @QualificacaoId AND agente_id = @AgenteId"; | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| sql, new { QualificacaoId = qualificacaoId, AgenteId = agenteId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<bool> QualificacaoExistsAsync(Guid qualificacaoId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM qualificacoes WHERE id = @Id"; | |
| var count = await Conn.ExecuteScalarAsync<long>( | |
| new CommandDefinition(sql, new { Id = qualificacaoId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<bool> AgenteExistsAsync(Guid agenteId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM agentes WHERE id = @Id"; | |
| var count = await Conn.ExecuteScalarAsync<long>( | |
| new CommandDefinition(sql, new { Id = agenteId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task AddAsync(QualificacaoAgente vinculo, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO qualificacao_agentes (id, qualificacao_id, agente_id) | |
| VALUES (@Id, @QualificacaoId, @AgenteId) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition( | |
| sql, new { vinculo.Id, vinculo.QualificacaoId, vinculo.AgenteId }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> RemoveAsync(Guid qualificacaoId, Guid agenteId, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM qualificacao_agentes WHERE qualificacao_id = @QualificacaoId AND agente_id = @AgenteId"; | |
| var affected = await Conn.ExecuteAsync(new CommandDefinition( | |
| sql, new { QualificacaoId = qualificacaoId, AgenteId = agenteId }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| } | |