using Microsoft.EntityFrameworkCore;

namespace Netina.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)
    {
        var discounts = await _repositoryWrapper.SetRepository<Discount>().TableNoTracking
            .Where(d=>!d.IsSpecialOffer)
            .OrderByDescending(b => b.CreatedAt)
            .Skip(request.Page * 15).Take(15)
            .Select(DiscountMapper.ProjectToSDto)
            .ToListAsync(cancellationToken);
        return discounts;
    }
}