ldc-boutique-cnc-sable/app/Queries/Discounts/GetDiscountFormDataQuery.php
Jérémy Munsch 44ffc4d34e
feat(DiscountProductDto): add picture property and update fromModel method
feat(ProductDragAndDropComponent): enhance product display with picture and improve layout
2026-10-28 10:02:43 +01:00

64 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Queries\Discounts;
use App\Contracts\Query;
use App\Contracts\QueryInput;
use App\Dto\DiscountDto;
use App\Dto\DiscountProductDto;
use App\Models\Product;
use Domain\Enums\DiscountType;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Query<\App\Queries\Discounts\GetDiscountFormDataQueryInput, \App\Queries\Discounts\GetDiscountFormDataQueryOutput>
*/
final readonly class GetDiscountFormDataQuery implements Query
{
/**
* @param \App\Queries\Discounts\GetDiscountFormDataQueryInput $input
*/
#[\Override]
public function __invoke(QueryInput $input): GetDiscountFormDataQueryOutput
{
Assert::isInstanceOf($input, GetDiscountFormDataQueryInput::class);
$discountTypes = DiscountType::cases();
if ($input->discount !== null) {
$selectedProds = Product::query()
->select(['id', 'name', 'picture'])
->where('discount_id', $input->discount->id)
->orderBy('name')
->get()
->map(fn (Product $p): DiscountProductDto => DiscountProductDto::fromModel($p));
$existingProds = Product::query()
->select(['id', 'name', 'picture'])
->whereNotIn('id', $selectedProds->pluck('id'))
->orderBy('name')
->get()
->map(fn (Product $p): DiscountProductDto => DiscountProductDto::fromModel($p));
$discountDto = DiscountDto::fromModel($input->discount);
} else {
$selectedProds = collect();
$existingProds = Product::query()
->select(['id', 'name', 'picture'])
->orderBy('name')
->get()
->map(fn (Product $p): DiscountProductDto => DiscountProductDto::fromModel($p));
$discountDto = null;
}
return new GetDiscountFormDataQueryOutput(
discount: $discountDto,
discountTypes: $discountTypes,
existingProds: $existingProds,
selectedProds: $selectedProds,
);
}
}