wallabag/src/Wallabag/ApiBundle/Controller/SearchRestController.php
Casper Meijn 4f9c7a92a1 Update annotations to OpenApi 3
Most of the API annotations are directly converted. The changes in meaning are:
- Parameters "in body" is not supported anymore. These are changed to "in query" or to a request body (depending on the code).
2022-12-23 14:54:55 +01:00

98 lines
2.9 KiB
PHP

<?php
namespace Wallabag\ApiBundle\Controller;
use Hateoas\Configuration\Route as HateoasRoute;
use Hateoas\Representation\Factory\PagerfantaFactory;
use Nelmio\ApiDocBundle\Annotation\Operation;
use OpenApi\Annotations as OA;
use Pagerfanta\Doctrine\ORM\QueryAdapter as DoctrineORMAdapter;
use Pagerfanta\Pagerfanta;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Wallabag\CoreBundle\Repository\EntryRepository;
class SearchRestController extends WallabagRestController
{
/**
* Search all entries by term.
*
* @Operation(
* tags={"Search"},
* summary="Search all entries by term.",
* @OA\Parameter(
* name="term",
* in="query",
* description="Any query term",
* required=false,
* @OA\Schema(type="string")
* ),
* @OA\Parameter(
* name="page",
* in="query",
* description="what page you want.",
* required=false,
* @OA\Schema(
* type="integer",
* default=1
* )
* ),
* @OA\Parameter(
* name="perPage",
* in="query",
* description="results per page.",
* required=false,
* @OA\Schema(
* type="integer",
* default=30
* )
* ),
* @OA\Response(
* response="200",
* description="Returned when successful"
* )
* )
*
* @Route("/api/search.{_format}", methods={"GET"}, name="api_get_search", defaults={"_format": "json"})
*
* @return JsonResponse
*/
public function getSearchAction(Request $request, EntryRepository $entryRepository)
{
$this->validateAuthentication();
$term = $request->query->get('term');
$page = (int) $request->query->get('page', 1);
$perPage = (int) $request->query->get('perPage', 30);
$qb = $entryRepository->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 HateoasRoute(
'api_get_search',
[
'term' => $term,
'page' => $page,
'perPage' => $perPage,
],
true
)
);
return $this->sendResponse($paginatedCollection);
}
}