ldc-boutique-cnc-sable/app/Http/Requests/Basket/AddBasketRequest.php
Jérémy Munsch 46b5413f07
feat(basket): implement add, remove, and empty basket actions
This commit introduces new actions for adding, removing, and emptying items in the basket, along with their corresponding input classes. It also updates the BasketController to utilize these new actions, enhancing the basket management functionality.
2026-10-27 09:58:35 +01:00

50 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\Basket;
use App\Actions\Basket\AddToBasketInput;
use App\Models\Product;
use Domain\Models\ValueObjects\Entities\ProductId;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Ramsey\Uuid\Uuid;
class AddBasketRequest extends FormRequest
{
public function authorize(): bool
{
$product = Product::query()
->where('id', $this->input('rowId'))
->first();
return $product !== null && Gate::allows('addToBasket', $product);
}
public function toAddToBasketInput(): AddToBasketInput
{
return new AddToBasketInput(
$this->getProductId(),
$this->integer('qty')
);
}
public function getProductId(): ProductId
{
return new ProductId(
Uuid::fromString(
$this->string('rowId')->value()
)
);
}
/** @return array<string,mixed> */
public function rules(): array
{
return [
'rowId' => ['required', 'string', 'uuid', 'exists:products,id'],
'qty' => ['required', 'integer', 'min:1'],
];
}
}