namespace Netina.Repository.Handlers.Newsletters;

public class GetNewsletterMembersQueryHandler(IRepositoryWrapper repositoryWrapper)
    : IRequestHandler<GetNewsletterMembersQuery, List<NewsletterMemberSDto>>
{
    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;
    }
}