autobus-api_old/AutobusApi.Application/Regions/Queries/GetRegion/GetRegionQueryHandler.cs

37 lines
952 B
C#

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