scm-api / src /SCM.Application /Medicao /LinhaBaseService.cs
rafaeldavila's picture
Deploy SCM API (.NET 8) - Docker Space
de8b914 verified
Raw History Blame Contribute Delete
4.1 kB
using SCM.Application.Common;
using SCM.Domain.Medicao;
using SCM.Shared.Exceptions;
namespace SCM.Application.Medicao;
public sealed record LinhaBasePontoRequest(int Semana, DateOnly Data, decimal Percentual);
public sealed record DefinirLinhaBaseRequest(IReadOnlyList<LinhaBasePontoRequest> Pontos);
public sealed record LinhaBasePontoResponse(Guid Id, int Semana, DateOnly Data, decimal Percentual, string Origem);
/// <summary>Contrato de persistência da linha de base (implementado em Infra).</summary>
public interface ILinhaBaseRepository
{
Task<IReadOnlyList<LinhaBasePonto>> GetByEapAsync(Guid eapId, CancellationToken ct = default);
Task<bool> EapExistsAsync(Guid eapId, CancellationToken ct = default);
Task ReplaceAsync(Guid eapId, IReadOnlyList<LinhaBasePonto> pontos, CancellationToken ct = default);
Task<bool> DeletePontoAsync(Guid eapId, int semana, CancellationToken ct = default);
}
public interface ILinhaBaseService
{
Task<IReadOnlyList<LinhaBasePontoResponse>> GetByEapAsync(Guid eapId, CancellationToken ct = default);
Task<IReadOnlyList<LinhaBasePontoResponse>> DefinirAsync(Guid eapId, DefinirLinhaBaseRequest request, CancellationToken ct = default);
Task RemoverPontoAsync(Guid eapId, int semana, CancellationToken ct = default);
}
public sealed class LinhaBaseService : ILinhaBaseService
{
private readonly ILinhaBaseRepository _repository;
private readonly IUnitOfWork _unitOfWork;
public LinhaBaseService(ILinhaBaseRepository repository, IUnitOfWork unitOfWork)
{
_repository = repository;
_unitOfWork = unitOfWork;
}
public async Task<IReadOnlyList<LinhaBasePontoResponse>> GetByEapAsync(Guid eapId, CancellationToken ct = default)
{
if (!await _repository.EapExistsAsync(eapId, ct))
throw new NotFoundException($"EAP {eapId} não encontrada.");
return (await _repository.GetByEapAsync(eapId, ct)).Select(ToResponse).ToList();
}
public async Task<IReadOnlyList<LinhaBasePontoResponse>> DefinirAsync(
Guid eapId, DefinirLinhaBaseRequest request, CancellationToken ct = default)
{
if (!await _repository.EapExistsAsync(eapId, ct))
throw new NotFoundException($"EAP {eapId} não encontrada.");
if (request.Pontos.Select(p => p.Semana).Distinct().Count() != request.Pontos.Count)
throw new DomainException("Cada semana só pode ter um ponto na linha de base.");
var pontos = request.Pontos
.OrderBy(p => p.Semana)
.Select(p => LinhaBasePonto.Create(eapId, p.Semana, p.Data, p.Percentual))
.ToList();
// Curva S: o percentual acumulado não pode decrescer com o avanço das semanas.
for (var i = 1; i < pontos.Count; i++)
if (pontos[i].Percentual < pontos[i - 1].Percentual)
throw new DomainException(
$"A curva é acumulada: a semana {pontos[i].Semana} ({pontos[i].Percentual}%) não pode ser menor que a semana {pontos[i - 1].Semana} ({pontos[i - 1].Percentual}%).");
await ExecuteInTransactionAsync(() => _repository.ReplaceAsync(eapId, pontos, ct));
return pontos.Select(ToResponse).ToList();
}
public async Task RemoverPontoAsync(Guid eapId, int semana, CancellationToken ct = default)
{
if (!await _repository.EapExistsAsync(eapId, ct))
throw new NotFoundException($"EAP {eapId} não encontrada.");
var removed = false;
await ExecuteInTransactionAsync(async () => removed = await _repository.DeletePontoAsync(eapId, semana, ct));
if (!removed)
throw new NotFoundException($"Ponto da semana {semana} não encontrado na linha de base.");
}
private static LinhaBasePontoResponse ToResponse(LinhaBasePonto p) =>
new(p.Id, p.Semana, p.Data, p.Percentual, p.Origem);
private async Task ExecuteInTransactionAsync(Func<Task> action)
{
_unitOfWork.BeginTransaction();
try { await action(); _unitOfWork.Commit(); }
catch { _unitOfWork.Rollback(); throw; }
}
}