web: Implémente la pagination pour l'API

This commit is contained in:
2023-01-08 12:02:18 +01:00
parent 7a8b719f50
commit bebf37d3ea
3 changed files with 37 additions and 3 deletions

View File

@@ -25,7 +25,7 @@ paths:
maximum: 100
description: The maximum amount of items to return
- in: query
name: offset
name: page
schema:
type: integer
example: 0

View File

@@ -7,14 +7,24 @@ namespace Oki\Controller;
use Oki\DI\DI;
use Oki\Http\HttpResponse;
use Oki\Http\JsonResponse;
use Oki\Validator\PaginationValidator;
use Oki\Validator\PublicationValidator;
class ApiController
{
public function listPackages(DI $di): HttpResponse
{
$packages = $di->getPackageGateway()->listPackages();
return new JsonResponse(200, ['packages' => $packages]);
$limit = PaginationValidator::getLimit($_GET);
$page = PaginationValidator::getPage($_GET);
$total = $di->getPackageGateway()->getCount();
$packages = $di->getPackageGateway()->listPackagesPagination($limit, $page);
return new JsonResponse(200, [
'pagination' => [
'count' => count($packages),
'total' => $total
],
'packages' => $packages
]);
}
public function packageInfo(DI $di, array $params): HttpResponse

View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace Oki\Validator;
class PaginationValidator
{
public static function getLimit(array $get): int
{
if (isset($get['limit']) && is_numeric($get['limit'])) {
return min(100, max(0, intval($get['limit'])));
}
return 5;
}
public static function getPage(array $get): int
{
if (isset($get['page']) && is_numeric($get['page'])) {
return min(100, max(0, intval($get['page'])));
}
return 1;
}
}