| | 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 | |
|
| 4 | 13 | | public class UpdateBugHandler(IUnitOfWork _uow, IMapper _mapper, ILogger<UpdateBugHandler> _logger) : IRequestHandler<Up |
| | 14 | | { |
| | 15 | | public async Task<BaseResponse<GetBugByIdResponse>> Handle(UpdateBugRequest request, CancellationToken cancellationT |
| 4 | 16 | | { |
| 4 | 17 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 18 | |
|
| 4 | 19 | | _logger.LogInformation("Handling UpdateBugRequest for Bug ID: {BugId}", request.Id); |
| | 20 | |
|
| | 21 | | // Validate the request |
| 4 | 22 | | var validator = new UpdateBugValidator(); |
| 4 | 23 | | var validationResult = validator.Validate(request); |
| 4 | 24 | | if (!validationResult.IsValid) |
| 0 | 25 | | { |
| 0 | 26 | | _logger.LogWarning("Validation failed for UpdateBugRequest: {Errors}", validationResult.Errors); |
| 0 | 27 | | return new BaseResponse<GetBugByIdResponse>(false,validationResult.Errors.Select(e => e.ErrorMessage)); |
| | 28 | | } |
| | 29 | |
|
| 4 | 30 | | _uow.BeginTransaction(cancellationToken); |
| | 31 | |
|
| 4 | 32 | | var bug = await _uow.BugRepository.GetByIdAsync(request.Id, cancellationToken); |
| | 33 | |
|
| 4 | 34 | | if (bug == null) |
| 0 | 35 | | { |
| 0 | 36 | | _logger.LogWarning("Bug with ID: {BugId} not found for update.", request.Id); |
| 0 | 37 | | return new BaseResponse<GetBugByIdResponse>(false,$"Bug with ID {request.Id} not found."); |
| | 38 | | } |
| | 39 | | // Map the request to the bug entity |
| 4 | 40 | | bug.Title = request.Title ?? bug.Title; |
| 4 | 41 | | bug.Description = request.Description ?? bug.Description; |
| 4 | 42 | | bug.UpdateStatus(request.Status); |
| 4 | 43 | | bug.AssignedToUserId = request.AssignedToUserId ?? bug.AssignedToUserId; |
| | 44 | | // Update the bug in the repository |
| 4 | 45 | | await _uow.BugRepository.UpdateAsync(bug, cancellationToken); |
| | 46 | |
|
| | 47 | | // Save changes |
| 4 | 48 | | await _uow.SaveChangesAsync(cancellationToken); |
| | 49 | |
|
| | 50 | | // Commit the transaction |
| 4 | 51 | | _uow.CommitTransaction(cancellationToken); |
| 4 | 52 | | _logger.LogInformation("Bug with ID: {BugId} updated successfully.", request.Id); |
| | 53 | |
|
| 4 | 54 | | var response = _mapper.Map<GetBugByIdResponse>(bug); |
| | 55 | |
|
| 4 | 56 | | return new BaseResponse<GetBugByIdResponse>(response); |
| 4 | 57 | | } |
| | 58 | | } |