autobus-api_old/AutobusApi.Application/VehicleEnrollments/Queries/GetVehicleEnrollment/GetVehicleEnrollmentQueryHandler.cs

37 lines
1.1 KiB
C#

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<GetVehicleEnrollmentQuery, VehicleEnrollmentDto>
{
private readonly IApplicationDbContext _dbContext;
private readonly IMapper _mapper;
public GetVehicleEnrollmentQueryHandler(
IApplicationDbContext dbContext,
IMapper mapper)
{
_dbContext = dbContext;
_mapper = mapper;
}
public async Task<VehicleEnrollmentDto> 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<VehicleEnrollmentDto>(vehicleEnrollment);
}
}