Spaces:
Runtime error
Runtime error
Download src/SCM.Application/Obras/ObraService.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 3.05 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Application/Obras/ObraService.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Application/Obras/ObraService.cs
-
curl -L -o ObraService.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Application/Obras/ObraService.cs
3.05 kB
| using SCM.Application.Common; | |
| using SCM.Application.Obras.Dtos; | |
| using SCM.Domain.Obras; | |
| using SCM.Shared.Exceptions; | |
| namespace SCM.Application.Obras; | |
| public sealed class ObraService : IObraService | |
| { | |
| private readonly IObraRepository _repository; | |
| private readonly IUnitOfWork _unitOfWork; | |
| public ObraService(IObraRepository repository, IUnitOfWork unitOfWork) | |
| { | |
| _repository = repository; | |
| _unitOfWork = unitOfWork; | |
| } | |
| public async Task<IReadOnlyList<ObraResponse>> GetAllAsync(CancellationToken ct = default) | |
| { | |
| var itens = await _repository.GetAllAsync(ct); | |
| return itens.Select(x => x.Obra.ToResponse(x.QtdVinculos)).ToList(); | |
| } | |
| public async Task<ObraResponse> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| var obra = await _repository.GetByIdAsync(id, ct) | |
| ?? throw new NotFoundException($"Obra {id} não encontrada."); | |
| var qtd = await _repository.ContarVinculosAsync(id, ct); | |
| return obra.ToResponse(qtd); | |
| } | |
| public async Task<ObraResponse> CreateAsync(CreateObraRequest request, CancellationToken ct = default) | |
| { | |
| var obra = Obra.Create(request.Codigo, request.Nome); | |
| if (await _repository.CodigoExistsAsync(obra.Codigo, null, ct)) | |
| throw new ConflictException($"Já existe uma obra com o código '{obra.Codigo}'."); | |
| await ExecuteInTransactionAsync(() => _repository.AddAsync(obra, ct)); | |
| return obra.ToResponse(0); | |
| } | |
| public async Task<ObraResponse> UpdateAsync(Guid id, UpdateObraRequest request, CancellationToken ct = default) | |
| { | |
| var obra = await _repository.GetByIdAsync(id, ct) | |
| ?? throw new NotFoundException($"Obra {id} não encontrada."); | |
| obra.Atualizar(request.Codigo, request.Nome); | |
| if (await _repository.CodigoExistsAsync(obra.Codigo, id, ct)) | |
| throw new ConflictException($"Já existe uma obra com o código '{obra.Codigo}'."); | |
| await ExecuteInTransactionAsync(() => _repository.UpdateAsync(obra, ct)); | |
| var qtd = await _repository.ContarVinculosAsync(id, ct); | |
| return obra.ToResponse(qtd); | |
| } | |
| public async Task DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| var obra = await _repository.GetByIdAsync(id, ct) | |
| ?? throw new NotFoundException($"Obra {id} não encontrada."); | |
| // Não exclui obra com registros vinculados (critérios, qualificações, itens de controle). | |
| if (await _repository.ContarVinculosAsync(id, ct) > 0) | |
| throw new ConflictException("Não é possível excluir: a obra possui registros vinculados."); | |
| await ExecuteInTransactionAsync(() => _repository.DeleteAsync(obra.Id, ct)); | |
| } | |
| private async Task ExecuteInTransactionAsync(Func<Task> action) | |
| { | |
| _unitOfWork.BeginTransaction(); | |
| try | |
| { | |
| await action(); | |
| _unitOfWork.Commit(); | |
| } | |
| catch | |
| { | |
| _unitOfWork.Rollback(); | |
| throw; | |
| } | |
| } | |
| } | |