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
|
|
|
|
*/
|
2021-10-12 00:40:07 -04:00
|
|
|
public function search(Request $request): JsonResponse
|
2021-10-08 20:44:34 +09:00
|
|
|
{
|
2021-10-11 16:00:49 +09:00
|
|
|
$status = Response::HTTP_NO_CONTENT;
|
2021-10-12 00:40:07 -04:00
|
|
|
$result = $this->itemsCollection->findItem($request->input('query'), $request->input('locale') ?? 'en');
|
2021-10-11 16:00:49 +09:00
|
|
|
$response = [];
|
|
|
|
if ($result->isNotEmpty()) {
|
|
|
|
$status = Response::HTTP_OK;
|
|
|
|
$response = [
|
|
|
|
'items' => $result->toArray(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return response()->json($response, $status);
|
2021-10-08 20:44:34 +09:00
|
|
|
}
|
|
|
|
|
2021-10-10 13:10:18 +09:00
|
|
|
/**
|
|
|
|
* @param string $id
|
2021-10-11 19:47:34 -04:00
|
|
|
* @param string $locale
|
2021-10-10 13:10:18 +09:00
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
2021-10-11 19:47:34 -04:00
|
|
|
public function getItem(string $id, string $locale = 'en'): JsonResponse
|
2021-10-08 20:44:34 +09:00
|
|
|
{
|
2021-10-10 13:10:35 +09:00
|
|
|
try {
|
2021-10-11 19:47:34 -04:00
|
|
|
return response()->json($this->itemsCollection->getItemById($id, $locale));
|
2021-10-10 13:10:35 +09:00
|
|
|
} catch (Throwable $exception) {
|
|
|
|
Log::error($exception->getMessage());
|
|
|
|
Log::error($exception->getTraceAsString());
|
|
|
|
return response()->json([
|
2021-10-10 13:50:49 +09:00
|
|
|
'error' => 'Item not found.',
|
2021-10-10 13:10:35 +09:00
|
|
|
], Response::HTTP_NOT_FOUND);
|
|
|
|
}
|
2021-10-08 20:44:34 +09:00
|
|
|
}
|
2021-10-11 17:29:15 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
2021-10-11 19:47:34 -04:00
|
|
|
public function getLocales(): JsonResponse
|
2021-10-11 17:29:15 -04:00
|
|
|
{
|
|
|
|
try {
|
2021-10-11 19:47:34 -04:00
|
|
|
return response()->json($this->itemsCollection->getLocales());
|
2021-10-11 17:29:15 -04:00
|
|
|
} catch (Throwable $exception) {
|
|
|
|
Log::error($exception->getMessage());
|
|
|
|
Log::error($exception->getTraceAsString());
|
|
|
|
return response()->json([
|
2021-10-11 19:47:34 -04:00
|
|
|
'error' => 'No locale not found.',
|
2021-10-11 17:29:15 -04:00
|
|
|
], Response::HTTP_NOT_FOUND);
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 19:47:34 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $id
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
public function refreshAllCache(): JsonResponse
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return response()->json($this->itemsCollection->refreshAllCache());
|
|
|
|
} catch (Throwable $exception) {
|
|
|
|
Log::error($exception->getMessage());
|
|
|
|
Log::error($exception->getTraceAsString());
|
|
|
|
}
|
|
|
|
}
|
2021-10-08 20:44:34 +09:00
|
|
|
}
|