From 25ef0602af12420055e1a0085244828a5dad4798 Mon Sep 17 00:00:00 2001 From: Ben Borla Date: Sat, 18 Nov 2023 13:24:07 +0800 Subject: [PATCH] chore: updated paginator, allowed flexible page sizing --- api/src/Controller/IndexController.php | 9 ++++++--- api/src/Pagination/Paginator.php | 16 ++++++++++++++-- api/src/Repository/FruitRepository.php | 6 +++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/api/src/Controller/IndexController.php b/api/src/Controller/IndexController.php index 2b21917..9f2e1e9 100644 --- a/api/src/Controller/IndexController.php +++ b/api/src/Controller/IndexController.php @@ -14,14 +14,17 @@ final class IndexController extends AbstractController public function all(Request $request, FruitRepository $fruits): JsonResponse { $page = (int) $request->get('page'); + $size = (int) $request->get('size'); $orderBy = $request->get('order_by', 'name'); $search = $request->get('search'); $direction = $request->get('direction', 'ASC'); /** @var \ArrayIterator $result **/ - $result = $fruits->all($page, $orderBy, $direction, $search) - ->getResults(); + $result = $fruits->all($page, $size, $orderBy, $direction, $search)->toArray(); - return new JsonResponse($result->getArrayCopy()); + // @INFO: Remove irrelevant property when responding + unset($result['queryBuilder']); + + return new JsonResponse($result); } } diff --git a/api/src/Pagination/Paginator.php b/api/src/Pagination/Paginator.php index 91fd313..ccd821a 100644 --- a/api/src/Pagination/Paginator.php +++ b/api/src/Pagination/Paginator.php @@ -20,7 +20,7 @@ final class Paginator public function __construct( private readonly DoctrineQueryBuilder $queryBuilder, - private readonly int $pageSize = self::PAGE_SIZE + private int $pageSize = self::PAGE_SIZE ) { } @@ -31,7 +31,7 @@ public function paginate(int $page = 1): self $query = $this->queryBuilder ->setFirstResult($firstResult) - ->setMaxResults($this->pageSize) + ->setMaxResults($this->getPageSize()) ->getQuery(); // @INFO: Set Hydration to Array, to fit on JSON response @@ -68,6 +68,13 @@ public function getLastPage(): int return (int) ceil($this->numResults / $this->pageSize); } + public function setPageSize(int $pageSize = 10): self + { + $this->pageSize = $pageSize; + + return $this; + } + public function getPageSize(): int { return $this->pageSize; @@ -110,4 +117,9 @@ public function getResults(): \Traversable { return $this->results; } + + public function toArray(): array + { + return get_object_vars($this); + } } diff --git a/api/src/Repository/FruitRepository.php b/api/src/Repository/FruitRepository.php index 4d9adc0..3a775b1 100644 --- a/api/src/Repository/FruitRepository.php +++ b/api/src/Repository/FruitRepository.php @@ -29,6 +29,7 @@ public function __construct(ManagerRegistry $registry) */ public function all( int $page = 1, + int $size = 10, string $orderBy = 'name', string $direction = 'ASC', string $search = '', @@ -38,10 +39,13 @@ public function all( ->orWhere('f.family LIKE :search') ->orWhere('f.genus LIKE :search') ->orWhere('f.fruitOrder LIKE :search') + ->orWhere('f.source LIKE :search') ->orderBy("f.$orderBy", $direction) ->setParameter('search', "%$search%"); - return (new Paginator($qb))->paginate($page); + return (new Paginator($qb)) + ->setPageSize($size) + ->paginate($page); } /**