using Microsoft.EntityFrameworkCore;
using NetinaShop.Domain.Entities.Warehouses;

namespace NetinaShop.Repository.Handlers.Warehouses;

public class GetShippingsQueryHandler : IRequestHandler<GetShippingsQuery,List<ShippingSDto>>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetShippingsQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<List<ShippingSDto>> Handle(GetShippingsQuery request, CancellationToken cancellationToken)
    {
        return await _repositoryWrapper
            .SetRepository<Shipping>()
            .TableNoTracking.OrderByDescending(b => b.CreatedAt)
            .Skip(request.Page * 20)
            .Take(20)
            .Select(ShippingMapper.ProjectToSDto)
            .ToListAsync(cancellationToken);
    }
}