Skip to content

Commit

Permalink
Add custom ordering functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Hermanek committed Aug 14, 2024
1 parent 8b1f6d3 commit a1fa4ec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/ApiFilter/ApiQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ApiQuery

/**
* @param list<CustomFilterInterface> $customFilters
* @param list<CustomOrderInterface> $customOrders
*
* @throws ORMException
*/
Expand All @@ -32,6 +33,7 @@ public function __construct(
#[Deprecated] protected ?CustomFilterInterface $customFilter = null,
protected bool $fetchOneAdditionalRecord = false,
protected array $customFilters = [],
protected array $customOrders = [],
) {
if ($this->customFilter instanceof CustomFilterInterface && empty($this->customFilters)) {
$this->customFilters = [$this->customFilter];
Expand Down Expand Up @@ -84,6 +86,10 @@ private function applyOrders(): void
foreach ($this->apiParams->getOrder() as $field => $direction) {
$this->dqb->addOrderBy('t.' . $field, $direction);
}

foreach ($this->customOrders as $customOrder) {
$customOrder->apply($this->dqb, $this->apiParams);
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/ApiFilter/CustomOrderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace AnzuSystems\CommonBundle\ApiFilter;

use Doctrine\ORM\QueryBuilder;

interface CustomOrderInterface
{
public function apply(QueryBuilder $dqb, ApiParams $apiParams): QueryBuilder;
}
9 changes: 8 additions & 1 deletion src/Repository/AbstractAnzuRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use AnzuSystems\CommonBundle\ApiFilter\ApiQuery;
use AnzuSystems\CommonBundle\ApiFilter\ApiResponseList;
use AnzuSystems\CommonBundle\ApiFilter\CustomFilterInterface;
use AnzuSystems\CommonBundle\ApiFilter\CustomOrderInterface;
use AnzuSystems\Contracts\Entity\AnzuUser;
use AnzuSystems\Contracts\Entity\Interfaces\UserTrackingInterface;
use Closure;
Expand Down Expand Up @@ -64,13 +65,15 @@ public function getAllByIdIndexed(int | string ...$id): ArrayCollection

/**
* @param list<CustomFilterInterface> $customFilters
* @param list<CustomOrderInterface> $customOrders
*
* @throws ORMException
*/
public function findByApiParams(
ApiParams $apiParams,
#[Deprecated] ?CustomFilterInterface $customFilter = null,
array $customFilters = [],
array $customOrders = [],
?Closure $mapDataFn = null,
): ApiResponseList {
if ($customFilter instanceof CustomFilterInterface && empty($customFilters)) {
Expand All @@ -81,7 +84,8 @@ public function findByApiParams(
entityManager: $this->getEntityManager(),
metadata: $this->getClassMetadata(),
apiParams: $apiParams,
customFilters: $customFilters
customFilters: $customFilters,
customOrders: $customOrders,
);

$data = $apiQuery->getData();
Expand All @@ -104,6 +108,7 @@ public function findByApiParams(

/**
* @param list<CustomFilterInterface> $customFilters
* @param list<CustomOrderInterface> $customOrders
* @param ?CustomFilterInterface $customFilter Deprecated
*
* @throws ORMException
Expand All @@ -112,6 +117,7 @@ public function findByApiParamsWithInfiniteListing(
ApiParams $apiParams,
#[Deprecated] ?CustomFilterInterface $customFilter = null,
array $customFilters = [],
array $customOrders = [],
?Closure $mapDataFn = null,
): ApiInfiniteResponseList {
if ($customFilter instanceof CustomFilterInterface && empty($customFilters)) {
Expand All @@ -124,6 +130,7 @@ public function findByApiParamsWithInfiniteListing(
apiParams: $apiParams,
fetchOneAdditionalRecord: true,
customFilters: $customFilters,
customOrders: $customOrders,
);
$data = $apiQuery->getData();
$totalCount = $apiParams->getLimit() + $apiParams->getOffset() + 1;
Expand Down

0 comments on commit a1fa4ec

Please sign in to comment.