diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/SearchRestController.php | 91 | ||||
-rw-r--r-- | src/Wallabag/ApiBundle/Resources/config/routing_rest.yml | 5 |
2 files changed, 96 insertions, 0 deletions
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 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\ApiBundle\Controller; | ||
4 | |||
5 | use Hateoas\Configuration\Route; | ||
6 | use Hateoas\Representation\Factory\PagerfantaFactory; | ||
7 | use JMS\Serializer\SerializationContext; | ||
8 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | ||
9 | use Pagerfanta\Adapter\DoctrineORMAdapter; | ||
10 | use Pagerfanta\Pagerfanta; | ||
11 | use Symfony\Component\HttpFoundation\JsonResponse; | ||
12 | use Symfony\Component\HttpFoundation\Request; | ||
13 | use Symfony\Component\HttpFoundation\Response; | ||
14 | use Symfony\Component\HttpKernel\Exception\HttpException; | ||
15 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
16 | use Wallabag\CoreBundle\Entity\Entry; | ||
17 | use Wallabag\CoreBundle\Entity\Tag; | ||
18 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; | ||
19 | use Wallabag\CoreBundle\Event\EntrySavedEvent; | ||
20 | |||
21 | class SearchRestController extends WallabagRestController | ||
22 | { | ||
23 | /** | ||
24 | * Search all entries by term. | ||
25 | * | ||
26 | * @ApiDoc( | ||
27 | * parameters={ | ||
28 | * {"name"="term", "dataType"="string", "required"=false, "format"="any", "description"="Any query term"}, | ||
29 | * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, | ||
30 | * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."} | ||
31 | * } | ||
32 | * ) | ||
33 | * | ||
34 | * @return JsonResponse | ||
35 | */ | ||
36 | public function getSearchAction(Request $request) | ||
37 | { | ||
38 | $this->validateAuthentication(); | ||
39 | |||
40 | $term = $request->query->get('term'); | ||
41 | $page = (int) $request->query->get('page', 1); | ||
42 | $perPage = (int) $request->query->get('perPage', 30); | ||
43 | |||
44 | $qb = $this->get('wallabag_core.entry_repository') | ||
45 | ->getBuilderForSearchByUser( | ||
46 | $this->getUser()->getId(), | ||
47 | $term, | ||
48 | null | ||
49 | ); | ||
50 | |||
51 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); | ||
52 | $pager = new Pagerfanta($pagerAdapter); | ||
53 | |||
54 | $pager->setMaxPerPage($perPage); | ||
55 | $pager->setCurrentPage($page); | ||
56 | |||
57 | $pagerfantaFactory = new PagerfantaFactory('page', 'perPage'); | ||
58 | $paginatedCollection = $pagerfantaFactory->createRepresentation( | ||
59 | $pager, | ||
60 | new Route( | ||
61 | 'api_get_search', | ||
62 | [ | ||
63 | 'term' => $term, | ||
64 | 'page' => $page, | ||
65 | 'perPage' => $perPage, | ||
66 | ], | ||
67 | UrlGeneratorInterface::ABSOLUTE_URL | ||
68 | ) | ||
69 | ); | ||
70 | |||
71 | return $this->sendResponse($paginatedCollection); | ||
72 | } | ||
73 | |||
74 | /** | ||
75 | * Shortcut to send data serialized in json. | ||
76 | * | ||
77 | * @param mixed $data | ||
78 | * | ||
79 | * @return JsonResponse | ||
80 | */ | ||
81 | private function sendResponse($data) | ||
82 | { | ||
83 | // https://github.com/schmittjoh/JMSSerializerBundle/issues/293 | ||
84 | $context = new SerializationContext(); | ||
85 | $context->setSerializeNull(true); | ||
86 | |||
87 | $json = $this->get('jms_serializer')->serialize($data, 'json', $context); | ||
88 | |||
89 | return (new JsonResponse())->setJson($json); | ||
90 | } | ||
91 | } | ||
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: | |||
3 | resource: "WallabagApiBundle:EntryRest" | 3 | resource: "WallabagApiBundle:EntryRest" |
4 | name_prefix: api_ | 4 | name_prefix: api_ |
5 | 5 | ||
6 | search: | ||
7 | type: rest | ||
8 | resource: "WallabagApiBundle:SearchRest" | ||
9 | name_prefix: api_ | ||
10 | |||
6 | tag: | 11 | tag: |
7 | type: rest | 12 | type: rest |
8 | resource: "WallabagApiBundle:TagRest" | 13 | resource: "WallabagApiBundle:TagRest" |