autobus-api_old/AutobusApi.Application/Employees/Queries/GetEmployee/GetEmployeeQueryHandler.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.Employees.Queries.GetEmployee;
public class GetEmployeeQueryHandler : IRequestHandler<GetEmployeeQuery, EmployeeDto>
{
private readonly IApplicationDbContext _dbContext;
private readonly IMapper _mapper;
public GetEmployeeQueryHandler(
IApplicationDbContext dbContext,
IMapper mapper)
{
_dbContext = dbContext;
_mapper = mapper;
}
public async Task<EmployeeDto> Handle(
GetEmployeeQuery request,
CancellationToken cancellationToken)
{
var employee = await _dbContext.Employees
.ProjectTo<EmployeeDto>(_mapper.ConfigurationProvider)
.SingleOrDefaultAsync(c => c.Id == request.Id);
if (employee == null)
{
throw new NotFoundException();
}
return employee;
}
}