Spaces:
Runtime error
Runtime error
Download src/SCM.Application/Projects/ProjectService.cs from rafaeldavila/scm-api: direct link, hf CLI and curl.
- Browser
- Download file 3.01 kB
-
https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Application/Projects/ProjectService.cs
- Command line
-
hf download hf://spaces/rafaeldavila/scm-api/src/SCM.Application/Projects/ProjectService.cs
-
curl -L -o ProjectService.cs https://huggingface.co/spaces/rafaeldavila/scm-api/resolve/main/src/SCM.Application/Projects/ProjectService.cs
3.01 kB
| using SCM.Application.Common; | |
| using SCM.Application.Projects.Dtos; | |
| using SCM.Domain.Projects; | |
| using SCM.Shared.Exceptions; | |
| using SCM.Shared.Pagination; | |
| namespace SCM.Application.Projects; | |
| public sealed class ProjectService : IProjectService | |
| { | |
| private readonly IProjectRepository _repository; | |
| private readonly IUnitOfWork _unitOfWork; | |
| public ProjectService(IProjectRepository repository, IUnitOfWork unitOfWork) | |
| { | |
| _repository = repository; | |
| _unitOfWork = unitOfWork; | |
| } | |
| public async Task<PagedResult<ProjectResponse>> GetPagedAsync( | |
| int page, int pageSize, string? search, CancellationToken ct = default) | |
| { | |
| var request = new PageRequest(page, pageSize); | |
| var (items, total) = await _repository.GetPagedAsync(request.Offset, request.PageSize, search, ct); | |
| var responses = items.Select(p => p.ToResponse()).ToList(); | |
| return new PagedResult<ProjectResponse>(responses, request.Page, request.PageSize, total); | |
| } | |
| public async Task<ProjectResponse> GetByIdAsync(Guid id, CancellationToken ct = default) | |
| { | |
| var project = await _repository.GetByIdAsync(id, ct) | |
| ?? throw new NotFoundException($"Obra {id} não encontrada."); | |
| return project.ToResponse(); | |
| } | |
| public async Task<ProjectResponse> CreateAsync(CreateProjectRequest request, CancellationToken ct = default) | |
| { | |
| if (await _repository.CodeExistsAsync(request.Code, null, ct)) | |
| throw new ConflictException($"Já existe uma obra com o código '{request.Code}'."); | |
| var project = Project.Create( | |
| request.Code, request.Name, request.Description, request.StartDate, request.EndDate, DateTime.UtcNow); | |
| await ExecuteInTransactionAsync(() => _repository.AddAsync(project, ct)); | |
| return project.ToResponse(); | |
| } | |
| public async Task<ProjectResponse> UpdateAsync(Guid id, UpdateProjectRequest request, CancellationToken ct = default) | |
| { | |
| var project = await _repository.GetByIdAsync(id, ct) | |
| ?? throw new NotFoundException($"Obra {id} não encontrada."); | |
| project.UpdateDetails( | |
| request.Name, request.Description, request.StartDate, request.EndDate, request.Status, DateTime.UtcNow); | |
| await ExecuteInTransactionAsync(() => _repository.UpdateAsync(project, ct)); | |
| return project.ToResponse(); | |
| } | |
| public async Task DeleteAsync(Guid id, CancellationToken ct = default) | |
| { | |
| var deleted = false; | |
| await ExecuteInTransactionAsync(async () => deleted = await _repository.DeleteAsync(id, ct)); | |
| if (!deleted) | |
| throw new NotFoundException($"Obra {id} não encontrada."); | |
| } | |
| private async Task ExecuteInTransactionAsync(Func<Task> action) | |
| { | |
| _unitOfWork.BeginTransaction(); | |
| try | |
| { | |
| await action(); | |
| _unitOfWork.Commit(); | |
| } | |
| catch | |
| { | |
| _unitOfWork.Rollback(); | |
| throw; | |
| } | |
| } | |
| } | |