scm-api / src /SCM.Infrastructure /Persistence /Repositories /EapPlanejamentoRepository.cs
rafaeldavila's picture
Deploy SCM API (.NET 8) - Docker Space
de8b914 verified
Raw History Blame Contribute Delete
2.82 kB
using System.Data;
using Dapper;
using SCM.Application.Common;
using SCM.Application.Medicao;
using SCM.Domain.Medicao;
namespace SCM.Infrastructure.Persistence.Repositories;
public sealed class EapPlanejamentoRepository : IEapPlanejamentoRepository
{
private readonly IUnitOfWork _uow;
public EapPlanejamentoRepository(IUnitOfWork uow) => _uow = uow;
private IDbConnection Conn => _uow.Connection;
private IDbTransaction? Tx => _uow.CurrentTransaction;
public async Task<IReadOnlyList<EapCriterioPlanejamento>> GetByEapAsync(Guid eapId, CancellationToken ct = default)
{
const string sql = """
SELECT id, eap_id, criterio_eap_item_id, qt_feed, qt_projeto_executivo, hh_feed, unidade_medida_id
FROM eap_criterio_planejamentos WHERE eap_id = @Id
""";
var rows = await Conn.QueryAsync<EapCriterioPlanejamento>(
new CommandDefinition(sql, new { Id = eapId }, Tx, cancellationToken: ct));
return rows.ToList();
}
public async Task<IReadOnlyList<Guid>> GetItemIdsDoCriterioDaEapAsync(Guid eapId, CancellationToken ct = default)
{
const string sql = """
SELECT i.id
FROM criterio_eap_itens i
JOIN eaps e ON e.criterio_eap_id = i.criterio_eap_id
WHERE e.id = @EapId
""";
var rows = await Conn.QueryAsync<Guid>(
new CommandDefinition(sql, new { EapId = eapId }, Tx, cancellationToken: ct));
return rows.ToList();
}
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 ReplaceAsync(Guid eapId, IReadOnlyList<EapCriterioPlanejamento> itens, CancellationToken ct = default)
{
await Conn.ExecuteAsync(new CommandDefinition(
"DELETE FROM eap_criterio_planejamentos WHERE eap_id = @Id",
new { Id = eapId }, Tx, cancellationToken: ct));
const string sql = """
INSERT INTO eap_criterio_planejamentos
(id, eap_id, criterio_eap_item_id, qt_feed, qt_projeto_executivo, hh_feed, unidade_medida_id)
VALUES (@Id, @EapId, @CriterioEapItemId, @QtFeed, @QtProjetoExecutivo, @HhFeed, @UnidadeMedidaId)
""";
foreach (var p in itens)
await Conn.ExecuteAsync(new CommandDefinition(sql, new
{
p.Id, p.EapId, p.CriterioEapItemId, p.QtFeed, p.QtProjetoExecutivo, p.HhFeed, p.UnidadeMedidaId
}, Tx, cancellationToken: ct));
}
}