]>
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; | |
b3205798 CR |
7 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
8 | use Pagerfanta\Adapter\DoctrineORMAdapter; | |
9 | use Pagerfanta\Pagerfanta; | |
10 | use Symfony\Component\HttpFoundation\JsonResponse; | |
11 | use Symfony\Component\HttpFoundation\Request; | |
b3205798 CR |
12 | |
13 | class 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 | ], | |
80104254 | 59 | true |
b3205798 CR |
60 | ) |
61 | ); | |
62 | ||
63 | return $this->sendResponse($paginatedCollection); | |
64 | } | |
b3205798 | 65 | } |