namespace CMSMicroservice.Application.RoleCQ.Queries.GetRole;
public class GetRoleQueryHandler : IRequestHandler<GetRoleQuery, GetRoleResponseDto>
{
    private readonly IApplicationDbContext _context;

    public GetRoleQueryHandler(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<GetRoleResponseDto> Handle(GetRoleQuery request,
        CancellationToken cancellationToken)
    {
        var response = await _context.Roles
            .AsNoTracking()
            .Where(x => x.Id == request.Id)
            .ProjectToType<GetRoleResponseDto>()
            .FirstOrDefaultAsync(cancellationToken);

        return response ?? throw new NotFoundException(nameof(Role), request.Id);
    }
}
