Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/GrupoConsultaRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 3.56 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/GrupoConsultaRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/GrupoConsultaRepository.cs
-
curl -L -o GrupoConsultaRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/GrupoConsultaRepository.cs
3.56 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Consultas; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class GrupoConsultaRepository : IGrupoConsultaRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public GrupoConsultaRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| public async Task<IReadOnlyList<GrupoConsultaComContagem>> GetAllAsync(CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT g.id, g.nome, COUNT(c.id) AS qtd_consultas | |
| FROM grupos_consulta g | |
| LEFT JOIN consultas_personalizadas c ON c.grupo_consulta_id = g.id | |
| GROUP BY g.id, g.nome | |
| ORDER BY g.nome | |
| """; | |
| var rows = await Conn.QueryAsync<GrupoConsultaRow>( | |
| new CommandDefinition(sql, transaction: Tx, cancellationToken: ct)); | |
| return rows | |
| .Select(r => new GrupoConsultaComContagem( | |
| new GrupoConsulta { Id = r.Id, Nome = r.Nome }, r.QtdConsultas)) | |
| .ToList(); | |
| } | |
| public async Task<GrupoConsulta?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT id, nome FROM grupos_consulta WHERE id = @Id"; | |
| return await Conn.QueryFirstOrDefaultAsync<GrupoConsulta>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> NomeExistsAsync(string nome, Guid? excludeId = null, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT COUNT(*) | |
| FROM grupos_consulta | |
| WHERE lower(nome) = lower(@Nome) | |
| AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId) | |
| """; | |
| var count = await Conn.ExecuteScalarAsync<long>( | |
| new CommandDefinition(sql, new { Nome = nome, ExcludeId = excludeId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<int> ContarConsultasAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM consultas_personalizadas WHERE grupo_consulta_id = @Id"; | |
| return await Conn.ExecuteScalarAsync<int>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task AddAsync(GrupoConsulta grupo, CancellationToken ct = default) | |
| { | |
| const string sql = "INSERT INTO grupos_consulta (id, nome) VALUES (@Id, @Nome)"; | |
| await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { grupo.Id, grupo.Nome }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task UpdateAsync(GrupoConsulta grupo, CancellationToken ct = default) | |
| { | |
| const string sql = "UPDATE grupos_consulta SET nome = @Nome WHERE id = @Id"; | |
| await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { grupo.Id, grupo.Nome }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM grupos_consulta WHERE id = @Id"; | |
| var affected = await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| private sealed class GrupoConsultaRow | |
| { | |
| public Guid Id { get; set; } | |
| public string Nome { get; set; } = string.Empty; | |
| public int QtdConsultas { get; set; } | |
| } | |
| } | |