Spaces:
Runtime error
Runtime error
Download src/SCM.Infrastructure/Persistence/Repositories/CampoInspecaoRepository.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 4.19 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/CampoInspecaoRepository.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Infrastructure/Persistence/Repositories/CampoInspecaoRepository.cs
-
curl -L -o CampoInspecaoRepository.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Infrastructure/Persistence/Repositories/CampoInspecaoRepository.cs
4.19 kB
| using System.Data; | |
| using Dapper; | |
| using SCM.Application.Common; | |
| using SCM.Domain.Qualidade; | |
| namespace SCM.Infrastructure.Persistence.Repositories; | |
| public sealed class CampoInspecaoRepository : ICampoInspecaoRepository | |
| { | |
| private readonly IUnitOfWork _uow; | |
| public CampoInspecaoRepository(IUnitOfWork uow) => _uow = uow; | |
| private IDbConnection Conn => _uow.Connection; | |
| private IDbTransaction? Tx => _uow.CurrentTransaction; | |
| private const string Columns = | |
| "id, sub_atividade_padrao_id, nome, tipo, tipo_dado, etapa, obrigatorio_prd, obrigatorio_cq, em_uso, ordem"; | |
| public async Task<IReadOnlyList<CampoInspecao>> GetBySubAtividadeAsync(Guid subAtividadePadraoId, CancellationToken ct = default) | |
| { | |
| const string sql = $""" | |
| SELECT {Columns} FROM campos_inspecao | |
| WHERE sub_atividade_padrao_id = @SubId | |
| ORDER BY ordem, nome | |
| """; | |
| var rows = await Conn.QueryAsync<CampoInspecao>( | |
| new CommandDefinition(sql, new { SubId = subAtividadePadraoId }, Tx, cancellationToken: ct)); | |
| return rows.ToList(); | |
| } | |
| public async Task<CampoInspecao?> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = $"SELECT {Columns} FROM campos_inspecao WHERE id = @Id"; | |
| return await Conn.QueryFirstOrDefaultAsync<CampoInspecao>( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> NomeExistsAsync(string nome, Guid subAtividadePadraoId, Guid? excludeId = null, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| SELECT COUNT(*) FROM campos_inspecao | |
| WHERE lower(nome) = lower(@Nome) AND sub_atividade_padrao_id = @SubId | |
| AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId) | |
| """; | |
| var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition( | |
| sql, new { Nome = nome, SubId = subAtividadePadraoId, ExcludeId = excludeId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task<bool> SubAtividadeExistsAsync(Guid subAtividadePadraoId, CancellationToken ct = default) | |
| { | |
| const string sql = "SELECT COUNT(*) FROM subativs_padrao WHERE id = @Id"; | |
| var count = await Conn.ExecuteScalarAsync<long>( | |
| new CommandDefinition(sql, new { Id = subAtividadePadraoId }, Tx, cancellationToken: ct)); | |
| return count > 0; | |
| } | |
| public async Task AddAsync(CampoInspecao c, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| INSERT INTO campos_inspecao | |
| (id, sub_atividade_padrao_id, nome, tipo, tipo_dado, etapa, obrigatorio_prd, obrigatorio_cq, em_uso, ordem) | |
| VALUES | |
| (@Id, @SubAtividadePadraoId, @Nome, @Tipo, @TipoDado, @Etapa, @ObrigatorioPrd, @ObrigatorioCq, @EmUso, @Ordem) | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(c), Tx, cancellationToken: ct)); | |
| } | |
| public async Task UpdateAsync(CampoInspecao c, CancellationToken ct = default) | |
| { | |
| const string sql = """ | |
| UPDATE campos_inspecao | |
| SET nome = @Nome, tipo = @Tipo, tipo_dado = @TipoDado, etapa = @Etapa, | |
| obrigatorio_prd = @ObrigatorioPrd, obrigatorio_cq = @ObrigatorioCq, em_uso = @EmUso, ordem = @Ordem | |
| WHERE id = @Id | |
| """; | |
| await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(c), Tx, cancellationToken: ct)); | |
| } | |
| public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| const string sql = "DELETE FROM campos_inspecao WHERE id = @Id"; | |
| var affected = await Conn.ExecuteAsync( | |
| new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct)); | |
| return affected > 0; | |
| } | |
| private static object ToParameters(CampoInspecao c) => new | |
| { | |
| c.Id, | |
| c.SubAtividadePadraoId, | |
| c.Nome, | |
| Tipo = (int)c.Tipo, | |
| TipoDado = (int)c.TipoDado, | |
| Etapa = (int)c.Etapa, | |
| c.ObrigatorioPrd, | |
| c.ObrigatorioCq, | |
| c.EmUso, | |
| c.Ordem | |
| }; | |
| } | |