autobus-api_old/AutobusApi.Application/Routes/Queries/GetRoute/GetRouteQueryHandler.cs

39 lines
1018 B
C#

using AutobusApi.Application.Common.Exceptions;
using AutobusApi.Application.Common.Interfaces;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace AutobusApi.Application.Routes.Queries.GetRoute;
public class GetRouteQueryHandler : IRequestHandler<GetRouteQuery, RouteDto>
{
private readonly IApplicationDbContext _dbContext;
private readonly IMapper _mapper;
public GetRouteQueryHandler(
IApplicationDbContext dbContext,
IMapper mapper)
{
_dbContext = dbContext;
_mapper = mapper;
}
public async Task<RouteDto> Handle(
GetRouteQuery request,
CancellationToken cancellationToken)
{
var route = await _dbContext.Routes
.ProjectTo<RouteDto>(_mapper.ConfigurationProvider)
.SingleOrDefaultAsync(c => c.Id == request.Id);
if (route == null)
{
throw new NotFoundException();
}
return route;
}
}