namespace CMSMicroservice.Application.UserCartsCQ.Queries.GetUserCarts;
public class GetUserCartsQueryHandler : IRequestHandler<GetUserCartsQuery, GetUserCartsResponseDto>
{
    private readonly IApplicationDbContext _context;

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

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

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