Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/AgenteRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 4.6 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/AgenteRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/AgenteRepository.cs
-
curl -L -o AgenteRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/AgenteRepository.cs
4.6 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Qualidade; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class AgenteRepository : IAgenteRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public AgenteRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| public async Task<IReadOnlyList<AgenteComContagem>> GetAllAsync(CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT a.id, a.codigo, a.nome, a.tipo, a.cpf, a.email, COUNT(p.id) AS qtd | |
| FROM agentes a | |
| LEFT JOIN programacoes p ON p.agente_id = a.id | |
| GROUP BY a.id, a.codigo, a.nome, a.tipo, a.cpf, a.email | |
| ORDER BY a.nome | |
| """; | |
| var rows = await Conn.QueryAsync<AgenteRow>( | |
| new CommandDefinition(sql, transaction: Tx, cancellationToken: ct)); | |
| return rows.Select(r => new AgenteComContagem(r.ToDomain(), r.Qtd)).ToList(); | |
| } | |
| public async Task<Agente?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT id, codigo, nome, tipo, cpf, email FROM agentes WHERE id = @Id"; | |
| return await Conn.QueryFirstOrDefaultAsync<Agente>( | |
| 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 agentes | |
| WHERE codigo = @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<bool> CpfExistsAsync(string cpf, Guid? excludeId = null, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT COUNT(*) FROM agentes | |
| WHERE cpf = @Cpf AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId) | |
| """; | |
| var count = await Conn.ExecuteScalarAsync<long>( | |
| new CommandDefinition(sql, new { Cpf = cpf, ExcludeId = excludeId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<int> ContarProgramacoesAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM programacoes WHERE agente_id = @Id"; | |
| return await Conn.ExecuteScalarAsync<int>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task AddAsync(Agente agente, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO agentes (id, codigo, nome, tipo, cpf, email) | |
| VALUES (@Id, @Codigo, @Nome, @Tipo, @Cpf, @Email) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(agente), Tx, cancellationToken: ct)); | |
| } | |
| public async Task UpdateAsync(Agente agente, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| UPDATE agentes | |
| SET codigo = @Codigo, nome = @Nome, tipo = @Tipo, cpf = @Cpf, email = @Email | |
| WHERE id = @Id | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(agente), Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM agentes WHERE id = @Id"; | |
| var affected = await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| private static object ToParameters(Agente a) => new | |
| { | |
| a.Id, | |
| a.Codigo, | |
| a.Nome, | |
| Tipo = (int)a.Tipo, | |
| a.Cpf, | |
| a.Email | |
| }; | |
| private sealed class AgenteRow | |
| { | |
| public Guid Id { get; set; } | |
| public string Codigo { get; set; } = string.Empty; | |
| public string Nome { get; set; } = string.Empty; | |
| public int Tipo { get; set; } | |
| public string? Cpf { get; set; } | |
| public string? Email { get; set; } | |
| public int Qtd { get; set; } | |
| public Agente ToDomain() => new() | |
| { | |
| Id = Id, | |
| Codigo = Codigo, | |
| Nome = Nome, | |
| Tipo = (AgenteTipo)Tipo, | |
| Cpf = Cpf, | |
| Email = Email | |
| }; | |
| } | |
| } | |