Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[item] allow viewing of all items in api v2 #1017

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions app/Http/Controllers/Api/V2/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Comment on lines -22 to -24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just saying - I would like to keep validation whether ids is array. it doesn't have to be required.


$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);
}
Expand Down
10 changes: 6 additions & 4 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
46 changes: 41 additions & 5 deletions tests/Feature/Api/V2/ItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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([
Expand All @@ -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();
}
}