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

namespace Netina.Repository.Handlers.Warehouses;

public class GetShippingQueryHandler : IRequestHandler<GetShippingQuery, ShippingSDto>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetShippingQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<ShippingSDto> Handle(GetShippingQuery request, CancellationToken cancellationToken)
    {
        var shippingMethod = await _repositoryWrapper.SetRepository<Shipping>().TableNoTracking.Where(b => b.Id == request.Id)
            .Select(ShippingMapper.ProjectToSDto)
            .FirstOrDefaultAsync(cancellationToken);
        if (shippingMethod == null)
            throw new AppException("Shipping method not found", ApiResultStatusCode.NotFound);
        return shippingMethod;
    }
}