diff --git a/app/Http/Controllers/Api/V2/ItemController.php b/app/Http/Controllers/Api/V2/ItemController.php index d1206dfc7..bbb0e3959 100644 --- a/app/Http/Controllers/Api/V2/ItemController.php +++ b/app/Http/Controllers/Api/V2/ItemController.php @@ -17,14 +17,18 @@ public function show($id) public function index(Request $request) { - $size = $request->input('size', 15); + $validated = $request->validate([ + 'ids' => ['array'], + 'ids.*' => ['required', 'string'], + 'size' => ['integer'], + ]); $items = Item::with(['images', 'authorities']) ->when( $request->filled('ids'), - fn ($query) => $query->whereIn('id', (array) $request->input('ids')) + fn ($query) => $query->whereIn('id', $validated['ids']) ) - ->paginate($size); + ->paginate($validated['size'] ?? null); return ItemResource::collection($items); } diff --git a/routes/api.php b/routes/api.php index 5d380835f..596d20b99 100644 --- a/routes/api.php +++ b/routes/api.php @@ -69,7 +69,9 @@ ); }); -Route::prefix('v2')->group(function () { - Route::get('items/{id}', [V2ItemController::class, 'show']); - Route::get('items', [V2ItemController::class, 'index']); -}); +Route::prefix('v2') + ->name('api.v2.') + ->group(function () { + Route::resource('items', V2ItemController::class) + ->only(['index', 'show']); + }); diff --git a/tests/Feature/Api/V2/ItemsTest.php b/tests/Feature/Api/V2/ItemsTest.php index df3ef0e75..ac1dce529 100644 --- a/tests/Feature/Api/V2/ItemsTest.php +++ b/tests/Feature/Api/V2/ItemsTest.php @@ -15,7 +15,7 @@ class ItemsTest extends TestCase use RefreshDatabase; use WithoutSearchIndexing; - public function test_detail() + public function testShow() { $authority = Authority::factory()->create(['name' => 'Wouwerman, Philips']); $item_image = ItemImage::factory()->make(['iipimg_url' => 'test_iipimg_url']); @@ -81,8 +81,7 @@ public function testIndex() $items = Item::factory()->count(3)->create(); $response = $this->getJson('/api/v2/items'); - $response->assertStatus(200); - + $response->assertOk(); $response->assertJsonCount(3, 'data'); foreach ($items as $item) { $response->assertJsonFragment([ @@ -101,11 +100,13 @@ public function testIndexWithIds() { $items = Item::factory()->count(3)->create(); $filtered = $items->random(2); - $response = $this->getJson('/api/v2/items?ids[]=' . $filtered->pluck('id')->implode('&ids[]=')); - - $response->assertStatus(200); + $url = route('api.v2.items.index', [ + 'ids' => $filtered->pluck('id')->toArray() + ]); + $response = $this->getJson($url); - $response->assertJsonCount(2, 'data'); + $response->assertOk(); + $response->assertJsonCount($filtered->count(), 'data'); foreach ($filtered as $item) { $response->assertJsonFragment([ 'id' => $item->id, @@ -118,4 +119,15 @@ public function testIndexWithIds() ]); } } + + public function testIndexWithInvalidIds() + { + $item = Item::factory()->create(); + $url = route('api.v2.items.index', [ + 'ids' => $item->id, + ]); + + $response = $this->getJson($url); + $response->assertUnprocessable(); + } }