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