Skip to content
Snippets Groups Projects
Commit daf1d3df authored by Tobias Gross-Vogt's avatar Tobias Gross-Vogt
Browse files

do pagination in backend

parent 26565530
No related branches found
No related tags found
No related merge requests found
Pipeline #195101 passed
......@@ -179,15 +179,9 @@
"version": "v0.1.42",
"source": {
"type": "git",
"url": "https://gitlab.tugraz.at/dbp/relay/dbp-relay-core-bundle.git",
"url": "git@gitlab.tugraz.at:dbp/relay/dbp-relay-core-bundle.git",
"reference": "6ae27634574cff5f08ef2ff6cbb3cf72c155cf20"
},
"dist": {
"type": "zip",
"url": "https://gitlab.tugraz.at/api/v4/projects/dbp%2Frelay%2Fdbp-relay-core-bundle/repository/archive.zip?sha=6ae27634574cff5f08ef2ff6cbb3cf72c155cf20",
"reference": "6ae27634574cff5f08ef2ff6cbb3cf72c155cf20",
"shasum": ""
},
"require": {
"api-platform/core": "^2.6.6",
"doctrine/annotations": "^1.0",
......@@ -284,11 +278,7 @@
"AGPL-3.0-or-later"
],
"description": "The core bundle of the Relay API gateway",
"support": {
"source": "https://gitlab.tugraz.at/dbp/relay/dbp-relay-core-bundle/-/tree/v0.1.42",
"issues": "https://gitlab.tugraz.at/dbp/relay/dbp-relay-core-bundle/-/issues"
},
"time": "2022-09-14T12:25:56+02:00"
"time": "2022-09-14T10:25:56+00:00"
},
{
"name": "doctrine/annotations",
......@@ -10255,5 +10245,5 @@
"platform-overrides": {
"php": "7.3"
},
"plugin-api-version": "2.3.0"
"plugin-api-version": "2.2.0"
}
......@@ -6,6 +6,7 @@ namespace Dbp\Relay\BasePersonBundle\API;
use Dbp\Relay\BasePersonBundle\Entity\Person;
use Dbp\Relay\CoreBundle\Exception\ApiError;
use Dbp\Relay\CoreBundle\Pagination\Paginator;
interface PersonProviderInterface
{
......@@ -16,10 +17,8 @@ interface PersonProviderInterface
* * LocalData::QUERY_PARAMETER_NAME
*
* @throws ApiError
*
* @return Person[]
*/
public function getPersons(array $options): array;
public function getPersons(array $options): Paginator;
/**
* Throws an HTTP_NOT_FOUND exception if no person with the given ID can be found.
......
......@@ -8,19 +8,21 @@ use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use Dbp\Relay\BasePersonBundle\API\PersonProviderInterface;
use Dbp\Relay\BasePersonBundle\Entity\Person;
use Dbp\Relay\CoreBundle\Helpers\ArrayFullPaginator;
use Dbp\Relay\CoreBundle\LocalData\LocalData;
use Dbp\Relay\CoreBundle\Pagination\Pagination;
use Dbp\Relay\CoreBundle\Pagination\Paginator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
final class PersonCollectionDataProvider extends AbstractController implements CollectionDataProviderInterface, RestrictedDataProviderInterface
{
public const ITEMS_PER_PAGE = 250;
public const MAX_NUM_ITEMS_PER_PAGE_DEFAULT = 50;
private $api;
/** @var PersonProviderInterface */
private $personProvider;
public function __construct(PersonProviderInterface $api)
public function __construct(PersonProviderInterface $personProvider)
{
$this->api = $api;
$this->personProvider = $personProvider;
}
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
......@@ -28,12 +30,10 @@ final class PersonCollectionDataProvider extends AbstractController implements C
return Person::class === $resourceClass;
}
public function getCollection(string $resourceClass, string $operationName = null, array $context = []): ArrayFullPaginator
public function getCollection(string $resourceClass, string $operationName = null, array $context = []): Paginator
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$perPage = self::ITEMS_PER_PAGE;
$page = 1;
$filters = $context['filters'] ?? [];
$options = [];
......@@ -42,18 +42,8 @@ final class PersonCollectionDataProvider extends AbstractController implements C
}
LocalData::addOptions($options, $filters);
Pagination::addOptions($options, $filters, self::MAX_NUM_ITEMS_PER_PAGE_DEFAULT);
$persons = $this->api->getPersons($options);
if (isset($filters['page'])) {
$page = (int) $filters['page'];
}
if (isset($filters['perPage'])) {
$perPage = (int) $filters['perPage'];
}
// TODO: do pagination via API
return new ArrayFullPaginator($persons, $page, $perPage);
return $this->personProvider->getPersons($options);
}
}
......@@ -19,6 +19,7 @@ use Dbp\Relay\CoreBundle\LocalData\LocalDataAwareTrait;
* "parameters" = {
* {"name" = "includeLocal", "in" = "query", "description" = "Local data attributes to include", "type" = "string"},
* {"name" = "queryLocal", "in" = "query", "description" = "Local query parameters to apply", "type" = "string"},
* {"name" = "partialPagination", "in" = "query", "description" = "Partial pagination", "type" = "bool", "example" = "false"}
* }
* }
* },
......
......@@ -6,6 +6,9 @@ namespace Dbp\Relay\BasePersonBundle\Service;
use Dbp\Relay\BasePersonBundle\API\PersonProviderInterface;
use Dbp\Relay\BasePersonBundle\Entity\Person;
use Dbp\Relay\CoreBundle\Pagination\FullPaginator;
use Dbp\Relay\CoreBundle\Pagination\Pagination;
use Dbp\Relay\CoreBundle\Pagination\Paginator;
class DummyPersonProvider implements PersonProviderInterface
{
......@@ -19,14 +22,15 @@ class DummyPersonProvider implements PersonProviderInterface
$this->currentIdentifier = null;
}
public function getPersons(array $options): array
public function getPersons(array $options): Paginator
{
$person = $this->getCurrentPerson();
if ($person !== null) {
return [$person];
$persons = [];
$currentPerson = $this->getCurrentPerson();
if ($currentPerson !== null) {
$persons[] = $currentPerson;
}
return [];
return new FullPaginator($persons, 1, Pagination::MAX_NUM_ITEMS_PER_PAGE_DEFAULT, count($persons));
}
public function getPerson(string $id, array $options = []): Person
......
......@@ -6,6 +6,9 @@ namespace Dbp\Relay\BasePersonBundle\TestUtils;
use Dbp\Relay\BasePersonBundle\API\PersonProviderInterface;
use Dbp\Relay\BasePersonBundle\Entity\Person;
use Dbp\Relay\CoreBundle\Pagination\FullPaginator;
use Dbp\Relay\CoreBundle\Pagination\Pagination;
use Dbp\Relay\CoreBundle\Pagination\Paginator;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class DummyPersonProvider implements PersonProviderInterface
......@@ -18,9 +21,11 @@ class DummyPersonProvider implements PersonProviderInterface
$this->person = $person;
}
public function getPersons(array $options): array
public function getPersons(array $options): Paginator
{
return [$this->person];
$persons = [$this->person];
return new FullPaginator($persons, 1, Pagination::MAX_NUM_ITEMS_PER_PAGE_DEFAULT, count($persons));
}
public function getPerson(string $id, array $options = []): Person
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment