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 1 commit
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
13 changes: 5 additions & 8 deletions app/Http/Controllers/Api/V2/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ public function show($id)

public function index(Request $request)
{
$ids = $request->input('ids');

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)
->when(
$request->filled('ids'),
fn ($query) => $query->whereIn('id', (array) $request->input('ids'))
)
->paginate($size);

return ItemResource::collection($items);
Expand Down
28 changes: 26 additions & 2 deletions tests/Feature/Api/V2/ItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
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()
{
Expand Down Expand Up @@ -74,10 +76,10 @@ 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 = $this->getJson('/api/v2/items');

$response->assertStatus(200);

Expand All @@ -94,4 +96,26 @@ public function test_multiple()
]);
}
}

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);

$response->assertJsonCount(2, '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' => [],
]);
}
}
}