Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/ItemControleRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 5.61 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/ItemControleRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/ItemControleRepository.cs
-
curl -L -o ItemControleRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/ItemControleRepository.cs
5.61 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Controle; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class ItemControleRepository : IItemControleRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public ItemControleRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| public async Task<IReadOnlyList<ItemControleComContagem>> GetByObraAsync(Guid obraId, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT i.id, i.codigo, i.descricao, i.obra_id, i.quantidade_medicao, i.estado_avanco, i.gitec_pendencia, i.revalidacao_pendente, COUNT(p.id) AS qtd | |
| FROM itens_controle i | |
| LEFT JOIN itens_programados p ON p.item_controle_id = i.id | |
| WHERE i.obra_id = @ObraId | |
| GROUP BY i.id, i.codigo, i.descricao, i.obra_id, i.quantidade_medicao, i.estado_avanco, i.gitec_pendencia, i.revalidacao_pendente | |
| ORDER BY i.codigo | |
| """; | |
| var rows = await Conn.QueryAsync<ItemControleRow>( | |
| new CommandDefinition(sql, new { ObraId = obraId }, Tx, cancellationToken: ct)); | |
| return rows.Select(r => new ItemControleComContagem(r.ToDomain(), r.Qtd)).ToList(); | |
| } | |
| public async Task<ItemControle?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT id, codigo, descricao, obra_id, quantidade_medicao, estado_avanco, gitec_pendencia, revalidacao_pendente FROM itens_controle WHERE id = @Id"; | |
| return await Conn.QueryFirstOrDefaultAsync<ItemControle>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> CodigoExistsAsync(string codigo, Guid obraId, Guid? excludeId = null, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT COUNT(*) FROM itens_controle | |
| WHERE codigo = @Codigo AND obra_id = @ObraId AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId) | |
| """; | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| sql, new { Codigo = codigo, ObraId = obraId, ExcludeId = excludeId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<bool> ObraExistsAsync(Guid obraId, CancellationToken ct = default) | |
| { | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| "SELECT COUNT(*) FROM obras WHERE id = @Id", new { Id = obraId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task SetRevalidacaoPendenteAsync(Guid itemControleId, bool pendente, CancellationToken ct = default) | |
| => await Conn.ExecuteAsync(new CommandDefinition( | |
| "UPDATE itens_controle SET revalidacao_pendente = @Pendente WHERE id = @Id", | |
| new { Id = itemControleId, Pendente = pendente }, Tx, cancellationToken: ct)); | |
| public async Task<int> ContarItensProgramadosAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM itens_programados WHERE item_controle_id = @Id"; | |
| return await Conn.ExecuteScalarAsync<int>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task AddAsync(ItemControle item, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO itens_controle (id, codigo, descricao, obra_id, quantidade_medicao, estado_avanco) | |
| VALUES (@Id, @Codigo, @Descricao, @ObraId, @QuantidadeMedicao, @EstadoAvanco) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(item), Tx, cancellationToken: ct)); | |
| } | |
| public async Task UpdateAsync(ItemControle item, CancellationToken ct = default) | |
| { | |
| const string sql = "UPDATE itens_controle SET codigo = @Codigo, descricao = @Descricao, quantidade_medicao = @QuantidadeMedicao WHERE id = @Id"; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(item), Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM itens_controle WHERE id = @Id"; | |
| var affected = await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| private static object ToParameters(ItemControle i) => new | |
| { | |
| i.Id, | |
| i.Codigo, | |
| i.Descricao, | |
| i.ObraId, | |
| i.QuantidadeMedicao, | |
| EstadoAvanco = (int)i.EstadoAvanco | |
| }; | |
| private sealed class ItemControleRow | |
| { | |
| public Guid Id { get; set; } | |
| public string Codigo { get; set; } = string.Empty; | |
| public string Descricao { get; set; } = string.Empty; | |
| public Guid ObraId { get; set; } | |
| public decimal QuantidadeMedicao { get; set; } | |
| public int EstadoAvanco { get; set; } | |
| public int GitecPendencia { get; set; } | |
| public bool RevalidacaoPendente { get; set; } | |
| public int Qtd { get; set; } | |
| public ItemControle ToDomain() => new() | |
| { | |
| Id = Id, | |
| Codigo = Codigo, | |
| Descricao = Descricao, | |
| ObraId = ObraId, | |
| QuantidadeMedicao = QuantidadeMedicao, | |
| EstadoAvanco = (EstadoAvancoIc)EstadoAvanco, | |
| GitecPendencia = (GitecPendencia)GitecPendencia, | |
| RevalidacaoPendente = RevalidacaoPendente | |
| }; | |
| } | |
| } | |