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

namespace NetinaShop.Repository.Handlers.Warehouses;

public class DeleteShippingCommandHandler : IRequestHandler<DeleteShippingCommand, bool>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public DeleteShippingCommandHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<bool> Handle(DeleteShippingCommand request, CancellationToken cancellationToken)
    {
        var ent = await _repositoryWrapper.SetRepository<Shipping>().TableNoTracking
            .FirstOrDefaultAsync(s => s.Id == request.Id, cancellationToken);
        if (ent == null)
            throw new AppException("Shipping not found", ApiResultStatusCode.NotFound);

        _repositoryWrapper.SetRepository<Shipping>().Delete(ent);
        await _repositoryWrapper.SaveChangesAsync(cancellationToken);
        return true;
    }
}