Spaces:
Runtime error
Runtime error
File size: 4,103 Bytes
de8b914 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | 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; }
}
}
|