using Netina.Domain.Entities.Warehouses;

namespace Netina.Repository.Handlers.Warehouses;

public class DeleteShippingCommandHandler(IRepositoryWrapper repositoryWrapper)
    : IRequestHandler<DeleteShippingCommand, bool>
{
    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;
    }
}