Skip to content

Commit

Permalink
feature: install spatie/laravel-json-api-paginate
Browse files Browse the repository at this point in the history
  • Loading branch information
mascam97 committed Dec 3, 2023
1 parent 2949ec0 commit 3ea3730
Show file tree
Hide file tree
Showing 21 changed files with 248 additions and 29 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"rebing/graphql-laravel": "^9.1",
"spatie/laravel-activitylog": "^4.7",
"spatie/laravel-data": "^2.1",
"spatie/laravel-json-api-paginate": "^1.13",
"spatie/laravel-model-states": "^2.3",
"spatie/laravel-permission": "^5.9",
"spatie/laravel-query-builder": "5.1.2",
Expand Down
69 changes: 67 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions config/json-api-paginate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

return [

/*
* The maximum number of results that will be returned
* when using the JSON API paginator.
*/
'max_results' => 50,

/*
* The default number of results that will be returned
* when using the JSON API paginator.
*/
'default_size' => 20,

/*
* The key of the page[x] query string parameter for page number.
*/
'number_parameter' => 'number',

/*
* The key of the page[x] query string parameter for page size.
*/
'size_parameter' => 'size',

/*
* The key of the page[x] query string parameter for cursor.
*/
'cursor_parameter' => 'cursor',

/*
* The name of the macro that is added to the Eloquent query builder.
*/
'method_name' => 'jsonPaginate',

/*
* If you only need to display Next and Previous links, you may use
* simple pagination to perform a more efficient query.
*/
'use_simple_pagination' => false,

/*
* If you want to use cursor pagination, set this to true.
* This would override use_simple_pagination.
*/
'use_cursor_pagination' => false,

/*
* Here you can override the base url to be used in the link items.
*/
'base_url' => null,

/*
* The name of the query parameter used for pagination
*/
'pagination_parameter' => 'page',
];
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class PublicQuotesController extends Controller
*/
public function index(PublicQuoteIndexQuery $quoteQuery): AnonymousResourceCollection
{
$quotes = $quoteQuery->paginate();
$quotes = $quoteQuery
->jsonPaginate()
->withQueryString();

return PublicQuoteResource::collection($quotes);
}
Expand Down
1 change: 1 addition & 0 deletions src/App/Api/PublicQuotes/Queries/PublicQuoteIndexQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function __construct(Request $request)

$this->allowedFilters(['title', 'content', 'user_id'])
->allowedIncludes('user')
->defaultSort('created_at')
->allowedSorts('id', 'title', 'created_at');
}
}
4 changes: 3 additions & 1 deletion src/App/Api/Quotes/Controllers/QuoteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class QuoteController extends Controller
*/
public function index(IndexQuoteQuery $quoteQuery): AnonymousResourceCollection
{
$quotes = $quoteQuery->paginate();
$quotes = $quoteQuery
->jsonPaginate()
->withQueryString();

return QuoteResource::collection($quotes);
}
Expand Down
1 change: 1 addition & 0 deletions src/App/Api/Quotes/Queries/IndexQuoteQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function __construct(Request $request)
parent::__construct($query, $request);

$this->allowedFilters(['title', 'content', 'state'])
->defaultSort('created_at')
->allowedSorts('id', 'title', 'created_at');
}
}
4 changes: 3 additions & 1 deletion src/App/Api/Ratings/Controllers/RatingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class RatingController extends Controller
*/
public function index(RatingIndexQuery $quoteQuery): AnonymousResourceCollection
{
$quotes = $quoteQuery->paginate();
$quotes = $quoteQuery
->jsonPaginate()
->withQueryString();

return RatingResource::collection($quotes);
}
Expand Down
1 change: 1 addition & 0 deletions src/App/Api/Ratings/Queries/RatingIndexQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct(Request $request)

$this->allowedFilters(['qualifier_type', 'rateable_type'])
->allowedIncludes(['qualifier', 'rateable'])
->defaultSort('created_at')
->allowedSorts('id', 'created_at');
}
}
4 changes: 3 additions & 1 deletion src/App/Api/Users/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class UserController extends Controller
*/
public function index(UserIndexQuery $userQuery): AnonymousResourceCollection
{
$users = $userQuery->paginate();
$users = $userQuery
->jsonPaginate()
->withQueryString();

return UserResource::collection($users);
}
Expand Down
3 changes: 2 additions & 1 deletion src/App/Api/Users/Queries/UserIndexQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(Request $request)

$this->allowedFilters(['id', 'name'])
->allowedIncludes('quotes')
->allowedSorts('id', 'name');
->defaultSort('created_at')
->allowedSorts('id', 'name', 'created_at');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public function index(ActivityIndexQuery $activityQuery): AnonymousResourceColle
{
$this->authorize('viewAny', Activity::class);

$activities = $activityQuery->paginate();
$activities = $activityQuery
->jsonPaginate()
->withQueryString();

return ActivityResource::collection($activities);
}
Expand Down
4 changes: 3 additions & 1 deletion src/App/ApiAdmin/Users/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public function index(UserIndexQuery $userQuery): AnonymousResourceCollection
{
$this->authorize('viewAny', User::class);

$users = $userQuery->paginate();
$users = $userQuery
->jsonPaginate()
->withQueryString();

return UserResource::collection($users);
}
Expand Down
3 changes: 2 additions & 1 deletion src/App/ApiAdmin/Users/Queries/UserIndexQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct(Request $request)

$this->allowedFilters(['id', 'name', AllowedFilter::trashed()])
->allowedIncludes(['permissions', 'roles'])
->allowedSorts('id', 'name');
->defaultSort('created_at')
->allowedSorts('id', 'name', 'created_at');
}
}
3 changes: 2 additions & 1 deletion src/App/Web/Quotes/Queries/QuoteIndexQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct(Request $request)
parent::__construct($query, $request);

$this->allowedFilters(['title', 'content'])
->allowedSorts('title');
->defaultSort('created_at')
->allowedSorts('title', 'created_at');
}
}
Loading

0 comments on commit 3ea3730

Please sign in to comment.