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