autobus-api_old/AutobusApi.Application/Cities/Queries/GetCity/GetCityQueryHandler.cs

37 lines
928 B
C#

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