Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/DeliveryOrderRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 7 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/DeliveryOrderRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/DeliveryOrderRepository.cs
-
curl -L -o DeliveryOrderRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/DeliveryOrderRepository.cs
7 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Materiais; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class DeliveryOrderRepository : IDeliveryOrderRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public DeliveryOrderRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| private const string Columns = """ | |
| id, obra_id, nota_fiscal, item_recebido, ordem_compra, quantidade, data_recebimento, | |
| unidade_medida_id, book_material_id, tipo_material_macro_id, | |
| peso, comprimento, largura, espessura, diametro, | |
| generico1, generico2, generico3, generico4, generico5 | |
| """; | |
| public async Task<IReadOnlyList<DeliveryOrder>> GetByObraAsync( | |
| Guid obraId, string? notaFiscal = null, Guid? tipoMaterialMacroId = null, CancellationToken ct = default) | |
| { | |
| const string sql = $""" | |
| SELECT {Columns} | |
| FROM delivery_orders | |
| WHERE obra_id = @ObraId | |
| AND (@NotaFiscal::text IS NULL OR nota_fiscal ILIKE '%' || @NotaFiscal || '%') | |
| AND (@TipoMacroId::uuid IS NULL OR tipo_material_macro_id = @TipoMacroId) | |
| ORDER BY nota_fiscal, item_recebido | |
| """; | |
| var rows = await Conn.QueryAsync<DeliveryOrder>(new CommandDefinition( | |
| sql, new { ObraId = obraId, NotaFiscal = notaFiscal, TipoMacroId = tipoMaterialMacroId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<DeliveryOrder?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = $"SELECT {Columns} FROM delivery_orders WHERE id = @Id"; | |
| return await Conn.QueryFirstOrDefaultAsync<DeliveryOrder>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> ParExistsAsync(Guid obraId, string notaFiscal, string itemRecebido, Guid? excludeId = null, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT COUNT(*) FROM delivery_orders | |
| WHERE obra_id = @ObraId AND lower(nota_fiscal) = lower(@NotaFiscal) | |
| AND lower(item_recebido) = lower(@ItemRecebido) | |
| AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId) | |
| """; | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| sql, new { ObraId = obraId, NotaFiscal = notaFiscal, ItemRecebido = itemRecebido, ExcludeId = excludeId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<bool> ObraExistsAsync(Guid obraId, CancellationToken ct = default) | |
| => await ExisteAsync("obras", obraId, ct); | |
| public async Task<bool> UnidadeExistsAsync(Guid unidadeMedidaId, CancellationToken ct = default) | |
| => await ExisteAsync("unidades_medida", unidadeMedidaId, ct); | |
| public async Task<bool> BookMaterialExistsAsync(Guid bookMaterialId, CancellationToken ct = default) | |
| => await ExisteAsync("tipos_material", bookMaterialId, ct); | |
| public async Task<Guid?> GetAgrupadorDoTipoMacroAsync(Guid tipoMaterialMacroId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT agrupador_id FROM tipos_material_macro WHERE id = @Id"; | |
| var rows = (await Conn.QueryAsync<Guid?>( | |
| new CommandDefinition(sql, new { Id = tipoMaterialMacroId }, Tx, cancellationToken: ct))).ToList(); | |
| if (rows.Count == 0) return null; // tipo macro não existe | |
| return rows[0] ?? Guid.Empty; // existe; Empty = sem agrupador | |
| } | |
| public async Task<int> ContarVinculosRirAsync(Guid deliveryOrderId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM rir_itens WHERE delivery_order_id = @Id"; | |
| return await Conn.ExecuteScalarAsync<int>( | |
| new CommandDefinition(sql, new { Id = deliveryOrderId }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task AddAsync(DeliveryOrder d, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO delivery_orders | |
| (id, obra_id, nota_fiscal, item_recebido, ordem_compra, quantidade, data_recebimento, | |
| unidade_medida_id, book_material_id, tipo_material_macro_id, | |
| peso, comprimento, largura, espessura, diametro, | |
| generico1, generico2, generico3, generico4, generico5) | |
| VALUES | |
| (@Id, @ObraId, @NotaFiscal, @ItemRecebido, @OrdemCompra, @Quantidade, @DataRecebimento, | |
| @UnidadeMedidaId, @BookMaterialId, @TipoMaterialMacroId, | |
| @Peso, @Comprimento, @Largura, @Espessura, @Diametro, | |
| @Generico1, @Generico2, @Generico3, @Generico4, @Generico5) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(d), Tx, cancellationToken: ct)); | |
| } | |
| public async Task UpdateAsync(DeliveryOrder d, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| UPDATE delivery_orders | |
| SET nota_fiscal = @NotaFiscal, item_recebido = @ItemRecebido, ordem_compra = @OrdemCompra, | |
| quantidade = @Quantidade, data_recebimento = @DataRecebimento, | |
| unidade_medida_id = @UnidadeMedidaId, book_material_id = @BookMaterialId, | |
| tipo_material_macro_id = @TipoMaterialMacroId, | |
| peso = @Peso, comprimento = @Comprimento, largura = @Largura, | |
| espessura = @Espessura, diametro = @Diametro, | |
| generico1 = @Generico1, generico2 = @Generico2, generico3 = @Generico3, | |
| generico4 = @Generico4, generico5 = @Generico5 | |
| WHERE id = @Id | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(d), Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM delivery_orders WHERE id = @Id"; | |
| var affected = await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| private async Task<bool> ExisteAsync(string tabela, Guid id, CancellationToken ct) | |
| { | |
| var sql = $"SELECT COUNT(*) FROM {tabela} WHERE id = @Id"; | |
| var count = await Conn.ExecuteScalarAsync<long>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| private static object ToParameters(DeliveryOrder d) => new | |
| { | |
| d.Id, d.ObraId, d.NotaFiscal, d.ItemRecebido, d.OrdemCompra, d.Quantidade, d.DataRecebimento, | |
| d.UnidadeMedidaId, d.BookMaterialId, d.TipoMaterialMacroId, | |
| d.Peso, d.Comprimento, d.Largura, d.Espessura, d.Diametro, | |
| d.Generico1, d.Generico2, d.Generico3, d.Generico4, d.Generico5 | |
| }; | |
| } | |