using Microsoft.EntityFrameworkCore;

namespace Netina.Repository.Handlers.Marketers;

public class GetMarketersQueryHandler : IRequestHandler<GetMarketersQuery,List<MarketerSDto>>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetMarketersQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<List<MarketerSDto>> Handle(GetMarketersQuery request, CancellationToken cancellationToken)
    {
        var query = _repositoryWrapper.SetRepository<Marketer>()
            .TableNoTracking;

        return await query.OrderByDescending(q => q.CreatedAt)
            .Skip(request.Page * 20)
            .Take(20)
            .Select(MarketerMapper.ProjectToSDto)
            .ToListAsync(cancellationToken);
    }
}