2021-10-08 20:44:34 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Data\ItemsCollection;
|
2021-10-10 13:10:18 +09:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2021-10-08 20:44:34 +09:00
|
|
|
use Illuminate\Http\Request;
|
2021-10-10 13:10:35 +09:00
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Throwable;
|
2021-10-08 20:44:34 +09:00
|
|
|
|
|
|
|
class ItemController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ItemsCollection
|
|
|
|
*/
|
|
|
|
private ItemsCollection $itemsCollection;
|
|
|
|
|
|
|
|
public function __construct(ItemsCollection $itemsCollection)
|
|
|
|
{
|
|
|
|
$this->itemsCollection = $itemsCollection;
|
|
|
|
}
|
|
|
|
|
2021-10-10 13:10:18 +09:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
public function search(Request $request): JsonResponse
|
2021-10-08 20:44:34 +09:00
|
|
|
{
|
|
|
|
return response()->json([
|
|
|
|
'item' => $this->itemsCollection->findItem($request->input('query')),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-10-10 13:10:18 +09:00
|
|
|
/**
|
|
|
|
* @param string $id
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
public function getItem(string $id): JsonResponse
|
2021-10-08 20:44:34 +09:00
|
|
|
{
|
2021-10-10 13:10:35 +09:00
|
|
|
try {
|
|
|
|
return response()->json([
|
|
|
|
'item' => $this->itemsCollection->getItemById($id),
|
|
|
|
]);
|
|
|
|
} catch (Throwable $exception) {
|
|
|
|
Log::error($exception->getMessage());
|
|
|
|
Log::error($exception->getTraceAsString());
|
|
|
|
return response()->json([
|
|
|
|
'item' => [],
|
|
|
|
], Response::HTTP_NOT_FOUND);
|
|
|
|
}
|
2021-10-08 20:44:34 +09:00
|
|
|
}
|
|
|
|
}
|