Skip to content

Commit

Permalink
Add per_page to questionnaire pagination (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
mako321 authored Aug 24, 2023
1 parent 1312be8 commit 02ad66c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
42 changes: 42 additions & 0 deletions src/Http/Controllers/Contracts/QuestionnaireAdminApiContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/QuestionnaireAdminApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")
);
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Contracts/QuestionnaireServiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/Services/QuestionnaireService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions tests/Api/QuestionnaireListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 02ad66c

Please sign in to comment.