Skip to content

Commit

Permalink
Add history total pages (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
EVDW authored Jul 2, 2021
1 parent f3d9f63 commit 2d6978f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions docker/nginx/nginx.conf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 "*";
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/EndPoint/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/Symfony/Repository/HistoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class HistoryRepository extends ServiceEntityRepository
{
private const LIMIT = 10;
public const LIMIT = 10;

public function __construct(ManagerRegistry $registry)
{
Expand All @@ -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);
Expand Down

0 comments on commit 2d6978f

Please sign in to comment.