HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Shipments/ShipmentApiController.php
<?php

namespace App\Shipments;

use App\Orders\Product\OrderedProduct;
use App\Shipments\Requests\ClearOrderedProductsFromShipment;
use App\Shipments\Requests\SearchRequest;
use App\Shipments\Requests\ShipmentWithOrderedProductsRequest;
use App\Shipments\Resources\Shipment as ShipmentResource;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

class ShipmentApiController
{
    use AuthorizesRequests;

    protected string $classModelName = Shipment::class;
    private ShipmentService $shipmentService;

    public function __construct()
    {


        $this->shipmentService = new ShipmentService();
    }

    /**
     * @param $model
     * @return ShipmentResource
     */
    public function show($model)
    {
        $this->authorize('show', $model);
        $model->load('order', 'order.orderedProducts');
        return new ShipmentResource($model);
    }


    /**
     * This method is called on the overview page.
     * It will render the section and view.
     *
     * @return AnonymousResourceCollection
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function index()
    {
        $this->authorize('index', $this->classModelName);
        return ShipmentResource::collection($this->shipmentService->models()->get());
    }

    public function update($model)
    {
        $this->authorize('update', $this->classModelName);
        return new ShipmentResource($this->shipmentService->saveFromJsonArray(request()->all()));
    }

    /**
     * Search for orders
     *
     * @param SearchRequest $request
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function shipmentSearch(SearchRequest $request)
    {
        $resultsPerPage = (is_numeric($request->get('perpage')) ? (int) $request->get('perpage') : 5);
        $shipments = $this->shipmentService->search($request->all())->orderBy('created_at', 'desc')->with('order')->paginate($resultsPerPage);
        $shipments->appends($request->all()); //Add existing query string parameters to the current pagination links

        return ShipmentResource::collection($shipments);
    }

    /**
     * @param ShipmentWithOrderedProductsRequest $request*
     * @return void|null
     */
    public function removeOrderedProductsFromShipment(ShipmentWithOrderedProductsRequest $request) {
        $this->authorize('update', $this->classModelName);

        $orderedProductIds = $request->get('ordered_product_ids');
        if(!$orderedProductIds) return null;
        $orderedProductIds = array_map(fn($orderedProductId) => (int) $orderedProductId, $orderedProductIds);

        OrderedProduct::whereIn('id', $orderedProductIds)->update(['shipment_id' => null]);
    }

    /**
     * @param ShipmentWithOrderedProductsRequest $request
     * @return void|null
     */
    public function addOrderedProductsFromShipment(ShipmentWithOrderedProductsRequest $request) {
        $shipmentId = (int) $request->get('shipment_id');
        $this->authorize('update', $this->classModelName::find($shipmentId));

        $shipmentId = (int) $request->get('shipment_id');
        $orderedProductIds = $request->get('ordered_product_ids');
        if(!$orderedProductIds) return null;
        $orderedProductIds = array_map(fn($orderedProductId) => (int) $orderedProductId, $orderedProductIds);

        /** @var Shipment $shipment */
        OrderedProduct::whereIn('id', $orderedProductIds)->update(['shipment_id' => $shipmentId]);
        return;
    }

    /**
     * @param ClearOrderedProductsFromShipment $request
     */
    public function clearOrderedProductsFromShipment(ClearOrderedProductsFromShipment $request) {
        $shipmentId = (int) $request->get('shipment_id');
        $this->authorize('update', $this->classModelName::find($shipmentId));

        OrderedProduct::where('shipment_id', '=', $shipmentId)->update(['shipment_id' => null]);
    }
}