using AutobusApi.Application.Common.Exceptions; using AutobusApi.Application.Common.Interfaces; using AutoMapper; using MediatR; using Microsoft.EntityFrameworkCore; namespace AutobusApi.Application.VehicleEnrollments.Queries.GetVehicleEnrollment; public class GetVehicleEnrollmentQueryHandler : IRequestHandler { private readonly IApplicationDbContext _dbContext; private readonly IMapper _mapper; public GetVehicleEnrollmentQueryHandler( IApplicationDbContext dbContext, IMapper mapper) { _dbContext = dbContext; _mapper = mapper; } public async Task Handle( GetVehicleEnrollmentQuery request, CancellationToken cancellationToken) { var vehicleEnrollment = await _dbContext.VehicleEnrollments .SingleOrDefaultAsync(c => c.Id == request.Id); if (vehicleEnrollment == null) { throw new NotFoundException(); } return _mapper.Map(vehicleEnrollment); } }