Spaces:
Runtime error
Runtime error
Download src/SCM.Application/Contratos/ContratoService.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 5.08 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Application/Contratos/ContratoService.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Application/Contratos/ContratoService.cs
-
curl -L -o ContratoService.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Application/Contratos/ContratoService.cs
5.08 kB
| using SCM.Application.Common; | |
| using SCM.Application.Contratos.Dtos; | |
| using SCM.Domain.Contratos; | |
| using SCM.Shared.Exceptions; | |
| namespace SCM.Application.Contratos; | |
| public sealed class ContratoService : IContratoService | |
| { | |
| private readonly IContratoRepository _repository; | |
| private readonly IUnitOfWork _unitOfWork; | |
| public ContratoService(IContratoRepository repository, IUnitOfWork unitOfWork) | |
| { | |
| _repository = repository; | |
| _unitOfWork = unitOfWork; | |
| } | |
| public async Task<IReadOnlyList<ContratoResponse>> GetByObraAsync(Guid obraId, CancellationToken ct = default) | |
| { | |
| var itens = await _repository.GetByObraAsync(obraId, ct); | |
| return itens.Select(x => x.Contrato.ToResponse(x.QtdCriterios)).ToList(); | |
| } | |
| public async Task<ContratoResponse> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| var contrato = await _repository.GetByIdAsync(id, ct) | |
| ?? throw new NotFoundException($"Contrato {id} não encontrado."); | |
| var qtd = await _repository.ContarCriteriosAsync(id, ct); | |
| return contrato.ToResponse(qtd); | |
| } | |
| public async Task<ContratoResponse> CreateAsync(CreateContratoRequest request, CancellationToken ct = default) | |
| { | |
| var contrato = Contrato.Create(request.Codigo, request.Descricao, request.CodProjeto, request.ObraId, request.DataInicio, request.ValorContrato); | |
| await GarantirUnicidadeAsync(contrato, null, ct); | |
| await ExecuteInTransactionAsync(() => _repository.AddAsync(contrato, ct)); | |
| return contrato.ToResponse(0); | |
| } | |
| public async Task<ContratoResponse> UpdateAsync(Guid id, UpdateContratoRequest request, CancellationToken ct = default) | |
| { | |
| var contrato = await _repository.GetByIdAsync(id, ct) | |
| ?? throw new NotFoundException($"Contrato {id} não encontrado."); | |
| contrato.Atualizar(request.Codigo, request.Descricao, request.CodProjeto, request.DataInicio, request.ValorContrato); | |
| await GarantirUnicidadeAsync(contrato, id, ct); | |
| await ExecuteInTransactionAsync(() => _repository.UpdateAsync(contrato, ct)); | |
| var qtd = await _repository.ContarCriteriosAsync(id, ct); | |
| return contrato.ToResponse(qtd); | |
| } | |
| public async Task DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| var contrato = await _repository.GetByIdAsync(id, ct) | |
| ?? throw new NotFoundException($"Contrato {id} não encontrado."); | |
| // Regra do legado: não exclui contrato com critérios EAP vinculados. | |
| if (await _repository.ContarCriteriosAsync(id, ct) > 0) | |
| throw new ConflictException("Não é possível excluir: o contrato possui critérios vinculados."); | |
| await ExecuteInTransactionAsync(() => _repository.DeleteAsync(contrato.Id, ct)); | |
| } | |
| public async Task<SemanaContratoResponse> ObterSemanaPorDataAsync(Guid id, DateOnly data, CancellationToken ct = default) | |
| { | |
| var dataInicio = await ObterDataInicioAsync(id, ct); | |
| var semana = SemanaContrato.SemanaDe(dataInicio, data); | |
| return MontarSemana(dataInicio, semana); | |
| } | |
| public async Task<SemanaContratoResponse> ObterSemanaPorNumeroAsync(Guid id, int semana, CancellationToken ct = default) | |
| { | |
| var dataInicio = await ObterDataInicioAsync(id, ct); | |
| return MontarSemana(dataInicio, semana); | |
| } | |
| private async Task<DateOnly> ObterDataInicioAsync(Guid id, CancellationToken ct) | |
| { | |
| var contrato = await _repository.GetByIdAsync(id, ct) | |
| ?? throw new NotFoundException($"Contrato {id} não encontrado."); | |
| return contrato.DataInicio | |
| ?? throw new DomainException("O contrato não possui data de início definida."); | |
| } | |
| private static SemanaContratoResponse MontarSemana(DateOnly dataInicio, int semana) => new( | |
| semana, | |
| SemanaContrato.InicioDaSemana(dataInicio, semana), | |
| SemanaContrato.FimDaSemana(dataInicio, semana)); | |
| private async Task GarantirUnicidadeAsync(Contrato contrato, Guid? excludeId, CancellationToken ct) | |
| { | |
| if (await _repository.CodigoExistsAsync(contrato.Codigo, contrato.ObraId, excludeId, ct)) | |
| throw new ConflictException($"Já existe um contrato com o código '{contrato.Codigo}' nesta obra."); | |
| if (await _repository.DescricaoExistsAsync(contrato.Descricao, contrato.ObraId, excludeId, ct)) | |
| throw new ConflictException("Já existe um contrato com essa descrição nesta obra."); | |
| if (!string.IsNullOrEmpty(contrato.CodProjeto) | |
| && await _repository.CodProjetoExistsAsync(contrato.CodProjeto, excludeId, ct)) | |
| throw new ConflictException($"Já existe um contrato com o código de projeto '{contrato.CodProjeto}'."); | |
| } | |
| private async Task ExecuteInTransactionAsync(Func<Task> action) | |
| { | |
| _unitOfWork.BeginTransaction(); | |
| try | |
| { | |
| await action(); | |
| _unitOfWork.Commit(); | |
| } | |
| catch | |
| { | |
| _unitOfWork.Rollback(); | |
| throw; | |
| } | |
| } | |
| } | |