Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/ConversaoUnidadeMedidaRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 4.95 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/ConversaoUnidadeMedidaRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/ConversaoUnidadeMedidaRepository.cs
-
curl -L -o ConversaoUnidadeMedidaRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/ConversaoUnidadeMedidaRepository.cs
4.95 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Cadastros; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class ConversaoUnidadeMedidaRepository : IConversaoUnidadeMedidaRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public ConversaoUnidadeMedidaRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| public async Task<IReadOnlyList<ConversaoComUnidades>> GetAllAsync(CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT c.id, c.unidade_de_id, c.unidade_para_id, c.fator, | |
| ude.codigo AS codigo_de, upa.codigo AS codigo_para | |
| FROM conversoes_unidade_medida c | |
| JOIN unidades_medida ude ON ude.id = c.unidade_de_id | |
| JOIN unidades_medida upa ON upa.id = c.unidade_para_id | |
| ORDER BY ude.codigo, upa.codigo | |
| """; | |
| var rows = await Conn.QueryAsync<ConversaoRow>( | |
| new CommandDefinition(sql, transaction: Tx, cancellationToken: ct)); | |
| return rows.Select(r => new ConversaoComUnidades(r.ToDomain(), r.CodigoDe, r.CodigoPara)).ToList(); | |
| } | |
| public async Task<ConversaoUnidadeMedida?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT id, unidade_de_id, unidade_para_id, fator FROM conversoes_unidade_medida WHERE id = @Id"; | |
| return await Conn.QueryFirstOrDefaultAsync<ConversaoUnidadeMedida>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<ConversaoUnidadeMedida?> GetByParAsync(Guid unidadeDeId, Guid unidadeParaId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT id, unidade_de_id, unidade_para_id, fator | |
| FROM conversoes_unidade_medida | |
| WHERE unidade_de_id = @UnidadeDeId AND unidade_para_id = @UnidadeParaId | |
| """; | |
| return await Conn.QueryFirstOrDefaultAsync<ConversaoUnidadeMedida>(new CommandDefinition( | |
| sql, new { UnidadeDeId = unidadeDeId, UnidadeParaId = unidadeParaId }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> ParExistsAsync(Guid unidadeDeId, Guid unidadeParaId, Guid? excludeId = null, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT COUNT(*) FROM conversoes_unidade_medida | |
| WHERE unidade_de_id = @UnidadeDeId AND unidade_para_id = @UnidadeParaId | |
| AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId) | |
| """; | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| sql, new { UnidadeDeId = unidadeDeId, UnidadeParaId = unidadeParaId, ExcludeId = excludeId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<bool> UnidadeExistsAsync(Guid unidadeMedidaId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM unidades_medida WHERE id = @Id"; | |
| var count = await Conn.ExecuteScalarAsync<long>( | |
| new CommandDefinition(sql, new { Id = unidadeMedidaId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task AddAsync(ConversaoUnidadeMedida c, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO conversoes_unidade_medida (id, unidade_de_id, unidade_para_id, fator) | |
| VALUES (@Id, @UnidadeDeId, @UnidadeParaId, @Fator) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition( | |
| sql, new { c.Id, c.UnidadeDeId, c.UnidadeParaId, c.Fator }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task UpdateAsync(ConversaoUnidadeMedida c, CancellationToken ct = default) | |
| { | |
| const string sql = "UPDATE conversoes_unidade_medida SET fator = @Fator WHERE id = @Id"; | |
| await Conn.ExecuteAsync(new CommandDefinition( | |
| sql, new { c.Id, c.Fator }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM conversoes_unidade_medida WHERE id = @Id"; | |
| var affected = await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| private sealed class ConversaoRow | |
| { | |
| public Guid Id { get; set; } | |
| public Guid UnidadeDeId { get; set; } | |
| public Guid UnidadeParaId { get; set; } | |
| public decimal Fator { get; set; } | |
| public string CodigoDe { get; set; } = string.Empty; | |
| public string CodigoPara { get; set; } = string.Empty; | |
| public ConversaoUnidadeMedida ToDomain() => new() | |
| { | |
| Id = Id, | |
| UnidadeDeId = UnidadeDeId, | |
| UnidadeParaId = UnidadeParaId, | |
| Fator = Fator | |
| }; | |
| } | |
| } | |