3 namespace Wallabag\ApiBundle\Controller
;
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
;
21 class SearchRestController
extends WallabagRestController
24 * Search all entries by term.
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."}
34 * @return JsonResponse
36 public function getSearchAction(Request
$request)
38 $this->validateAuthentication();
40 $term = $request->query
->get('term');
41 $page = (int) $request->query
->get('page', 1);
42 $perPage = (int) $request->query
->get('perPage', 30);
44 $qb = $this->get('wallabag_core.entry_repository')
45 ->getBuilderForSearchByUser(
46 $this->getUser()->getId(),
51 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
52 $pager = new Pagerfanta($pagerAdapter);
54 $pager->setMaxPerPage($perPage);
55 $pager->setCurrentPage($page);
57 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
58 $paginatedCollection = $pagerfantaFactory->createRepresentation(
65 'perPage' => $perPage,
67 UrlGeneratorInterface
::ABSOLUTE_URL
71 return $this->sendResponse($paginatedCollection);
75 * Shortcut to send data serialized in json.
79 * @return JsonResponse
81 private function sendResponse($data)
83 // https://github.com/schmittjoh/JMSSerializerBundle/issues/293
84 $context = new SerializationContext();
85 $context->setSerializeNull(true);
87 $json = $this->get('jms_serializer')->serialize($data, 'json', $context);
89 return (new JsonResponse())->setJson($json);