ldc-boutique-cnc-sable/app/Queries/Shops/GetPaginatedShopsQuery.php

59 lines
1.7 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace App\Queries\Shops;
use App\Contracts\Query;
use App\Contracts\QueryInput;
use App\Dto\PickupExceptionDto;
use App\Dto\ShopDto;
use App\Models\PickupException;
use App\Models\Shop;
use App\Services\DataTableService;
use Illuminate\Contracts\Database\Query\Builder;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Query<\App\Queries\Shops\GetPaginatedShopsQueryInput, \App\Queries\Shops\GetPaginatedShopsQueryOutput>
*/
final readonly class GetPaginatedShopsQuery implements Query
{
public function __construct(
private DataTableService $dataTable,
) {
}
/**
* @param \App\Queries\Shops\GetPaginatedShopsQueryInput $input
*/
#[\Override]
public function __invoke(QueryInput $input): GetPaginatedShopsQueryOutput
{
Assert::isInstanceOf($input, GetPaginatedShopsQueryInput::class);
$query = Shop::query();
if ($input->pickupExceptionId !== null) {
$query->whereHas(
'pickupExceptions',
fn (Builder $q) => $q->where('id', (string) $input->pickupExceptionId)
);
}
$this->dataTable->sortQuery(Shop::class, $query);
$paginator = $this->dataTable->paginateQuery($query, $input->pagination);
$shops = $paginator->through(fn (Shop $shop): ShopDto => ShopDto::fromModel($shop));
$pickupExceptions = PickupException::query()
->get()
->map(fn (PickupException $pe): PickupExceptionDto => PickupExceptionDto::fromModel($pe));
return new GetPaginatedShopsQueryOutput(
shops: $shops,
pickupExceptions: $pickupExceptions,
);
}
}