Skip to content

Commit

Permalink
[item] similar api
Browse files Browse the repository at this point in the history
  • Loading branch information
rastislav-chynoransky committed Jan 22, 2024
1 parent d14acab commit 9ba5a17
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/Elasticsearch/Repositories/ItemRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ public function getSimilar(int $size, Model $model, $locale = null): SearchResul
[
'term' => ['has_iip' => true]
]
]
],
'filter' => [
['term' => ['frontend' => config('app.frontend')]],
],
]
];

Expand Down
14 changes: 14 additions & 0 deletions app/Http/Controllers/Api/V2/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ public function related(string $id)
->get();
return ItemResource::collection($related);
}

public function similar(string $id)
{
$size = request()->get('size', 1);
$item = Item::findOrFail($id);
$ids = $this->itemRepository
->getSimilar($size, $item)
->getCollection()
->pluck('id');
$similar = Item::with(['images', 'authorities', 'translations'])
->whereIn('id', $ids)
->get();
return ItemResource::collection($similar);
}
}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
Route::prefix('v2')->group(function () {
Route::get('items/suggestions', [V2ItemController::class, 'suggestions']);
Route::get('items/{id}/related', [V2ItemController::class, 'related']);
Route::get('items/{id}/similar', [V2ItemController::class, 'similar']);
Route::get('items/{id}', [V2ItemController::class, 'show']);
});

0 comments on commit 9ba5a17

Please sign in to comment.