mirror of
https://github.com/wallabag/wallabag.git
synced 2024-11-23 09:31:04 +00:00
Use pager in getEntries() and return Hateoas collection
This commit is contained in:
parent
bcf53ab75b
commit
6e22bd737b
1 changed files with 16 additions and 5 deletions
|
@ -9,6 +9,8 @@ use Symfony\Component\HttpFoundation\Response;
|
||||||
use Wallabag\CoreBundle\Entity\Entry;
|
use Wallabag\CoreBundle\Entity\Entry;
|
||||||
use Wallabag\CoreBundle\Entity\Tag;
|
use Wallabag\CoreBundle\Entity\Tag;
|
||||||
use Wallabag\CoreBundle\Service\Extractor;
|
use Wallabag\CoreBundle\Service\Extractor;
|
||||||
|
use Hateoas\Configuration\Route;
|
||||||
|
use Hateoas\Representation\Factory\PagerfantaFactory;
|
||||||
|
|
||||||
class WallabagRestController extends Controller
|
class WallabagRestController extends Controller
|
||||||
{
|
{
|
||||||
|
@ -82,20 +84,29 @@ class WallabagRestController extends Controller
|
||||||
$isStarred = $request->query->get('star');
|
$isStarred = $request->query->get('star');
|
||||||
$sort = $request->query->get('sort', 'created');
|
$sort = $request->query->get('sort', 'created');
|
||||||
$order = $request->query->get('order', 'desc');
|
$order = $request->query->get('order', 'desc');
|
||||||
$page = $request->query->get('page', 1);
|
$page = (int) $request->query->get('page', 1);
|
||||||
$perPage = $request->query->get('perPage', 30);
|
$perPage = (int) $request->query->get('perPage', 30);
|
||||||
$tags = $request->query->get('tags', array());
|
$tags = $request->query->get('tags', array());
|
||||||
|
|
||||||
$entries = $this
|
$pager = $this
|
||||||
->getDoctrine()
|
->getDoctrine()
|
||||||
->getRepository('WallabagCoreBundle:Entry')
|
->getRepository('WallabagCoreBundle:Entry')
|
||||||
->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order);
|
->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order);
|
||||||
|
|
||||||
if (!$entries) {
|
if (0 === $pager->getNbResults()) {
|
||||||
throw $this->createNotFoundException();
|
throw $this->createNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
$json = $this->get('serializer')->serialize($entries, 'json');
|
$pager->setCurrentPage($page);
|
||||||
|
$pager->setMaxPerPage($perPage);
|
||||||
|
|
||||||
|
$pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
|
||||||
|
$paginatedCollection = $pagerfantaFactory->createRepresentation(
|
||||||
|
$pager,
|
||||||
|
new Route('api_get_entries', [], $absolute = true)
|
||||||
|
);
|
||||||
|
|
||||||
|
$json = $this->get('serializer')->serialize($paginatedCollection, 'json');
|
||||||
|
|
||||||
return new Response($json, 200, array('application/json'));
|
return new Response($json, 200, array('application/json'));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue