autobus-api_old/AutobusApi.Application/Companies/Queries/GetCompany/GetCompanyQueryHandler.cs

39 lines
1.0 KiB
C#

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