Skip to content

Commit

Permalink
Merge pull request #1014 from SlovakNationalGallery/NSNG-555-api-show…
Browse files Browse the repository at this point in the history
…-multiple

Nsng 555 api show multiple
  • Loading branch information
igor-kamil authored Jun 13, 2024
2 parents 684499f + 9983e26 commit a667dc9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
20 changes: 19 additions & 1 deletion app/Http/Controllers/Api/V2/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace App\Http\Controllers\Api\V2;

use App\Item;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\Api\V2\ItemResource;
use App\Item;

class ItemController extends Controller
{
Expand All @@ -13,4 +14,21 @@ public function show($id)
$item = Item::with('images')->findOrFail($id);
return new ItemResource($item);
}

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

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

return ItemResource::collection($items);
}
}
2 changes: 2 additions & 0 deletions app/Http/Resources/Api/V2/ItemResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function toArray($request)
'description' => $this->description,
'authorities' => AuthorityResource::collection($this->authorities),
'image_ratio' => $this->image_ratio,
'medium' => $this->medium,
'measurements' => $this->measurements,
'images' => $this->images->map(function (ItemImage $image) {
return ['deep_zoom_url' => $image->getDeepZoomUrl()];
}),
Expand Down
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@

Route::prefix('v2')->group(function () {
Route::get('items/{id}', [V2ItemController::class, 'show']);
Route::get('items', [V2ItemController::class, 'index']);
});
25 changes: 25 additions & 0 deletions tests/Feature/Api/V2/ItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public function test_detail()
'date_latest' => 2010,
'description' => 'test_description',
'image_ratio' => 1.5,
'medium' => 'test_medium',
'measurement' => 'test_measurements',
]);

$item->authorities()->attach($authority, ['role' => 'autor/author']);
Expand Down Expand Up @@ -61,6 +63,8 @@ public function test_detail()
'date_latest' => 2010,
'description' => 'test_description',
'image_ratio' => 1.5,
'medium' => 'test_medium',
'measurements' => ['test_measurements'],
'images' => [
[
'deep_zoom_url' => 'https://img.test.sk/zoom/?path=test_iipimg_url.dzi',
Expand All @@ -69,4 +73,25 @@ public function test_detail()
],
]);
}

public function test_multiple()
{
$items = Item::factory()->count(3)->create();
$response = $this->getJson('/api/v2/items/?ids[]=' . $items->pluck('id')->implode('&ids[]='));

$response->assertStatus(200);

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

0 comments on commit a667dc9

Please sign in to comment.