]>
Commit | Line | Data |
---|---|---|
b3205798 CR |
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 | } |