namespace CMSMicroservice.Application.UserCQ.Queries.GetUser;
public class GetUserQueryHandler : IRequestHandler<GetUserQuery, GetUserResponseDto>
{
    private readonly IApplicationDbContext _context;

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

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

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