| | 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.Exceptions; |
| | 7 | | using FLP.Core.Interfaces.Repository; |
| | 8 | | using MediatR; |
| | 9 | | using Microsoft.Extensions.Logging; |
| | 10 | |
|
| | 11 | | namespace FLP.Application.Handlers.Bugs; |
| | 12 | |
|
| 3 | 13 | | public class GetBugByIdHandler(IUnitOfWork _uow, IMapper _mapper, ILogger<GetBugByIdHandler> _logger) : IRequestHandler< |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// runs the GetBugByIdRequest handler to retrieve a bug by its ID. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="request"></param> |
| | 19 | | /// <param name="cancellationToken"></param> |
| | 20 | | /// <returns></returns> |
| | 21 | | /// <exception cref="ArgumentException"></exception> |
| | 22 | | /// <exception cref="NotFoundException"></exception> |
| | 23 | | public async Task<BaseResponse<GetBugByIdResponse>> Handle(GetBugByIdRequest request, CancellationToken cancellation |
| 3 | 24 | | { |
| 3 | 25 | | cancellationToken.ThrowIfCancellationRequested(); |
| 3 | 26 | | _logger.LogInformation("Handling GetBugByIdRequest for Id: {Id}", request.Id); |
| | 27 | |
|
| | 28 | | // Validate the request |
| 3 | 29 | | var validator = new GetBugByIdValidator(); |
| 3 | 30 | | var validationResult = validator.Validate(request); |
| 3 | 31 | | if (!validationResult.IsValid) |
| 1 | 32 | | { |
| 1 | 33 | | _logger.LogWarning("Validation failed for GetBugByIdRequest: {Errors}", validationResult.Errors); |
| 1 | 34 | | return new BaseResponse<GetBugByIdResponse>(false, validationResult.Errors.Select(e => e.ErrorMessage)); |
| | 35 | | } |
| | 36 | |
|
| 2 | 37 | | var bug = await _uow.BugRepository.GetByIdAsync(request.Id, cancellationToken); |
| | 38 | |
|
| 2 | 39 | | if (bug == null) |
| 1 | 40 | | { |
| 1 | 41 | | return new BaseResponse<GetBugByIdResponse>(false, "Bug not found"); |
| | 42 | | } |
| | 43 | |
|
| 1 | 44 | | _logger.LogInformation("Bug found with Id: {Id}", request.Id); |
| | 45 | | // Map the bug entity to a response DTO |
| 1 | 46 | | var response = _mapper.Map<GetBugByIdResponse>(bug); |
| 1 | 47 | | return new BaseResponse<GetBugByIdResponse>(response); |
| 3 | 48 | | } |
| | 49 | | } |