From b32057980e33e7ddd93480017496a589006b8260 Mon Sep 17 00:00:00 2001 From: Craig Roberts Date: Mon, 9 Apr 2018 17:24:45 +0800 Subject: Fixes [wallabag/wallabag#2611] Add a basic Search REST endpoint - Adds a new `search` key to `src/Wallabag/ApiBundle/Resources/config/routing_rest.yml` - Reuses the `getBuilderForSearchByUser` method from the EntryRepository - Supports, `term`, `page`, and `perPage` query parameters - Some very basic tests --- .../ApiBundle/Controller/SearchRestController.php | 91 ++++++++++++++++++++++ .../ApiBundle/Resources/config/routing_rest.yml | 5 ++ 2 files changed, 96 insertions(+) create mode 100644 src/Wallabag/ApiBundle/Controller/SearchRestController.php (limited to 'src/Wallabag/ApiBundle') diff --git a/src/Wallabag/ApiBundle/Controller/SearchRestController.php b/src/Wallabag/ApiBundle/Controller/SearchRestController.php new file mode 100644 index 00000000..c0b2cb24 --- /dev/null +++ b/src/Wallabag/ApiBundle/Controller/SearchRestController.php @@ -0,0 +1,91 @@ +validateAuthentication(); + + $term = $request->query->get('term'); + $page = (int) $request->query->get('page', 1); + $perPage = (int) $request->query->get('perPage', 30); + + $qb = $this->get('wallabag_core.entry_repository') + ->getBuilderForSearchByUser( + $this->getUser()->getId(), + $term, + null + ); + + $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); + $pager = new Pagerfanta($pagerAdapter); + + $pager->setMaxPerPage($perPage); + $pager->setCurrentPage($page); + + $pagerfantaFactory = new PagerfantaFactory('page', 'perPage'); + $paginatedCollection = $pagerfantaFactory->createRepresentation( + $pager, + new Route( + 'api_get_search', + [ + 'term' => $term, + 'page' => $page, + 'perPage' => $perPage, + ], + UrlGeneratorInterface::ABSOLUTE_URL + ) + ); + + return $this->sendResponse($paginatedCollection); + } + + /** + * Shortcut to send data serialized in json. + * + * @param mixed $data + * + * @return JsonResponse + */ + private function sendResponse($data) + { + // https://github.com/schmittjoh/JMSSerializerBundle/issues/293 + $context = new SerializationContext(); + $context->setSerializeNull(true); + + $json = $this->get('jms_serializer')->serialize($data, 'json', $context); + + return (new JsonResponse())->setJson($json); + } +} diff --git a/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml b/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml index c0283e71..06e62c37 100644 --- a/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml +++ b/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml @@ -3,6 +3,11 @@ entry: resource: "WallabagApiBundle:EntryRest" name_prefix: api_ +search: + type: rest + resource: "WallabagApiBundle:SearchRest" + name_prefix: api_ + tag: type: rest resource: "WallabagApiBundle:TagRest" -- cgit v1.2.3 From 9133bd02d11c37c98b2c7c979e363cc7bff8f914 Mon Sep 17 00:00:00 2001 From: Craig Roberts Date: Tue, 10 Apr 2018 19:50:26 +0800 Subject: [wallabag/wallabag#2611] Fix PHPCS lint errors --- src/Wallabag/ApiBundle/Controller/SearchRestController.php | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src/Wallabag/ApiBundle') diff --git a/src/Wallabag/ApiBundle/Controller/SearchRestController.php b/src/Wallabag/ApiBundle/Controller/SearchRestController.php index c0b2cb24..6620107d 100644 --- a/src/Wallabag/ApiBundle/Controller/SearchRestController.php +++ b/src/Wallabag/ApiBundle/Controller/SearchRestController.php @@ -10,13 +10,7 @@ use Pagerfanta\Adapter\DoctrineORMAdapter; use Pagerfanta\Pagerfanta; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -use Wallabag\CoreBundle\Entity\Entry; -use Wallabag\CoreBundle\Entity\Tag; -use Wallabag\CoreBundle\Event\EntryDeletedEvent; -use Wallabag\CoreBundle\Event\EntrySavedEvent; class SearchRestController extends WallabagRestController { -- cgit v1.2.3 From 019e1acc4962229a538421b6f2b0643d03c1d72c Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 24 Oct 2018 20:11:45 +0200 Subject: Factorize sendResponse between Api controllers And run newer cs fixer --- .../ApiBundle/Controller/EntryRestController.php | 19 ------------------- .../ApiBundle/Controller/SearchRestController.php | 19 ------------------- .../ApiBundle/Controller/WallabagRestController.php | 19 +++++++++++++++++++ 3 files changed, 19 insertions(+), 38 deletions(-) (limited to 'src/Wallabag/ApiBundle') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index f792aaf2..a79e852c 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -4,7 +4,6 @@ namespace Wallabag\ApiBundle\Controller; use Hateoas\Configuration\Route; use Hateoas\Representation\Factory\PagerfantaFactory; -use JMS\Serializer\SerializationContext; use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; @@ -773,24 +772,6 @@ class EntryRestController extends WallabagRestController return $this->sendResponse($results); } - /** - * Shortcut to send data serialized in json. - * - * @param mixed $data - * - * @return JsonResponse - */ - private function sendResponse($data) - { - // https://github.com/schmittjoh/JMSSerializerBundle/issues/293 - $context = new SerializationContext(); - $context->setSerializeNull(true); - - $json = $this->get('jms_serializer')->serialize($data, 'json', $context); - - return (new JsonResponse())->setJson($json); - } - /** * Retrieve value from the request. * Used for POST & PATCH on a an entry. diff --git a/src/Wallabag/ApiBundle/Controller/SearchRestController.php b/src/Wallabag/ApiBundle/Controller/SearchRestController.php index 6620107d..f8da0ad4 100644 --- a/src/Wallabag/ApiBundle/Controller/SearchRestController.php +++ b/src/Wallabag/ApiBundle/Controller/SearchRestController.php @@ -4,7 +4,6 @@ namespace Wallabag\ApiBundle\Controller; use Hateoas\Configuration\Route; use Hateoas\Representation\Factory\PagerfantaFactory; -use JMS\Serializer\SerializationContext; use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Pagerfanta\Adapter\DoctrineORMAdapter; use Pagerfanta\Pagerfanta; @@ -64,22 +63,4 @@ class SearchRestController extends WallabagRestController return $this->sendResponse($paginatedCollection); } - - /** - * Shortcut to send data serialized in json. - * - * @param mixed $data - * - * @return JsonResponse - */ - private function sendResponse($data) - { - // https://github.com/schmittjoh/JMSSerializerBundle/issues/293 - $context = new SerializationContext(); - $context->setSerializeNull(true); - - $json = $this->get('jms_serializer')->serialize($data, 'json', $context); - - return (new JsonResponse())->setJson($json); - } } diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index 3c7ad0cf..f18b0910 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php @@ -3,6 +3,7 @@ namespace Wallabag\ApiBundle\Controller; use FOS\RestBundle\Controller\FOSRestController; +use JMS\Serializer\SerializationContext; use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Security\Core\Exception\AccessDeniedException; @@ -64,4 +65,22 @@ class WallabagRestController extends FOSRestController throw $this->createAccessDeniedException('Access forbidden. Entry user id: ' . $requestUserId . ', logged user id: ' . $user->getId()); } } + + /** + * Shortcut to send data serialized in json. + * + * @param mixed $data + * + * @return JsonResponse + */ + protected function sendResponse($data) + { + // https://github.com/schmittjoh/JMSSerializerBundle/issues/293 + $context = new SerializationContext(); + $context->setSerializeNull(true); + + $json = $this->get('jms_serializer')->serialize($data, 'json', $context); + + return (new JsonResponse())->setJson($json); + } } -- cgit v1.2.3 From 801042544444d58580d87d04d5602797027153fc Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 24 Oct 2018 20:29:33 +0200 Subject: Fix third argument to Route --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 3 +-- src/Wallabag/ApiBundle/Controller/SearchRestController.php | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) (limited to 'src/Wallabag/ApiBundle') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index a79e852c..16d8a40b 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -10,7 +10,6 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\HttpException; -use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Entity\Tag; use Wallabag\CoreBundle\Event\EntryDeletedEvent; @@ -140,7 +139,7 @@ class EntryRestController extends WallabagRestController 'tags' => $tags, 'since' => $since, ], - UrlGeneratorInterface::ABSOLUTE_URL + true ) ); diff --git a/src/Wallabag/ApiBundle/Controller/SearchRestController.php b/src/Wallabag/ApiBundle/Controller/SearchRestController.php index f8da0ad4..d9f99844 100644 --- a/src/Wallabag/ApiBundle/Controller/SearchRestController.php +++ b/src/Wallabag/ApiBundle/Controller/SearchRestController.php @@ -9,7 +9,6 @@ use Pagerfanta\Adapter\DoctrineORMAdapter; use Pagerfanta\Pagerfanta; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\Routing\Generator\UrlGeneratorInterface; class SearchRestController extends WallabagRestController { @@ -57,7 +56,7 @@ class SearchRestController extends WallabagRestController 'page' => $page, 'perPage' => $perPage, ], - UrlGeneratorInterface::ABSOLUTE_URL + true ) ); -- cgit v1.2.3