Skip to content

Commit

Permalink
Support for json:api pagination (#426)
Browse files Browse the repository at this point in the history
* Support for json:api pagination

* Fix styling

* fix psalm

* Fix styling

Co-authored-by: binaryk <[email protected]>
  • Loading branch information
binaryk and binaryk authored Nov 12, 2021
1 parent 0b60c6a commit d4996d9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/Filters/PaginationDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Binaryk\LaravelRestify\Filters;

use Spatie\DataTransferObject\DataTransferObject;

class PaginationDto extends DataTransferObject
{
public int|string|null $perPage;

public int|string|null $page;
}
17 changes: 17 additions & 0 deletions src/Http/Requests/RestifyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Binaryk\LaravelRestify\Http\Requests;

use Binaryk\LaravelRestify\Fields\EagerField;
use Binaryk\LaravelRestify\Filters\PaginationDto;
use Binaryk\LaravelRestify\Http\Requests\Concerns\DetermineRequestType;
use Binaryk\LaravelRestify\Http\Requests\Concerns\InteractWithRepositories;
use Illuminate\Foundation\Http\FormRequest;
Expand Down Expand Up @@ -42,4 +43,20 @@ public function relatedEagerField(): EagerField

return $eagerField;
}

public function pagination(): PaginationDto
{
$perPage = ($this->input('page.size') ?? $this->input('perPage'));

if (is_array($this->input('page'))) {
$pageNumber = $this->input('page.number');
} else {
$pageNumber = $this->input('page');
}

return new PaginationDto(
perPage: $perPage,
page: $pageNumber,
);
}
}
2 changes: 1 addition & 1 deletion src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ public function index(RestifyRequest $request)
* @var AbstractPaginator $paginator
*/
$paginator = RepositorySearchService::instance()->search($request, $this)
->paginate($request->query('perPage') ?? static::$defaultPerPage);
->paginate($request->pagination()->perPage ?? static::$defaultPerPage, page: $request->pagination()->page);

$items = $this->indexCollection($request, $paginator->getCollection())->map(function ($value) {
return static::resolveWith($value);
Expand Down
5 changes: 0 additions & 5 deletions src/Services/LoginService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Binaryk\LaravelRestify\Events\UserLoggedIn;
use Binaryk\LaravelRestify\Services\Concerns\AuthenticatesUsers;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Http\Request;

class LoginService
Expand All @@ -31,10 +30,6 @@ public function login(Request $request)
}

if ($this->attemptLogin($request)) {
if (($user = $this->guard()->user()) instanceof MustVerifyEmail && ! $user->hasVerifiedEmail()) {
return abort(401, 'User must verify email.');
}

event(new UserLoggedIn($this->guard()->user()));

return $this->guard()->user()->createToken('login');
Expand Down
17 changes: 17 additions & 0 deletions tests/Controllers/Index/RepositoryIndexControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public function it_can_paginate(): void
->etc()
);

$this->getJson(PostRepository::to(null, [
'page[size]' => 10,
]))->assertJson(
fn (AssertableJson $json) => $json
->count('data', 10)
->etc()
);

$this->getJson(PostRepository::to(null, [
'perPage' => 10,
'page' => '2',
Expand All @@ -51,6 +59,15 @@ public function it_can_paginate(): void
->count('data', 5)
->etc()
);

$this->getJson(PostRepository::to(null, [
'page[size]' => 10,
'page[number]' => 2,
]))->assertJson(
fn (AssertableJson $json) => $json
->count('data', 5)
->etc()
);
}

/** * @test */
Expand Down

0 comments on commit d4996d9

Please sign in to comment.