Skip to content

Commit 0225504

Browse files
Define default limit (#159)
* Define default limit * Fix styleCI * Add tests * Scout search * Fix styleCI * ♻️ moved default limit tests to separate part * ✅ style ci --------- Co-authored-by: Gautier DELEGLISE <[email protected]>
1 parent df42a83 commit 0225504

File tree

9 files changed

+65
-3
lines changed

9 files changed

+65
-3
lines changed

Diff for: src/Concerns/Resource/Paginable.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ trait Paginable
1717
*/
1818
public function paginate($query, RestRequest $request)
1919
{
20+
$defaultLimit = $this->defaultLimit ?? 50;
21+
2022
// In case we have a scout builder
2123
if ($query instanceof Builder) {
22-
return $query->paginate($request->input('search.limit', 50), 'page', $request->input('search.page', 1));
24+
return $query->paginate($request->input('search.limit', $defaultLimit), 'page', $request->input('search.page', 1));
2325
}
2426

25-
return $query->paginate($request->input('search.limit', 50), ['*'], 'page', $request->input('search.page', 1));
27+
return $query->paginate($request->input('search.limit', $defaultLimit), ['*'], 'page', $request->input('search.page', 1));
2628
}
2729
}

Diff for: src/Console/stubs/resource.stub

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ class {{ class }} extends RestResource
1313
*/
1414
public static $model = \{{ namespacedModel }}::class;
1515

16+
/**
17+
* The default value for the pagination limit.
18+
*
19+
* @var int
20+
*/
21+
public int $defaultLimit = 50;
22+
1623
/**
1724
* The exposed fields that could be provided
1825
* @param RestRequest $request

Diff for: src/Console/stubs/user-resource.stub

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ class UserResource extends RestResource
1313
*/
1414
public static $model = \App\User::class;
1515

16+
/**
17+
* The default value for the pagination limit.
18+
*
19+
* @var int
20+
*/
21+
public int $defaultLimit = 50;
22+
1623
/**
1724
* The exposed fields that could be provided
1825
* @param RestRequest $request

Diff for: src/Http/Resource.php

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ class Resource implements \JsonSerializable
4646
*/
4747
public static $response = Response::class;
4848

49+
/**
50+
* The default value for the pagination limit.
51+
*
52+
* @var int
53+
*/
54+
public int $defaultLimit = 50;
55+
4956
/**
5057
* Get a fresh instance of the model represented by the resource.
5158
*

Diff for: src/Query/ScoutBuilder.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public function search(array $parameters = [])
4949
$this->applyInstructions($parameters['instructions']);
5050
});
5151

52-
$this->queryBuilder->take($parameters['limit'] ?? 50);
52+
$defaultLimit = $this->resource->defaultLimit ?? 50;
53+
$this->queryBuilder->take($parameters['limit'] ?? $defaultLimit);
5354

5455
$this->queryBuilder
5556
->query(function (Builder $query) use ($parameters) {

Diff for: tests/Feature/Controllers/SearchPaginateOperationsTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,22 @@ public function test_getting_a_list_of_resources_paginating_with_many_records():
8282
new ModelResource()
8383
);
8484
}
85+
86+
public function test_to_get_a_list_of_paginated_resources_from_the_default_limit(): void
87+
{
88+
ModelFactory::new()->count(100)->create()->fresh();
89+
90+
Gate::policy(Model::class, GreenPolicy::class);
91+
92+
$response = $this->post(
93+
'/api/model-with-default-limit/search',
94+
[],
95+
['Accept' => 'application/json']
96+
);
97+
98+
$response->assertStatus(200);
99+
$response->assertJsonPath('per_page', 32);
100+
$response->assertJsonPath('last_page', 4);
101+
$response->assertJsonCount(32, 'data');
102+
}
85103
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Tests\Support\Http\Controllers;
4+
5+
use Lomkit\Rest\Http\Controllers\Controller;
6+
use Lomkit\Rest\Tests\Support\Rest\Resources\ModelWithDefaultLimitResource;
7+
8+
class ModelWithDefaultLimitController extends Controller
9+
{
10+
public static $resource = ModelWithDefaultLimitResource::class;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Tests\Support\Rest\Resources;
4+
5+
class ModelWithDefaultLimitResource extends ModelResource
6+
{
7+
public int $defaultLimit = 32;
8+
}

Diff for: tests/Support/Routes/api.php

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
\Lomkit\Rest\Facades\Rest::resource('searchable-models', \Lomkit\Rest\Tests\Support\Http\Controllers\SearchableModelController::class);
88
\Lomkit\Rest\Facades\Rest::resource('model-hooks', \Lomkit\Rest\Tests\Support\Http\Controllers\ModelHooksController::class)->withSoftDeletes();
99
\Lomkit\Rest\Facades\Rest::resource('model-withs', \Lomkit\Rest\Tests\Support\Http\Controllers\ModelWithController::class);
10+
\Lomkit\Rest\Facades\Rest::resource('model-with-default-limit', \Lomkit\Rest\Tests\Support\Http\Controllers\ModelWithDefaultLimitController::class);
1011
\Lomkit\Rest\Facades\Rest::resource('no-relationship-authorization-models', \Lomkit\Rest\Tests\Support\Http\Controllers\NoRelationshipAuthorizationModelController::class);
1112

1213
\Lomkit\Rest\Facades\Rest::resource('no-exposed-fields', \Lomkit\Rest\Tests\Support\Http\Controllers\NoExposedFieldsController::class);

0 commit comments

Comments
 (0)