2026-09-30 11:20:39 +02:00
|
|
|
<?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();
|
|
|
|
|
|
2026-09-30 11:59:57 +02:00
|
|
|
if ($input->pickupExceptionId !== null) {
|
2026-09-30 11:20:39 +02:00
|
|
|
$query->whereHas(
|
|
|
|
|
'pickupExceptions',
|
2026-09-30 11:59:57 +02:00
|
|
|
fn (Builder $q) => $q->where('id', (string) $input->pickupExceptionId)
|
2026-09-30 11:20:39 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|