web: Implémente la pagination pour l'API
This commit is contained in:
@@ -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
|
||||
|
@@ -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
|
||||
|
24
web/src/Oki/Validator/PaginationValidator.php
Normal file
24
web/src/Oki/Validator/PaginationValidator.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user