From 502894eceab32409dd1d9200fd0e216c03d1c12a Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 21 Nov 2023 09:19:57 +0100 Subject: [PATCH] add limit and offset for apicontroller --- .../Controller/NewsItemApiController.php | 5 ++++- .../Repository/NewsItemRepository.php | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Controller/NewsItemApiController.php b/src/Bundle/ChillMainBundle/Controller/NewsItemApiController.php index 25fe7620f..5224228a8 100644 --- a/src/Bundle/ChillMainBundle/Controller/NewsItemApiController.php +++ b/src/Bundle/ChillMainBundle/Controller/NewsItemApiController.php @@ -36,7 +36,10 @@ class NewsItemApiController { $total = $this->newsItemRepository->countWithDateFilter(); $paginator = $this->paginatorFactory->create($total); - $newsItems = $this->newsItemRepository->findWithDateFilter(); + $newsItems = $this->newsItemRepository->findWithDateFilter( + $limit = $paginator->getItemsPerPage(), + $offset = $paginator->getCurrentPage()->getFirstItemNumber() + ); return new JsonResponse($this->serializer->serialize( new Collection(array_values($newsItems), $paginator), diff --git a/src/Bundle/ChillMainBundle/Repository/NewsItemRepository.php b/src/Bundle/ChillMainBundle/Repository/NewsItemRepository.php index b9b0f8727..88b597647 100644 --- a/src/Bundle/ChillMainBundle/Repository/NewsItemRepository.php +++ b/src/Bundle/ChillMainBundle/Repository/NewsItemRepository.php @@ -80,9 +80,19 @@ class NewsItemRepository implements ObjectRepository return $qb->getQuery()->getResult(); } - public function findWithDateFilter() + public function findWithDateFilter($limit = null, $offset = null) { - return $this->buildQueryWithDateFilter() + $qb = $this->buildQueryWithDateFilter(); + + if ($limit) { + $qb->setMaxResults($limit); + } + + if ($offset) { + $qb->setFirstResult($offset); + } + + return $qb ->getQuery() ->getResult(); }