namespace NetinaShop.Repository.Handlers.Discounts;

public class GetDiscountsQueryHandler : IRequestHandler<GetDiscountsQuery, List<DiscountSDto>>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetDiscountsQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<List<DiscountSDto>> Handle(GetDiscountsQuery request, CancellationToken cancellationToken)
    {
        return await _repositoryWrapper.SetRepository<Discount>().TableNoTracking
            .OrderByDescending(b => b.CreatedAt)
            .Skip(request.Page * 10).Take(10)
            .Select(DiscountMapper.ProjectToSDto)
            .ToListAsync(cancellationToken);
    }
}