From 2d6978fdb5b213e3ccd46f7d00a9dee619e5f671 Mon Sep 17 00:00:00 2001 From: Emmanuelle Collet Date: Fri, 2 Jul 2021 11:00:35 +0200 Subject: [PATCH] Add history total pages (#28) --- docker/nginx/nginx.conf.tmpl | 1 + src/Symfony/EndPoint/History.php | 5 ++++- src/Symfony/Repository/HistoryRepository.php | 14 +++++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/docker/nginx/nginx.conf.tmpl b/docker/nginx/nginx.conf.tmpl index c0d0c35..8963a12 100644 --- a/docker/nginx/nginx.conf.tmpl +++ b/docker/nginx/nginx.conf.tmpl @@ -20,6 +20,7 @@ server { # allow cross origin requests add_header Access-Control-Allow-Origin "*"; + add_header Access-Control-Expose-Headers "Total-Pages" always; if ($request_method = "OPTIONS") { add_header Access-Control-Allow-Origin "*"; diff --git a/src/Symfony/EndPoint/History.php b/src/Symfony/EndPoint/History.php index af29f49..baa97fd 100644 --- a/src/Symfony/EndPoint/History.php +++ b/src/Symfony/EndPoint/History.php @@ -25,7 +25,10 @@ public function list(Request $request, HistoryRepository $repository): JsonRespo $definitions[] = new HistoryDefinition($history); } - return new JsonResponse($definitions); + $totalHistories = $repository->countAllForUser($this->getUser()); + $totalPages = ceil($totalHistories / HistoryRepository::LIMIT); + + return new JsonResponse($definitions, Response::HTTP_OK, ['Total-Pages' => $totalPages]); } public function create(Request $request, HistoryRepository $repository): JsonResponse diff --git a/src/Symfony/Repository/HistoryRepository.php b/src/Symfony/Repository/HistoryRepository.php index 2b99a38..ac66348 100644 --- a/src/Symfony/Repository/HistoryRepository.php +++ b/src/Symfony/Repository/HistoryRepository.php @@ -9,7 +9,7 @@ class HistoryRepository extends ServiceEntityRepository { - private const LIMIT = 10; + public const LIMIT = 10; public function __construct(ManagerRegistry $registry) { @@ -30,6 +30,18 @@ public function findAllForUser(User $user, int $page = 1): array ; } + public function countAllForUser(User $user): int + { + return $this + ->createQueryBuilder('history') + ->select('COUNT(history.id)') + ->andWhere('history.user = :user') + ->setParameter('user', $user) + ->getQuery() + ->getSingleScalarResult() + ; + } + public function save(History $history): void { $this->_em->persist($history);