From 02ad66c2bf30206d6e834e2624ddc1d910d98d1f Mon Sep 17 00:00:00 2001 From: Maciej Rymarz <59456825+mako321@users.noreply.github.com> Date: Thu, 24 Aug 2023 17:12:37 +0200 Subject: [PATCH] Add per_page to questionnaire pagination (#51) --- .../QuestionnaireAdminApiContract.php | 42 +++++++++++++++++++ .../QuestionnaireAdminApiController.php | 4 +- .../QuestionnaireServiceContract.php | 2 +- src/Services/QuestionnaireService.php | 4 +- tests/Api/QuestionnaireListTest.php | 39 +++++++++++++++++ 5 files changed, 86 insertions(+), 5 deletions(-) diff --git a/src/Http/Controllers/Contracts/QuestionnaireAdminApiContract.php b/src/Http/Controllers/Contracts/QuestionnaireAdminApiContract.php index 551b70c..0f3df8d 100644 --- a/src/Http/Controllers/Contracts/QuestionnaireAdminApiContract.php +++ b/src/Http/Controllers/Contracts/QuestionnaireAdminApiContract.php @@ -21,6 +21,48 @@ interface QuestionnaireAdminApiContract * security={ * {"passport": {}}, * }, + * @OA\Parameter( + * name="page", + * required=false, + * in="query", + * @OA\Schema( + * type="integer", + * ), + * ), + * @OA\Parameter( + * name="per_page", + * required=false, + * in="query", + * @OA\Schema( + * type="integer", + * ), + * ), + * @OA\Parameter( + * name="order_by", + * required=false, + * in="query", + * @OA\Schema( + * type="string", + * enum={"id", "created_at", "title"} + * ), + * ), + * @OA\Parameter( + * name="order", + * required=false, + * in="query", + * @OA\Schema( + * type="string", + * enum={"ASC", "DESC"} + * ), + * ), + * @OA\Parameter( + * name="title", + * required=false, + * in="query", + * @OA\Schema( + * type="string", + * ), + * ), * @OA\Response( * response=200, * description="list of available questionnaires", diff --git a/src/Http/Controllers/QuestionnaireAdminApiController.php b/src/Http/Controllers/QuestionnaireAdminApiController.php index f6b173d..3d17d56 100644 --- a/src/Http/Controllers/QuestionnaireAdminApiController.php +++ b/src/Http/Controllers/QuestionnaireAdminApiController.php @@ -46,12 +46,12 @@ public function __construct( public function list(QuestionnaireListingRequest $request): JsonResponse { - return $this->sendResponseForResource( QuestionnaireResource::collection( $this->questionnaireService->list( QuestionnairesFilterCriteriaDto::instantiateFromRequest($request)->toArray(), - OrderDto::instantiateFromRequest($request) + OrderDto::instantiateFromRequest($request), + $request->get('per_page') ?? 15 )), __("Questionnaire list retrieved successfully") ); diff --git a/src/Services/Contracts/QuestionnaireServiceContract.php b/src/Services/Contracts/QuestionnaireServiceContract.php index 6ee640e..8ee7d50 100644 --- a/src/Services/Contracts/QuestionnaireServiceContract.php +++ b/src/Services/Contracts/QuestionnaireServiceContract.php @@ -17,7 +17,7 @@ public function deleteQuestionnaire(Questionnaire $questionnaire): bool; public function searchForFront(array $filters, ?bool $publicAnswers = null): LengthAwarePaginator; - public function list(array $criteria, OrderDto $orderDto): LengthAwarePaginator; + public function list(array $criteria, OrderDto $orderDto, int $perPage = 15): LengthAwarePaginator; public function findForFront(array $filters, User $user): ?array; diff --git a/src/Services/QuestionnaireService.php b/src/Services/QuestionnaireService.php index ff37002..fbb4ab0 100644 --- a/src/Services/QuestionnaireService.php +++ b/src/Services/QuestionnaireService.php @@ -70,14 +70,14 @@ public function searchForFront(array $filters, ?bool $publicAnswers = null): Len ); } - public function list(array $criteria, OrderDto $orderDto): LengthAwarePaginator + public function list(array $criteria, OrderDto $orderDto, int $perPage = 15): LengthAwarePaginator { $query = $this->questionnaireRepository->queryWithAppliedCriteria($criteria); if ($orderDto->getOrderBy()) { $query->orderBy($orderDto->getOrderBy(), $orderDto->getOrder() ?? 'asc'); } - return $query->paginate(); + return $query->paginate($perPage); } public function findForFront(array $filters, User $user): ?array diff --git a/tests/Api/QuestionnaireListTest.php b/tests/Api/QuestionnaireListTest.php index 6ea3f4e..35f6a21 100644 --- a/tests/Api/QuestionnaireListTest.php +++ b/tests/Api/QuestionnaireListTest.php @@ -171,6 +171,45 @@ public function testAdminCanListQuestionnaireWithFiltersAndSorts(): void $this->assertTrue($response->json('data.1.id') === $questionnaireOne->getKey()); } + public function testQuestionnaireListPagination(): void + { + $this->authenticateAsAdmin(); + + Questionnaire::factory() + ->count(25) + ->create(); + + $this->actingAs($this->user, 'api') + ->getJson('api/admin/questionnaire') + ->assertOk() + ->assertJsonCount(15, 'data') + ->assertJson([ + 'meta' => [ + 'total' => 25, + ], + ]); + + $this->actingAs($this->user, 'api') + ->getJson('api/admin/questionnaire?per_page=10') + ->assertOk() + ->assertJsonCount(10, 'data') + ->assertJson([ + 'meta' => [ + 'total' => 25, + ], + ]); + + $this->actingAs($this->user, 'api') + ->getJson('api/admin/questionnaire?per_page=10&page=3') + ->assertOk() + ->assertJsonCount(5, 'data') + ->assertJson([ + 'meta' => [ + 'total' => 25, + ], + ]); + } + public function testAnonymousCanListEmptyQuestionnaire(): void { $questionnaireModel = QuestionnaireModel::factory()->createOne();