namespace CMSMicroservice.Application.CategoryCQ.Queries.GetCategory;
public class GetCategoryQueryHandler : IRequestHandler<GetCategoryQuery, GetCategoryResponseDto>
{
    private readonly IApplicationDbContext _context;

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

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

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

