From 314450892119966ccabaa6ec8927bd59b0702c2f Mon Sep 17 00:00:00 2001 From: Wahyu Ramadhan Date: Thu, 30 Mar 2023 16:37:59 +0700 Subject: [PATCH] Minor fix --- .../Controllers/HierarchyFtthController.php | 34 +++++++++++++++++-- routes/web.php | 7 ++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/HierarchyFtthController.php b/app/Http/Controllers/HierarchyFtthController.php index e172ddc..1184109 100644 --- a/app/Http/Controllers/HierarchyFtthController.php +++ b/app/Http/Controllers/HierarchyFtthController.php @@ -49,8 +49,9 @@ class HierarchyFtthController extends Controller } } - public function show(HierarchyFtth $hierarchyFtth) + public function show($id) { + $hierarchyFtth = HierarchyFtth::where('id', $id)->first(); try { return response()->json($hierarchyFtth); } catch (\Exception $e) { @@ -61,8 +62,9 @@ class HierarchyFtthController extends Controller } } - public function update(Request $request, HierarchyFtth $hierarchyFtth) + public function update(Request $request, $id) { + $hierarchyFtth = HierarchyFtth::where('id', $id)->first(); try { $hierarchyFtth->update($request->all()); @@ -78,8 +80,9 @@ class HierarchyFtthController extends Controller } } - public function destroy(HierarchyFtth $hierarchyFtth) + public function destroy($id) { + $hierarchyFtth = HierarchyFtth::where('id', $id)->first(); try { $hierarchyFtth->delete(); @@ -94,4 +97,29 @@ class HierarchyFtthController extends Controller ], 500); } } + + public function getTreeByProject($project_id) + { + $data = HierarchyFtth::where('project_id', $project_id)->whereNull('parent_id')->orderByRaw('id ASC')->get(); + $finalData = []; + foreach($data as $objRow){ + $objRow->children = $this->getChildren($project_id, $objRow->id); + $objRow->key = rand(1, 1000); + $finalData[] = $objRow; + } + + return response()->json(['status'=>'success','data'=>$finalData,'code'=>200], 200); + } + + private function getChildren($project_id, $parent_id) + { + $finalData = []; + $data = HierarchyFtth::where('project_id', $project_id)->where('parent_id', $parent_id)->orderByRaw('id ASC')->get(); + foreach($data as $objRow){ + $objRow->key = rand(1, 1000); + $objRow->children = $this->getChildren($project_id, $objRow->id); + $finalData[] = $objRow; + } + return $finalData; + } } diff --git a/routes/web.php b/routes/web.php index 61a480c..b403f54 100644 --- a/routes/web.php +++ b/routes/web.php @@ -442,9 +442,10 @@ $router->group(['prefix'=>'api', 'middleware' => 'cors'], function () use ($rout $router->get('/hierarchy-ftths', 'HierarchyFtthController@index'); $router->post('/hierarchy-ftths', 'HierarchyFtthController@store'); $router->post('/hierarchy-ftths/search', 'HierarchyFtthController@search'); - $router->get('/hierarchy-ftths/{hierarchyFtth}', 'HierarchyFtthController@show'); - $router->put('/hierarchy-ftths/{hierarchyFtth}', 'HierarchyFtthController@update'); - $router->delete('/hierarchy-ftths/{hierarchyFtth}', 'HierarchyFtthController@destroy'); + $router->get('/hierarchy-ftths/{id}', 'HierarchyFtthController@show'); + $router->put('/hierarchy-ftths/{id}', 'HierarchyFtthController@update'); + $router->delete('/hierarchy-ftths/{id}', 'HierarchyFtthController@destroy'); + $router->get('/hierarchy-ftths/tree/{project_id}', 'HierarchyFtthController@getTreeByProject'); $router->post('/map-monitoring/search', 'MapMonitoringController@search'); });