From a8a485a2c247cbaf8b54179ed97fb84de4960d3a Mon Sep 17 00:00:00 2001 From: Mangiang Date: Tue, 12 Oct 2021 22:31:29 -0400 Subject: [PATCH 1/2] feat: Add hierarchy endpoint --- app/Data/ItemsCollection.php | 29 +++++++++++++++++++++++++ app/Http/Controllers/ItemController.php | 20 +++++++++++++++++ routes/web.php | 9 +++++--- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/app/Data/ItemsCollection.php b/app/Data/ItemsCollection.php index ebb8a6f..c9ba6bc 100644 --- a/app/Data/ItemsCollection.php +++ b/app/Data/ItemsCollection.php @@ -134,4 +134,33 @@ class ItemsCollection 'locale' => $this->locales[$locale][$id] ?? '' ]; } + + public function getHierarchy(string $id, string $locale = 'en'): Collection { + // Return 404 if the item does not exist + $itemData = $this->items[$id] ?? throw new ItemNotFoundException('Item not found'); + + // Initialize the hierarchy with the current item + $item = [ + '_id' => $itemData['_id'], + '_name' => $itemData['_name'], + '_parent' => $itemData['_parent'], + 'locale' => $this->locales[$locale][$id] ?? '' + ]; + $hierarchy = collect([$id => $item]); + + // Check the whole hierarchy and merge into the return variable + while (!empty($item['_parent'] ?? '')) { + $itemtId = $item['_parent']; + $itemData = $this->items[$itemtId] ?? null; + $item = [ + '_id' => $itemData['_id'], + '_name' => $itemData['_name'], + '_parent' => $itemData['_parent'], + 'locale' => $this->locales[$locale][$itemtId] ?? '' + ]; + $hierarchy = $hierarchy->merge([$itemtId => $item]); + } + + return $hierarchy; + } } diff --git a/app/Http/Controllers/ItemController.php b/app/Http/Controllers/ItemController.php index 95ade6a..0365113 100644 --- a/app/Http/Controllers/ItemController.php +++ b/app/Http/Controllers/ItemController.php @@ -60,6 +60,26 @@ class ItemController extends Controller } } + + /** + * @param Request $request The request + * @return JsonResponse Either {"error":"item not found"} or an object containing + * for each id, an object looking like + * {"item":{"_id":"id","_name":"name","_parent":"parent"}, "locale": {"Description":"description", "Name":"name", "ShortName":"shortName"}} + */ + public function getHierarchy(Request $request): JsonResponse + { + try { + return response()->json($this->itemsCollection->getHierarchy($request->id, $request->locale ?? 'en')); + } catch (Throwable $exception) { + Log::error($exception->getMessage()); + Log::error($exception->getTraceAsString()); + return response()->json([ + 'error' => 'Item not found.', + ], Response::HTTP_NOT_FOUND); + } + } + /** * @return JsonResponse */ diff --git a/routes/web.php b/routes/web.php index df590ce..6b47fbc 100644 --- a/routes/web.php +++ b/routes/web.php @@ -18,7 +18,10 @@ $router->get('/', function () { }); $router->get('/api/locales', 'ItemController@getLocales'); -$router->get('/api/refresh', 'ItemController@refreshAllCache'); -$router->post('/api/search', 'ItemController@search'); -$router->get('/api/item', 'ItemController@getItem'); +$router->get('/api/refresh', 'ItemController@refreshAllCache'); + +$router->post('/api/search', 'ItemController@search'); + +$router->get('/api/item/hierarchy', 'ItemController@getHierarchy'); +$router->get('/api/item', 'ItemController@getItem'); \ No newline at end of file From 3c4441bd59924779eb350450b843ef9331608376 Mon Sep 17 00:00:00 2001 From: Mangiang Date: Tue, 12 Oct 2021 23:54:48 -0400 Subject: [PATCH 2/2] fix: Update hierarchy endpoint to the new structure --- app/Data/ItemsCollection.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/app/Data/ItemsCollection.php b/app/Data/ItemsCollection.php index c9ba6bc..7d363be 100644 --- a/app/Data/ItemsCollection.php +++ b/app/Data/ItemsCollection.php @@ -141,21 +141,25 @@ class ItemsCollection // Initialize the hierarchy with the current item $item = [ - '_id' => $itemData['_id'], - '_name' => $itemData['_name'], - '_parent' => $itemData['_parent'], + 'item'=> [ + '_id' => $itemData['_id'], + '_name' => $itemData['_name'], + '_parent' => $itemData['_parent'] + ], 'locale' => $this->locales[$locale][$id] ?? '' ]; $hierarchy = collect([$id => $item]); // Check the whole hierarchy and merge into the return variable - while (!empty($item['_parent'] ?? '')) { - $itemtId = $item['_parent']; + while (!empty($item['item']['_parent'] ?? '')) { + $itemtId = $item['item']['_parent']; $itemData = $this->items[$itemtId] ?? null; $item = [ - '_id' => $itemData['_id'], - '_name' => $itemData['_name'], - '_parent' => $itemData['_parent'], + 'item'=> [ + '_id' => $itemData['_id'], + '_name' => $itemData['_name'], + '_parent' => $itemData['_parent'] + ], 'locale' => $this->locales[$locale][$itemtId] ?? '' ]; $hierarchy = $hierarchy->merge([$itemtId => $item]);