rafaeldavila's picture
Deploy SCM API (.NET 8) - Docker Space
de8b914 verified
Raw History Blame Contribute Delete
5.71 kB
using System.Data;
using Dapper;
using SCM.Application.Common;
using SCM.Domain.Contratos;
namespace SCM.Infrastructure.Persistence.Repositories;
public sealed class ContratoRepository : IContratoRepository
{
private readonly IUnitOfWork _uow;
public ContratoRepository(IUnitOfWork uow) => _uow = uow;
private IDbConnection Conn => _uow.Connection;
private IDbTransaction? Tx => _uow.CurrentTransaction;
public async Task<IReadOnlyList<ContratoComContagem>> GetByObraAsync(Guid obraId, CancellationToken ct = default)
{
const string sql = """
SELECT c.id, c.codigo, c.descricao, c.cod_projeto, c.obra_id, c.data_inicio, c.valor_contrato,
(SELECT COUNT(*) FROM criterios_eap x WHERE x.contrato_id = c.id) AS qtd
FROM contratos c
WHERE c.obra_id = @ObraId
ORDER BY c.codigo
""";
var rows = await Conn.QueryAsync<ContratoRow>(
new CommandDefinition(sql, new { ObraId = obraId }, Tx, cancellationToken: ct));
return rows.Select(r => new ContratoComContagem(r.ToDomain(), r.Qtd)).ToList();
}
public async Task<Contrato?> GetByIdAsync(Guid id, CancellationToken ct = default)
{
const string sql = "SELECT id, codigo, descricao, cod_projeto, obra_id, data_inicio, valor_contrato FROM contratos WHERE id = @Id";
return await Conn.QueryFirstOrDefaultAsync<Contrato>(
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 contratos
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> DescricaoExistsAsync(string descricao, Guid obraId, Guid? excludeId = null, CancellationToken ct = default)
{
const string sql = """
SELECT COUNT(*) FROM contratos
WHERE descricao = @Descricao AND obra_id = @ObraId AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId)
""";
var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition(
sql, new { Descricao = descricao, ObraId = obraId, ExcludeId = excludeId }, Tx, cancellationToken: ct));
return count > 0;
}
public async Task<bool> CodProjetoExistsAsync(string codProjeto, Guid? excludeId = null, CancellationToken ct = default)
{
const string sql = """
SELECT COUNT(*) FROM contratos
WHERE cod_projeto = @CodProjeto AND (@ExcludeId::uuid IS NULL OR id <> @ExcludeId)
""";
var count = await Conn.ExecuteScalarAsync<long>(new CommandDefinition(
sql, new { CodProjeto = codProjeto, ExcludeId = excludeId }, Tx, cancellationToken: ct));
return count > 0;
}
public async Task<int> ContarCriteriosAsync(Guid id, CancellationToken ct = default)
{
const string sql = "SELECT COUNT(*) FROM criterios_eap WHERE contrato_id = @Id";
return await Conn.ExecuteScalarAsync<int>(
new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct));
}
public async Task AddAsync(Contrato contrato, CancellationToken ct = default)
{
const string sql = """
INSERT INTO contratos (id, codigo, descricao, cod_projeto, obra_id, data_inicio, valor_contrato)
VALUES (@Id, @Codigo, @Descricao, @CodProjeto, @ObraId, @DataInicio, @ValorContrato)
""";
await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(contrato), Tx, cancellationToken: ct));
}
public async Task UpdateAsync(Contrato contrato, CancellationToken ct = default)
{
const string sql = """
UPDATE contratos
SET codigo = @Codigo, descricao = @Descricao, cod_projeto = @CodProjeto, data_inicio = @DataInicio, valor_contrato = @ValorContrato
WHERE id = @Id
""";
await Conn.ExecuteAsync(new CommandDefinition(sql, ToParameters(contrato), Tx, cancellationToken: ct));
}
public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default)
{
const string sql = "DELETE FROM contratos WHERE id = @Id";
var affected = await Conn.ExecuteAsync(
new CommandDefinition(sql, new { Id = id }, Tx, cancellationToken: ct));
return affected > 0;
}
private static object ToParameters(Contrato c) => new
{
c.Id,
c.Codigo,
c.Descricao,
c.CodProjeto,
c.ObraId,
c.DataInicio,
c.ValorContrato
};
private sealed class ContratoRow
{
public Guid Id { get; set; }
public string Codigo { get; set; } = string.Empty;
public string Descricao { get; set; } = string.Empty;
public string? CodProjeto { get; set; }
public Guid ObraId { get; set; }
public DateOnly? DataInicio { get; set; }
public decimal ValorContrato { get; set; }
public int Qtd { get; set; }
public Contrato ToDomain() => new()
{
Id = Id,
Codigo = Codigo,
Descricao = Descricao,
ValorContrato = ValorContrato,
CodProjeto = CodProjeto,
ObraId = ObraId,
DataInicio = DataInicio
};
}
}