autobus-api_old/AutobusApi.Application/VehicleEnrollments/Commands/DeleteVehicleEnrollment/DeleteVehicleEnrollmentCommandHandler.cs

34 lines
1.0 KiB
C#

using AutobusApi.Application.Common.Exceptions;
using AutobusApi.Application.Common.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace AutobusApi.Application.VehicleEnrollments.Commands.DeleteVehicleEnrollment;
public class DeleteVehicleEnrollmentCommandHandler : IRequestHandler<DeleteVehicleEnrollmentCommand>
{
private readonly IApplicationDbContext _dbContext;
public DeleteVehicleEnrollmentCommandHandler(IApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task Handle(
DeleteVehicleEnrollmentCommand request,
CancellationToken cancellationToken)
{
var vehicleEnrollment = await _dbContext.VehicleEnrollments
.SingleOrDefaultAsync(c => c.Id == request.Id, cancellationToken);
if (vehicleEnrollment == null)
{
throw new NotFoundException();
}
_dbContext.VehicleEnrollments.Remove(vehicleEnrollment);
await _dbContext.SaveChangesAsync(cancellationToken);
}
}