Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/UnidadeMedidaRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 3.75 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/UnidadeMedidaRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/UnidadeMedidaRepository.cs
-
curl -L -o UnidadeMedidaRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/UnidadeMedidaRepository.cs
3.75 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Cadastros; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class UnidadeMedidaRepository : IUnidadeMedidaRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public UnidadeMedidaRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| public async Task<IReadOnlyList<UnidadeMedidaComContagem>> GetAllAsync(CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT u.id, u.codigo, u.descricao, COUNT(x.id) AS qtd | |
| FROM unidades_medida u | |
| LEFT JOIN usos_unidade_medida x ON x.unidade_medida_id = u.id | |
| GROUP BY u.id, u.codigo, u.descricao | |
| ORDER BY u.codigo | |
| """; | |
| var rows = await Conn.QueryAsync<UnidadeMedidaRow>( | |
| new CommandDefinition(sql, transaction: Tx, cancellationToken: ct)); | |
| return rows.Select(r => new UnidadeMedidaComContagem(r.ToDomain(), r.Qtd)).ToList(); | |
| } | |
| public async Task<UnidadeMedida?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT id, codigo, descricao FROM unidades_medida WHERE id = @Id"; | |
| return await Conn.QueryFirstOrDefaultAsync<UnidadeMedida>( | |
| 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 unidades_medida | |
| 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> ContarUsosAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM usos_unidade_medida WHERE unidade_medida_id = @Id"; | |
| return await Conn.ExecuteScalarAsync<int>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task AddAsync(UnidadeMedida unidade, CancellationToken ct = default) | |
| { | |
| const string sql = "INSERT INTO unidades_medida (id, codigo, descricao) VALUES (@Id, @Codigo, @Descricao)"; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(unidade), Tx, cancellationToken: ct)); | |
| } | |
| public async Task UpdateAsync(UnidadeMedida unidade, CancellationToken ct = default) | |
| { | |
| const string sql = "UPDATE unidades_medida SET codigo = @Codigo, descricao = @Descricao WHERE id = @Id"; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(unidade), Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM unidades_medida WHERE id = @Id"; | |
| var affected = await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| private static object ToParameters(UnidadeMedida u) => new { u.Id, u.Codigo, u.Descricao }; | |
| private sealed class UnidadeMedidaRow | |
| { | |
| public Guid Id { get; set; } | |
| public string Codigo { get; set; } = string.Empty; | |
| public string Descricao { get; set; } = string.Empty; | |
| public int Qtd { get; set; } | |
| public UnidadeMedida ToDomain() => new() { Id = Id, Codigo = Codigo, Descricao = Descricao }; | |
| } | |
| } | |