using Microsoft.EntityFrameworkCore;

namespace Netina.Repository.Handlers.Newsletters;

public class GetNewsletterMembersQueryHandler : IRequestHandler<GetNewsletterMembersQuery,List<NewsletterMemberSDto>>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetNewsletterMembersQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<List<NewsletterMemberSDto>> Handle(GetNewsletterMembersQuery request, CancellationToken cancellationToken)
    {
        var dtos = await _repositoryWrapper.SetRepository<NewsletterMember>()
            .TableNoTracking
            .OrderByDescending(n => n.CreatedAt)
            .Select(NewsletterMemberMapper.ProjectToSDto)
            .Skip(request.Page * 20)
            .Take(20)
            .ToListAsync(cancellationToken);
        return dtos;
    }
}