| | 1 | | using AutoMapper; |
| | 2 | | using FLP.Application.Requests.Bugs; |
| | 3 | | using FLP.Application.Responses.Bugs; |
| | 4 | | using FLP.Application.Validators.Bugs; |
| | 5 | | using FLP.Core.Context.Shared; |
| | 6 | | using FLP.Core.Interfaces.Repository; |
| | 7 | | using MediatR; |
| | 8 | | using Microsoft.Extensions.Logging; |
| | 9 | |
|
| | 10 | | namespace FLP.Application.Handlers.Bugs; |
| | 11 | |
|
| 2 | 12 | | public class GetBugsHandler(IUnitOfWork _uow, IMapper _mapper, ILogger<GetBugsHandler> _logger) : IRequestHandler<GetBug |
| | 13 | | { |
| | 14 | | public async Task<BaseResponse<GetBugsPaginatedResponse>> Handle(GetBugsPaginatedRequest request, CancellationToken |
| 2 | 15 | | { |
| 2 | 16 | | _logger.LogInformation("GetBugsHandler called with request: {@Request}", request); |
| | 17 | |
|
| 2 | 18 | | var validator = new GetBugsValidator(); |
| 2 | 19 | | var validationResult = validator.Validate(request); |
| | 20 | |
|
| 2 | 21 | | if (!validationResult.IsValid) |
| 0 | 22 | | { |
| 0 | 23 | | _logger.LogWarning("Validation failed for GetBugsRequest: {Errors}", validationResult.Errors); |
| 0 | 24 | | return new BaseResponse<GetBugsPaginatedResponse>(false,validationResult.Errors.Select(e => e.ErrorMessage)) |
| | 25 | | } |
| | 26 | |
|
| | 27 | | // Retrieve bugs from the repository |
| 2 | 28 | | var bugs = await _uow.BugRepository.GetAsync(request, cancellationToken); |
| 2 | 29 | | var count = _uow.BugRepository.CountAsync(request, cancellationToken); |
| | 30 | |
|
| 2 | 31 | | _logger.LogInformation("Retrieved {Count} bugs.", bugs.Count()); |
| | 32 | | // Map the bugs to response DTOs |
| 2 | 33 | | var response = _mapper.Map<IEnumerable<GetBugResponse>>(bugs); |
| | 34 | |
|
| 2 | 35 | | _logger.LogInformation("Mapped bugs to response DTOs: {@Response}", response); |
| | 36 | |
|
| 2 | 37 | | return new BaseResponse<GetBugsPaginatedResponse>(new GetBugsPaginatedResponse(response, await count)); |
| 2 | 38 | | } |
| | 39 | | } |