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