aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ApiBundle/Controller
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller')
-rw-r--r--src/Wallabag/ApiBundle/Controller/DeveloperController.php2
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php28
-rw-r--r--src/Wallabag/ApiBundle/Controller/SearchRestController.php65
-rw-r--r--src/Wallabag/ApiBundle/Controller/WallabagRestController.php39
4 files changed, 109 insertions, 25 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/DeveloperController.php b/src/Wallabag/ApiBundle/Controller/DeveloperController.php
index c7178017..ae7e83da 100644
--- a/src/Wallabag/ApiBundle/Controller/DeveloperController.php
+++ b/src/Wallabag/ApiBundle/Controller/DeveloperController.php
@@ -2,9 +2,9 @@
2 2
3namespace Wallabag\ApiBundle\Controller; 3namespace Wallabag\ApiBundle\Controller;
4 4
5use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6use Symfony\Bundle\FrameworkBundle\Controller\Controller; 5use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7use Symfony\Component\HttpFoundation\Request; 6use Symfony\Component\HttpFoundation\Request;
7use Symfony\Component\Routing\Annotation\Route;
8use Wallabag\ApiBundle\Entity\Client; 8use Wallabag\ApiBundle\Entity\Client;
9use Wallabag\ApiBundle\Form\Type\ClientType; 9use Wallabag\ApiBundle\Form\Type\ClientType;
10 10
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index 33b75665..5c850091 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -4,14 +4,12 @@ namespace Wallabag\ApiBundle\Controller;
4 4
5use Hateoas\Configuration\Route; 5use Hateoas\Configuration\Route;
6use Hateoas\Representation\Factory\PagerfantaFactory; 6use Hateoas\Representation\Factory\PagerfantaFactory;
7use JMS\Serializer\SerializationContext;
8use Nelmio\ApiDocBundle\Annotation\ApiDoc; 7use Nelmio\ApiDocBundle\Annotation\ApiDoc;
9use Symfony\Component\HttpFoundation\JsonResponse; 8use Symfony\Component\HttpFoundation\JsonResponse;
10use Symfony\Component\HttpFoundation\Request; 9use Symfony\Component\HttpFoundation\Request;
11use Symfony\Component\HttpFoundation\Response; 10use Symfony\Component\HttpFoundation\Response;
12use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; 11use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
13use Symfony\Component\HttpKernel\Exception\HttpException; 12use Symfony\Component\HttpKernel\Exception\HttpException;
14use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
15use Wallabag\CoreBundle\Entity\Entry; 13use Wallabag\CoreBundle\Entity\Entry;
16use Wallabag\CoreBundle\Entity\Tag; 14use Wallabag\CoreBundle\Entity\Tag;
17use Wallabag\CoreBundle\Event\EntryDeletedEvent; 15use Wallabag\CoreBundle\Event\EntryDeletedEvent;
@@ -80,7 +78,7 @@ class EntryRestController extends WallabagRestController
80 * parameters={ 78 * parameters={
81 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."}, 79 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."},
82 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."}, 80 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."},
83 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."}, 81 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated' or 'archived', default 'created'", "description"="sort entries by date."},
84 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."}, 82 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
85 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, 83 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
86 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}, 84 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
@@ -141,7 +139,7 @@ class EntryRestController extends WallabagRestController
141 'tags' => $tags, 139 'tags' => $tags,
142 'since' => $since, 140 'since' => $since,
143 ], 141 ],
144 UrlGeneratorInterface::ABSOLUTE_URL 142 true
145 ) 143 )
146 ); 144 );
147 145
@@ -363,7 +361,7 @@ class EntryRestController extends WallabagRestController
363 } 361 }
364 362
365 if (null !== $data['isArchived']) { 363 if (null !== $data['isArchived']) {
366 $entry->setArchived((bool) $data['isArchived']); 364 $entry->updateArchived((bool) $data['isArchived']);
367 } 365 }
368 366
369 if (null !== $data['isStarred']) { 367 if (null !== $data['isStarred']) {
@@ -479,7 +477,7 @@ class EntryRestController extends WallabagRestController
479 } 477 }
480 478
481 if (null !== $data['isArchived']) { 479 if (null !== $data['isArchived']) {
482 $entry->setArchived((bool) $data['isArchived']); 480 $entry->updateArchived((bool) $data['isArchived']);
483 } 481 }
484 482
485 if (null !== $data['isStarred']) { 483 if (null !== $data['isStarred']) {
@@ -787,24 +785,6 @@ class EntryRestController extends WallabagRestController
787 } 785 }
788 786
789 /** 787 /**
790 * Shortcut to send data serialized in json.
791 *
792 * @param mixed $data
793 *
794 * @return JsonResponse
795 */
796 private function sendResponse($data)
797 {
798 // https://github.com/schmittjoh/JMSSerializerBundle/issues/293
799 $context = new SerializationContext();
800 $context->setSerializeNull(true);
801
802 $json = $this->get('jms_serializer')->serialize($data, 'json', $context);
803
804 return (new JsonResponse())->setJson($json);
805 }
806
807 /**
808 * Retrieve value from the request. 788 * Retrieve value from the request.
809 * Used for POST & PATCH on a an entry. 789 * Used for POST & PATCH on a an entry.
810 * 790 *
diff --git a/src/Wallabag/ApiBundle/Controller/SearchRestController.php b/src/Wallabag/ApiBundle/Controller/SearchRestController.php
new file mode 100644
index 00000000..d9f99844
--- /dev/null
+++ b/src/Wallabag/ApiBundle/Controller/SearchRestController.php
@@ -0,0 +1,65 @@
1<?php
2
3namespace Wallabag\ApiBundle\Controller;
4
5use Hateoas\Configuration\Route;
6use Hateoas\Representation\Factory\PagerfantaFactory;
7use Nelmio\ApiDocBundle\Annotation\ApiDoc;
8use Pagerfanta\Adapter\DoctrineORMAdapter;
9use Pagerfanta\Pagerfanta;
10use Symfony\Component\HttpFoundation\JsonResponse;
11use Symfony\Component\HttpFoundation\Request;
12
13class SearchRestController extends WallabagRestController
14{
15 /**
16 * Search all entries by term.
17 *
18 * @ApiDoc(
19 * parameters={
20 * {"name"="term", "dataType"="string", "required"=false, "format"="any", "description"="Any query term"},
21 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
22 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}
23 * }
24 * )
25 *
26 * @return JsonResponse
27 */
28 public function getSearchAction(Request $request)
29 {
30 $this->validateAuthentication();
31
32 $term = $request->query->get('term');
33 $page = (int) $request->query->get('page', 1);
34 $perPage = (int) $request->query->get('perPage', 30);
35
36 $qb = $this->get('wallabag_core.entry_repository')
37 ->getBuilderForSearchByUser(
38 $this->getUser()->getId(),
39 $term,
40 null
41 );
42
43 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
44 $pager = new Pagerfanta($pagerAdapter);
45
46 $pager->setMaxPerPage($perPage);
47 $pager->setCurrentPage($page);
48
49 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
50 $paginatedCollection = $pagerfantaFactory->createRepresentation(
51 $pager,
52 new Route(
53 'api_get_search',
54 [
55 'term' => $term,
56 'page' => $page,
57 'perPage' => $perPage,
58 ],
59 true
60 )
61 );
62
63 return $this->sendResponse($paginatedCollection);
64 }
65}
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
index 7d8cfbba..f18b0910 100644
--- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
@@ -3,6 +3,7 @@
3namespace Wallabag\ApiBundle\Controller; 3namespace Wallabag\ApiBundle\Controller;
4 4
5use FOS\RestBundle\Controller\FOSRestController; 5use FOS\RestBundle\Controller\FOSRestController;
6use JMS\Serializer\SerializationContext;
6use Nelmio\ApiDocBundle\Annotation\ApiDoc; 7use Nelmio\ApiDocBundle\Annotation\ApiDoc;
7use Symfony\Component\HttpFoundation\JsonResponse; 8use Symfony\Component\HttpFoundation\JsonResponse;
8use Symfony\Component\Security\Core\Exception\AccessDeniedException; 9use Symfony\Component\Security\Core\Exception\AccessDeniedException;
@@ -14,6 +15,8 @@ class WallabagRestController extends FOSRestController
14 * 15 *
15 * @ApiDoc() 16 * @ApiDoc()
16 * 17 *
18 * @deprecated Should use info endpoint instead
19 *
17 * @return JsonResponse 20 * @return JsonResponse
18 */ 21 */
19 public function getVersionAction() 22 public function getVersionAction()
@@ -24,6 +27,24 @@ class WallabagRestController extends FOSRestController
24 return (new JsonResponse())->setJson($json); 27 return (new JsonResponse())->setJson($json);
25 } 28 }
26 29
30 /**
31 * Retrieve information about the wallabag instance.
32 *
33 * @ApiDoc()
34 *
35 * @return JsonResponse
36 */
37 public function getInfoAction()
38 {
39 $info = [
40 'appname' => 'wallabag',
41 'version' => $this->container->getParameter('wallabag_core.version'),
42 'allowed_registration' => $this->container->getParameter('wallabag_user.registration_enabled'),
43 ];
44
45 return (new JsonResponse())->setJson($this->get('jms_serializer')->serialize($info, 'json'));
46 }
47
27 protected function validateAuthentication() 48 protected function validateAuthentication()
28 { 49 {
29 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) { 50 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
@@ -44,4 +65,22 @@ class WallabagRestController extends FOSRestController
44 throw $this->createAccessDeniedException('Access forbidden. Entry user id: ' . $requestUserId . ', logged user id: ' . $user->getId()); 65 throw $this->createAccessDeniedException('Access forbidden. Entry user id: ' . $requestUserId . ', logged user id: ' . $user->getId());
45 } 66 }
46 } 67 }
68
69 /**
70 * Shortcut to send data serialized in json.
71 *
72 * @param mixed $data
73 *
74 * @return JsonResponse
75 */
76 protected function sendResponse($data)
77 {
78 // https://github.com/schmittjoh/JMSSerializerBundle/issues/293
79 $context = new SerializationContext();
80 $context->setSerializeNull(true);
81
82 $json = $this->get('jms_serializer')->serialize($data, 'json', $context);
83
84 return (new JsonResponse())->setJson($json);
85 }
47} 86}