diff --git a/app/Http/Controllers/Api/V2/ItemController.php b/app/Http/Controllers/Api/V2/ItemController.php index 587ea41a8..bbb0e3959 100644 --- a/app/Http/Controllers/Api/V2/ItemController.php +++ b/app/Http/Controllers/Api/V2/ItemController.php @@ -17,17 +17,18 @@ public function show($id) public function index(Request $request) { - $ids = $request->input('ids'); + $validated = $request->validate([ + 'ids' => ['array'], + 'ids.*' => ['required', 'string'], + 'size' => ['integer'], + ]); - if (!$ids || !is_array($ids)) { - return response()->json(['error' => 'Invalid input'], 400); - } - - $size = $request->input('size', 15); - $items = Item::with(['images', 'authorities']) - ->whereIn('id', $ids) - ->paginate($size); + ->when( + $request->filled('ids'), + fn ($query) => $query->whereIn('id', $validated['ids']) + ) + ->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 c4f0cd4fd..ac1dce529 100644 --- a/tests/Feature/Api/V2/ItemsTest.php +++ b/tests/Feature/Api/V2/ItemsTest.php @@ -8,12 +8,14 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; use Illuminate\Support\Facades\Config; +use Tests\WithoutSearchIndexing; 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']); @@ -74,13 +76,12 @@ public function test_detail() ]); } - public function test_multiple() + public function testIndex() { $items = Item::factory()->count(3)->create(); - $response = $this->getJson('/api/v2/items/?ids[]=' . $items->pluck('id')->implode('&ids[]=')); - - $response->assertStatus(200); + $response = $this->getJson('/api/v2/items'); + $response->assertOk(); $response->assertJsonCount(3, 'data'); foreach ($items as $item) { $response->assertJsonFragment([ @@ -94,4 +95,39 @@ public function test_multiple() ]); } } + + public function testIndexWithIds() + { + $items = Item::factory()->count(3)->create(); + $filtered = $items->random(2); + $url = route('api.v2.items.index', [ + 'ids' => $filtered->pluck('id')->toArray() + ]); + $response = $this->getJson($url); + + $response->assertOk(); + $response->assertJsonCount($filtered->count(), 'data'); + foreach ($filtered as $item) { + $response->assertJsonFragment([ + 'id' => $item->id, + 'title' => $item->title, + 'description' => $item->description, + 'image_ratio' => $item->image_ratio, + 'medium' => $item->medium, + 'measurements' => [$item->measurement], + 'images' => [], + ]); + } + } + + public function testIndexWithInvalidIds() + { + $item = Item::factory()->create(); + $url = route('api.v2.items.index', [ + 'ids' => $item->id, + ]); + + $response = $this->getJson($url); + $response->assertUnprocessable(); + } }