Added default sorting by date in descending order for order queries. Updated related views and tests to reflect this change.
54 lines
1.5 KiB
PHP
54 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Queries\Orders;
|
|
|
|
use App\Contracts\Query;
|
|
use App\Contracts\QueryInput;
|
|
use App\Dto\OrderDto;
|
|
use App\Models\Order;
|
|
use App\Services\DataTableService;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\Date;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \App\Contracts\Query<
|
|
* \App\Queries\Orders\GetBoTodayOrdersQueryInput,
|
|
* \Illuminate\Contracts\Pagination\LengthAwarePaginator<int, \App\Dto\OrderDto>
|
|
* >
|
|
*/
|
|
final readonly class GetBoTodayOrdersQuery implements Query
|
|
{
|
|
public function __construct(
|
|
private DataTableService $dataTable,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param \App\Queries\Orders\GetBoTodayOrdersQueryInput $input
|
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator<int, \App\Dto\OrderDto>
|
|
*/
|
|
#[\Override]
|
|
public function __invoke(QueryInput $input): LengthAwarePaginator
|
|
{
|
|
Assert::isInstanceOf($input, GetBoTodayOrdersQueryInput::class);
|
|
|
|
$query = Order::query()->with(['shop']);
|
|
|
|
$query->whereCreatedAtBetween(Date::now(), Date::now());
|
|
|
|
$this->dataTable->sortQuery(
|
|
Order::class,
|
|
$query,
|
|
defaultSortCol: 'date',
|
|
defaultSortWay: \SortDirection::Descending,
|
|
columnMap: ['date' => 'id'],
|
|
);
|
|
|
|
$paginator = $this->dataTable->paginateQuery($query, $input->pagination);
|
|
|
|
return $paginator->through(fn (Order $order): OrderDto => OrderDto::fromModel($order));
|
|
}
|
|
}
|