http-api/src/Application/Aircrafts/Commands/UpdateAircraft/UpdateAircraftCommandHandler.cs
cuqmbr 3ebd0c3a2c
All checks were successful
/ build (push) Successful in 6m13s
/ tests (push) Successful in 47s
/ build-docker (push) Successful in 6m48s
add vehicles hierarchy management
2025-05-03 10:09:52 +03:00

57 lines
1.6 KiB
C#

using MediatR;
using cuqmbr.TravelGuide.Application.Common.Interfaces.Persistence;
using AutoMapper;
using cuqmbr.TravelGuide.Application.Common.Exceptions;
namespace cuqmbr.TravelGuide.Application.Aircrafts.Commands.UpdateAircraft;
public class UpdateAircraftCommandHandler :
IRequestHandler<UpdateAircraftCommand, AircraftDto>
{
private readonly UnitOfWork _unitOfWork;
private readonly IMapper _mapper;
public UpdateAircraftCommandHandler(
UnitOfWork unitOfWork,
IMapper mapper)
{
_unitOfWork = unitOfWork;
_mapper = mapper;
}
public async Task<AircraftDto> Handle(
UpdateAircraftCommand request,
CancellationToken cancellationToken)
{
var entity = await _unitOfWork.AircraftRepository.GetOneAsync(
e => e.Guid == request.Guid, cancellationToken);
if (entity == null)
{
throw new NotFoundException();
}
var duplicateEntity = await _unitOfWork.AircraftRepository.GetOneAsync(
e => e.Number == request.Number,
cancellationToken);
if (duplicateEntity != null)
{
throw new DuplicateEntityException(
"Aircraft with given number already exists.");
}
entity.Number = request.Number;
entity.Model = request.Model;
entity.Capacity = request.Capacity;
entity = await _unitOfWork.AircraftRepository.UpdateOneAsync(
entity, cancellationToken);
await _unitOfWork.SaveAsync(cancellationToken);
_unitOfWork.Dispose();
return _mapper.Map<AircraftDto>(entity);
}
}