Skip to content

Commit

Permalink
chore: updated paginator, allowed flexible page sizing
Browse files Browse the repository at this point in the history
  • Loading branch information
benborla committed Nov 18, 2023
1 parent 63901aa commit 25ef060
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
9 changes: 6 additions & 3 deletions api/src/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
16 changes: 14 additions & 2 deletions api/src/Pagination/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -110,4 +117,9 @@ public function getResults(): \Traversable
{
return $this->results;
}

public function toArray(): array
{
return get_object_vars($this);
}
}
6 changes: 5 additions & 1 deletion api/src/Repository/FruitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '',
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 25ef060

Please sign in to comment.