Skip to content

A free open source IT asset/license management system

Include next and previous urls in the payload

sniped901e821cc99
diff --git a/app/Http/Transformers/DatatablesTransformer.php b/app/Http/Transformers/DatatablesTransformer.php
index e33d34d1ff..7f80cf9201 100644
--- a/app/Http/Transformers/DatatablesTransformer.php
+++ b/app/Http/Transformers/DatatablesTransformer.php
@@ -11,10 +11,21 @@ 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');
+ $current_page = app('api_current_page');
+ $limit = (int) app('api_limit_value');
+ $total_pages = $limit > 0 ? (int) ceil($objects_array['total'] / $limit) : 1;
+
+ $objects_array['current_page'] = $current_page;
$objects_array['per_page'] = $limit;
- $objects_array['total_pages'] = $limit > 0 ? (int) ceil($objects_array['total'] / $limit) : 1;
+ $objects_array['total_pages'] = $total_pages;
+
+ $base_query = collect(request()->query())->except(['page', 'offset'])->all();
+ $objects_array['prev_page_url'] = $current_page > 1
+ ? request()->url().'?'.http_build_query(array_merge($base_query, ['page' => $current_page - 1, 'limit' => $limit]))
+ : null;
+ $objects_array['next_page_url'] = $current_page < $total_pages
+ ? request()->url().'?'.http_build_query(array_merge($base_query, ['page' => $current_page + 1, 'limit' => $limit]))
+ : null;
return $objects_array;
}
diff --git a/tests/Feature/Assets/Api/AssetPaginationTest.php b/tests/Feature/Assets/Api/AssetPaginationTest.php
index 12cdf91c5f..a8f752f3a7 100644
--- a/tests/Feature/Assets/Api/AssetPaginationTest.php
+++ b/tests/Feature/Assets/Api/AssetPaginationTest.php
@@ -161,14 +161,73 @@ class AssetPaginationTest extends TestCase
->assertJsonPath('current_page', 1);
}
- public function test_per_page_reflects_the_limit_parameter()
+ public function test_per_page_reflects_the_limit_parameter_as_an_integer()
{
Asset::factory()->count(3)->create();
- $this->actingAsForApi($this->user)
+ $response = $this->actingAsForApi($this->user)
->getJson(route('api.assets.index', ['limit' => 25]))
->assertOk()
->assertJsonPath('per_page', 25);
+
+ $this->assertIsInt($response->json('per_page'));
+ }
+
+ public function test_prev_page_url_is_null_on_first_page()
+ {
+ Asset::factory()->count(10)->create();
+
+ $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['page' => 1, 'limit' => 5]))
+ ->assertOk()
+ ->assertJsonPath('prev_page_url', null);
+ }
+
+ public function test_next_page_url_is_null_on_last_page()
+ {
+ Asset::factory()->count(10)->create();
+
+ $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['page' => 2, 'limit' => 5]))
+ ->assertOk()
+ ->assertJsonPath('next_page_url', null);
+ }
+
+ public function test_next_page_url_contains_correct_page_number()
+ {
+ Asset::factory()->count(10)->create();
+
+ $url = $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['page' => 1, 'limit' => 5]))
+ ->assertOk()
+ ->json('next_page_url');
+
+ $this->assertStringContainsString('page=2', $url);
+ $this->assertStringContainsString('limit=5', $url);
+ }
+
+ public function test_prev_page_url_contains_correct_page_number()
+ {
+ Asset::factory()->count(10)->create();
+
+ $url = $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['page' => 2, 'limit' => 5]))
+ ->assertOk()
+ ->json('prev_page_url');
+
+ $this->assertStringContainsString('page=1', $url);
+ $this->assertStringContainsString('limit=5', $url);
+ }
+
+ public function test_both_page_urls_null_when_single_page()
+ {
+ Asset::factory()->count(3)->create();
+
+ $this->actingAsForApi($this->user)
+ ->getJson(route('api.assets.index', ['page' => 1, 'limit' => 50]))
+ ->assertOk()
+ ->assertJsonPath('prev_page_url', null)
+ ->assertJsonPath('next_page_url', null);
}
public function test_total_pages_is_correct_for_even_division()