Skip to content

Commit

Permalink
Merge pull request #337 from PrestaShop/pagination
Browse files Browse the repository at this point in the history
`/reports` : Add pagination (per default)
  • Loading branch information
Progi1984 authored Jan 15, 2025
2 parents 17a1aa1 + e8cdfef commit fb40dd0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/Controller/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function reports(Request $request): JsonResponse
{
$executionFilters = [];
$requestParams = $request->query->all();
$paramPage = $requestParams['page'] ?? 1;
$paramLimit = $requestParams['limit'] ?? 20;
$paramLimit = $paramLimit > 100 ? 100 : $paramLimit;

if (isset($requestParams['filter_platform'])) {
$executionFilters['platform'] = $requestParams['filter_platform'];
Expand All @@ -52,7 +55,9 @@ public function reports(Request $request): JsonResponse
}
$executions = $this->executionRepository->findBy($executionFilters, [
'start_date' => 'DESC',
]);
], $paramLimit, ($paramPage - 1) * $paramLimit);

$numExecutions = $this->executionRepository->count($executionFilters);

$reportListing = [];
if (!isset($executionFilters['platform']) && !isset($executionFilters['campaign'])) {
Expand Down Expand Up @@ -108,7 +113,10 @@ public function reports(Request $request): JsonResponse
return ($tm1 < $tm2) ? 1 : (($tm1 > $tm2) ? -1 : 0);
});

return new JsonResponse($reports);
$response = new JsonResponse(['count' => $numExecutions, 'reports' => $reports]);
$response->headers->set('Access-Control-Allow-Origin', '*');

return $response;
}

#[Route('/reports/{idReport}', methods: ['GET'])]
Expand Down
23 changes: 23 additions & 0 deletions src/Repository/ExecutionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,27 @@ public function findAllBetweenDates(string $version, string $startDate, string $

return $qb->getQuery()->getResult();
}

public function count(array $criteria = []): int
{
$qb = $this->createQueryBuilder('e');
$qb->select($qb->expr()->count('e'));
if (isset($criteria['platform'])) {
$qb
->andWhere('e.platform = :platform')
->setParameter('platform', $criteria['platform']);
}
if (isset($criteria['campaign'])) {
$qb
->andWhere('e.campaign = :campaign')
->setParameter('campaign', $criteria['campaign']);
}
if (isset($criteria['version'])) {
$qb
->andWhere('e.version = :version')
->setParameter('version', $criteria['version']);
}

return $qb->getQuery()->getSingleScalarResult();
}
}
7 changes: 5 additions & 2 deletions tests/Controller/ReportControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ public function testReportsFilters(array $query, int $count): void
$this->assertEquals('application/json', $response->headers->get('content-type'));

$content = json_decode($response->getContent(), true);
$this->assertEquals($count, count($content));
$this->assertArrayHasKey('count', $content);
$this->assertGreaterThan(0, $content['count']);
$this->assertArrayHasKey('reports', $content);
$this->assertEquals($count, count($content['reports']));
$datePrevious = null;
foreach ($content as $item) {
foreach ($content['reports'] as $item) {
if ($datePrevious) {
$this->assertGreaterThanOrEqual($item['start_date'], $datePrevious);
}
Expand Down

0 comments on commit fb40dd0

Please sign in to comment.