Skip to content

A free open source IT asset/license management system

Added page number support in API, added per_page, total_pages, and current_page

snipe34a533b2d603
diff --git a/app/Http/Transformers/DatatablesTransformer.php b/app/Http/Transformers/DatatablesTransformer.php
index cd187c95d9..e33d34d1ff 100644
--- a/app/Http/Transformers/DatatablesTransformer.php
+++ b/app/Http/Transformers/DatatablesTransformer.php
@@ -11,6 +11,10 @@ class DatatablesTransformer
{
(isset($total)) ? $objects_array['total'] = $total : $objects_array['total'] = count($objects);
$objects_array['rows'] = $objects;
+ $objects_array['current_page'] = app('api_current_page');
+ $limit = app('api_limit_value');
+ $objects_array['per_page'] = $limit;
+ $objects_array['total_pages'] = $limit > 0 ? (int) ceil($objects_array['total'] / $limit) : 1;
return $objects_array;
}
diff --git a/app/Providers/SettingsServiceProvider.php b/app/Providers/SettingsServiceProvider.php
index 2d81b435f8..b68cade666 100644
--- a/app/Providers/SettingsServiceProvider.php
+++ b/app/Providers/SettingsServiceProvider.php
@@ -44,11 +44,28 @@ class SettingsServiceProvider extends ServiceProvider
return $limit;
});
- // Make sure the offset is actually set and is an integer
+ // Make sure the offset is actually set and is an integer.
+ // If 'page' is passed without 'offset', derive the offset from the page number.
app()->singleton('api_offset_value', function () {
- $offset = intval(request('offset'));
+ if (request()->filled('page') && ! request()->filled('offset')) {
+ $page = max(1, intval(request('page')));
+ return ($page - 1) * app('api_limit_value');
+ }
+
+ return intval(request('offset'));
+ });
+
+ // Resolve the current page number for inclusion in API list responses.
+ // Supports both page= and legacy offset= parameters.
+ app()->singleton('api_current_page', function () {
+ if (request()->filled('page') && ! request()->filled('offset')) {
+ return max(1, intval(request('page')));
+ }
+
+ $limit = app('api_limit_value');
+ $offset = app('api_offset_value');
- return $offset;
+ return $limit > 0 ? (int) floor($offset / $limit) + 1 : 1;
});
/**
diff --git a/tests/Feature/Assets/Api/AssetPaginationTest.php b/tests/Feature/Assets/Api/AssetPaginationTest.php
new file mode 100644
index 0000000000..12cdf91c5f
--- /dev/null
+++ b/tests/Feature/Assets/Api/AssetPaginationTest.php
@@ -0,0 +1,203 @@
+<?php
+
+namespace Tests\Feature\Assets\Api;
+
+use App\Models\Asset;
+use App\Models\User;
+use Tests\TestCase;
+
+class AssetPaginationTest extends TestCase
+{
+ private User $user;
+
+ protected function setUp(): void
+ {
+ parent::setUp();
+ $this->user = User::factory()->superuser()->create();
+ }
+
+ public function test_response_includes_pagination_fields()
+ {
+ Asset::factory()->count(3)->create();
+
+ $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index'))
+ ->assertOk()
+ ->assertJsonStructure(['total', 'rows', 'current_page', 'per_page', 'total_pages']);
+ }
+
+ public function test_default_request_returns_page_one()
+ {
+ Asset::factory()->count(3)->create();
+
+ $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index'))
+ ->assertOk()
+ ->assertJsonPath('current_page', 1);
+ }
+
+ public function test_offset_zero_returns_page_one()
+ {
+ Asset::factory()->count(10)->create();
+
+ $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['offset' => 0, 'limit' => 5]))
+ ->assertOk()
+ ->assertJsonPath('current_page', 1);
+ }
+
+ public function test_offset_derives_correct_page_number()
+ {
+ Asset::factory()->count(10)->create();
+
+ $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['offset' => 5, 'limit' => 5]))
+ ->assertOk()
+ ->assertJsonPath('current_page', 2);
+ }
+
+ public function test_page_one_returns_current_page_one()
+ {
+ Asset::factory()->count(10)->create();
+
+ $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['page' => 1, 'limit' => 5]))
+ ->assertOk()
+ ->assertJsonPath('current_page', 1);
+ }
+
+ public function test_page_two_returns_current_page_two()
+ {
+ Asset::factory()->count(10)->create();
+
+ $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['page' => 2, 'limit' => 5]))
+ ->assertOk()
+ ->assertJsonPath('current_page', 2);
+ }
+
+ public function test_page_one_returns_first_items()
+ {
+ foreach (range(1, 10) as $i) {
+ Asset::factory()->create(['asset_tag' => sprintf('PAG-TEST-%03d', $i)]);
+ }
+
+ $tags = $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['page' => 1, 'limit' => 5, 'sort' => 'asset_tag', 'order' => 'asc']))
+ ->assertOk()
+ ->json('rows.*.asset_tag');
+
+ $this->assertEquals(
+ ['PAG-TEST-001', 'PAG-TEST-002', 'PAG-TEST-003', 'PAG-TEST-004', 'PAG-TEST-005'],
+ $tags
+ );
+ }
+
+ public function test_page_two_returns_second_set_of_items()
+ {
+ foreach (range(1, 10) as $i) {
+ Asset::factory()->create(['asset_tag' => sprintf('PAG-TEST-%03d', $i)]);
+ }
+
+ $tags = $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['page' => 2, 'limit' => 5, 'sort' => 'asset_tag', 'order' => 'asc']))
+ ->assertOk()
+ ->json('rows.*.asset_tag');
+
+ $this->assertEquals(
+ ['PAG-TEST-006', 'PAG-TEST-007', 'PAG-TEST-008', 'PAG-TEST-009', 'PAG-TEST-010'],
+ $tags
+ );
+ }
+
+ public function test_offset_returns_correct_items()
+ {
+ foreach (range(1, 10) as $i) {
+ Asset::factory()->create(['asset_tag' => sprintf('PAG-TEST-%03d', $i)]);
+ }
+
+ $tags = $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['offset' => 5, 'limit' => 5, 'sort' => 'asset_tag', 'order' => 'asc']))
+ ->assertOk()
+ ->json('rows.*.asset_tag');
+
+ $this->assertEquals(
+ ['PAG-TEST-006', 'PAG-TEST-007', 'PAG-TEST-008', 'PAG-TEST-009', 'PAG-TEST-010'],
+ $tags
+ );
+ }
+
+ public function test_page_param_respects_limit()
+ {
+ Asset::factory()->count(10)->create();
+
+ $response = $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['page' => 1, 'limit' => 4]))
+ ->assertOk();
+
+ $this->assertCount(4, $response->json('rows'));
+ }
+
+ public function test_page_beyond_results_returns_empty_rows()
+ {
+ Asset::factory()->count(5)->create();
+
+ $response = $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['page' => 99, 'limit' => 5]))
+ ->assertOk();
+
+ $this->assertCount(0, $response->json('rows'));
+ $this->assertEquals(5, $response->json('total'));
+ }
+
+ public function test_offset_takes_precedence_over_page_when_both_provided()
+ {
+ Asset::factory()->count(10)->create();
+
+ // offset=0 should win over page=3, giving current_page=1
+ $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['offset' => 0, 'page' => 3, 'limit' => 5]))
+ ->assertOk()
+ ->assertJsonPath('current_page', 1);
+ }
+
+ public function test_per_page_reflects_the_limit_parameter()
+ {
+ Asset::factory()->count(3)->create();
+
+ $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['limit' => 25]))
+ ->assertOk()
+ ->assertJsonPath('per_page', 25);
+ }
+
+ public function test_total_pages_is_correct_for_even_division()
+ {
+ Asset::factory()->count(10)->create();
+
+ $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['limit' => 5]))
+ ->assertOk()
+ ->assertJsonPath('total_pages', 2);
+ }
+
+ public function test_total_pages_rounds_up_for_uneven_division()
+ {
+ Asset::factory()->count(11)->create();
+
+ $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['limit' => 5]))
+ ->assertOk()
+ ->assertJsonPath('total_pages', 3);
+ }
+
+ public function test_total_pages_is_one_when_results_fit_in_single_page()
+ {
+ Asset::factory()->count(3)->create();
+
+ $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['limit' => 5]))
+ ->assertOk()
+ ->assertJsonPath('total_pages', 1);
+ }
+}