autobus-api_old/AutobusApi.Application/TicketGroups/Queries/GetTicketGroup/GetTicketGroupQueryHandler.cs

37 lines
1017 B
C#

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