diff options
author | William Durand <will+git@drnd.me> | 2015-03-06 11:07:39 +0100 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2015-03-06 21:11:02 +0100 |
commit | 6e22bd737b2e543c85487864a9a25b0f2d5cc3f2 (patch) | |
tree | e6ea5fb4786e843b970b14d5a96da8c32548a315 /src/Wallabag | |
parent | bcf53ab75b2fbd0cc18faf2db61c81fa3d7c471d (diff) | |
download | wallabag-6e22bd737b2e543c85487864a9a25b0f2d5cc3f2.tar.gz wallabag-6e22bd737b2e543c85487864a9a25b0f2d5cc3f2.tar.zst wallabag-6e22bd737b2e543c85487864a9a25b0f2d5cc3f2.zip |
Use pager in getEntries() and return Hateoas collection
Diffstat (limited to 'src/Wallabag')
-rw-r--r-- | src/Wallabag/CoreBundle/Controller/WallabagRestController.php | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php index 3e25fe49..14f42c48 100644 --- a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php | |||
@@ -9,6 +9,8 @@ use Symfony\Component\HttpFoundation\Response; | |||
9 | use Wallabag\CoreBundle\Entity\Entry; | 9 | use Wallabag\CoreBundle\Entity\Entry; |
10 | use Wallabag\CoreBundle\Entity\Tag; | 10 | use Wallabag\CoreBundle\Entity\Tag; |
11 | use Wallabag\CoreBundle\Service\Extractor; | 11 | use Wallabag\CoreBundle\Service\Extractor; |
12 | use Hateoas\Configuration\Route; | ||
13 | use Hateoas\Representation\Factory\PagerfantaFactory; | ||
12 | 14 | ||
13 | class WallabagRestController extends Controller | 15 | class WallabagRestController extends Controller |
14 | { | 16 | { |
@@ -82,20 +84,29 @@ class WallabagRestController extends Controller | |||
82 | $isStarred = $request->query->get('star'); | 84 | $isStarred = $request->query->get('star'); |
83 | $sort = $request->query->get('sort', 'created'); | 85 | $sort = $request->query->get('sort', 'created'); |
84 | $order = $request->query->get('order', 'desc'); | 86 | $order = $request->query->get('order', 'desc'); |
85 | $page = $request->query->get('page', 1); | 87 | $page = (int) $request->query->get('page', 1); |
86 | $perPage = $request->query->get('perPage', 30); | 88 | $perPage = (int) $request->query->get('perPage', 30); |
87 | $tags = $request->query->get('tags', array()); | 89 | $tags = $request->query->get('tags', array()); |
88 | 90 | ||
89 | $entries = $this | 91 | $pager = $this |
90 | ->getDoctrine() | 92 | ->getDoctrine() |
91 | ->getRepository('WallabagCoreBundle:Entry') | 93 | ->getRepository('WallabagCoreBundle:Entry') |
92 | ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order); | 94 | ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order); |
93 | 95 | ||
94 | if (!$entries) { | 96 | if (0 === $pager->getNbResults()) { |
95 | throw $this->createNotFoundException(); | 97 | throw $this->createNotFoundException(); |
96 | } | 98 | } |
97 | 99 | ||
98 | $json = $this->get('serializer')->serialize($entries, 'json'); | 100 | $pager->setCurrentPage($page); |
101 | $pager->setMaxPerPage($perPage); | ||
102 | |||
103 | $pagerfantaFactory = new PagerfantaFactory('page', 'perPage'); | ||
104 | $paginatedCollection = $pagerfantaFactory->createRepresentation( | ||
105 | $pager, | ||
106 | new Route('api_get_entries', [], $absolute = true) | ||
107 | ); | ||
108 | |||
109 | $json = $this->get('serializer')->serialize($paginatedCollection, 'json'); | ||
99 | 110 | ||
100 | return new Response($json, 200, array('application/json')); | 111 | return new Response($json, 200, array('application/json')); |
101 | } | 112 | } |