35 lines
976 B
C#
35 lines
976 B
C#
using MediatR;
|
|
using cuqmbr.TravelGuide.Application.Common.Interfaces.Persistence;
|
|
using cuqmbr.TravelGuide.Application.Common.Exceptions;
|
|
|
|
namespace cuqmbr.TravelGuide.Application.Trains.Commands.DeleteTrain;
|
|
|
|
public class DeleteTrainCommandHandler : IRequestHandler<DeleteTrainCommand>
|
|
{
|
|
private readonly UnitOfWork _unitOfWork;
|
|
|
|
public DeleteTrainCommandHandler(UnitOfWork unitOfWork)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
}
|
|
|
|
public async Task Handle(
|
|
DeleteTrainCommand request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var entity = await _unitOfWork.TrainRepository.GetOneAsync(
|
|
e => e.Guid == request.Guid, cancellationToken);
|
|
|
|
if (entity == null)
|
|
{
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
await _unitOfWork.TrainRepository.DeleteOneAsync(
|
|
entity, cancellationToken);
|
|
|
|
await _unitOfWork.SaveAsync(cancellationToken);
|
|
_unitOfWork.Dispose();
|
|
}
|
|
}
|