using Microsoft.EntityFrameworkCore;

namespace NetinaShop.Repository.Handlers.Reviews;

public class GetReviewsQueryHandler : IRequestHandler<GetReviewsQuery,List<ReviewSDto>>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetReviewsQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<List<ReviewSDto>> Handle(GetReviewsQuery request, CancellationToken cancellationToken)
    {
        return await _repositoryWrapper.SetRepository<Review>()
            .TableNoTracking
            .OrderByDescending(r => r.CreatedAt)
            .Skip(request.Page * 15)
            .Take(15)
            .Select(ReviewMapper.ProjectToSDto)
            .ToListAsync(cancellationToken);
    }
}