2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Basket;
|
|
|
|
|
|
2026-09-30 10:24:52 +02:00
|
|
|
use App\Actions\Basket\AddToBasketInput;
|
2026-06-14 21:32:07 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-30 10:24:52 +02:00
|
|
|
public function toAddToBasketInput(): AddToBasketInput
|
2026-06-14 21:32:07 +02:00
|
|
|
{
|
2026-09-30 10:24:52 +02:00
|
|
|
return new AddToBasketInput(
|
|
|
|
|
$this->getProductId(),
|
|
|
|
|
$this->integer('qty')
|
|
|
|
|
);
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getProductId(): ProductId
|
|
|
|
|
{
|
|
|
|
|
return new ProductId(
|
|
|
|
|
Uuid::fromString(
|
|
|
|
|
$this->string('rowId')->value()
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-09-30 10:24:52 +02:00
|
|
|
|
|
|
|
|
/** @return array<string,mixed> */
|
|
|
|
|
public function rules(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'rowId' => ['required', 'string', 'uuid', 'exists:products,id'],
|
|
|
|
|
'qty' => ['required', 'integer', 'min:1'],
|
|
|
|
|
];
|
|
|
|
|
}
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|