2018-04-09 09:24:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\ApiBundle\Controller;
|
|
|
|
|
|
|
|
use Hateoas\Configuration\Route;
|
|
|
|
use Hateoas\Representation\Factory\PagerfantaFactory;
|
2022-11-06 12:00:41 +00:00
|
|
|
use Nelmio\ApiDocBundle\Annotation\Operation;
|
2020-07-29 04:36:43 +00:00
|
|
|
use Pagerfanta\Doctrine\ORM\QueryAdapter as DoctrineORMAdapter;
|
2018-04-09 09:24:45 +00:00
|
|
|
use Pagerfanta\Pagerfanta;
|
2022-11-06 12:00:41 +00:00
|
|
|
use Swagger\Annotations as SWG;
|
2018-04-09 09:24:45 +00:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2022-04-24 15:58:57 +00:00
|
|
|
use Wallabag\CoreBundle\Repository\EntryRepository;
|
2018-04-09 09:24:45 +00:00
|
|
|
|
|
|
|
class SearchRestController extends WallabagRestController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Search all entries by term.
|
|
|
|
*
|
2022-11-06 12:00:41 +00:00
|
|
|
* @Operation(
|
|
|
|
* tags={"Search"},
|
|
|
|
* summary="Search all entries by term.",
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* name="term",
|
|
|
|
* in="body",
|
|
|
|
* description="Any query term",
|
|
|
|
* required=false,
|
|
|
|
* @SWG\Schema(type="string")
|
|
|
|
* ),
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* name="page",
|
|
|
|
* in="body",
|
|
|
|
* description="what page you want.",
|
|
|
|
* required=false,
|
|
|
|
* @SWG\Schema(
|
|
|
|
* type="integer",
|
|
|
|
* default=1
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* name="perPage",
|
|
|
|
* in="body",
|
|
|
|
* description="results per page.",
|
|
|
|
* required=false,
|
|
|
|
* @SWG\Schema(
|
|
|
|
* type="integer",
|
|
|
|
* default=30
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="200",
|
|
|
|
* description="Returned when successful"
|
|
|
|
* )
|
2018-04-09 09:24:45 +00:00
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
public function getSearchAction(Request $request)
|
|
|
|
{
|
|
|
|
$this->validateAuthentication();
|
|
|
|
|
|
|
|
$term = $request->query->get('term');
|
|
|
|
$page = (int) $request->query->get('page', 1);
|
|
|
|
$perPage = (int) $request->query->get('perPage', 30);
|
|
|
|
|
2022-04-24 15:58:57 +00:00
|
|
|
$qb = $this->get(EntryRepository::class)
|
2018-04-09 09:24:45 +00:00
|
|
|
->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,
|
|
|
|
],
|
2018-10-24 18:29:33 +00:00
|
|
|
true
|
2018-04-09 09:24:45 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->sendResponse($paginatedCollection);
|
|
|
|
}
|
|
|
|
}
|