57 lines
1.6 KiB
C#
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);
|
|
}
|
|
}
|