Custom Backend OSPRO Surveyor Indonesia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.3 KiB

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\HierarchyFtth;
use App\Models\VersionGantt;
class HierarchyFtthController extends Controller
{
public function index()
{
try {
$hierarchyFtths = HierarchyFtth::all();
return response()->json($hierarchyFtths);
} catch (\Exception $e) {
return response()->json([
'message' => 'Failed to retrieve hierarchy FTTHs.',
'error' => $e->getMessage()
], 500);
}
}
public function search(Request $request)
{
$payload = $request->all();
$dataBuilder = $this->setUpPayload($payload, 'm_hierarchy_ftth');
$builder = $dataBuilder['builder'];
$countBuilder = $dataBuilder['count'];
$dataGet = $builder->get();
$totalRecord = $countBuilder->count();
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200);
}
public function store(Request $request)
{
try {
$attributes = $request->only(['name', 'parent_id', 'project_id']);
$hierarchyFtth = HierarchyFtth::create($attributes);
return response()->json([
'message' => 'Hierarchy FTTH created successfully.',
'data' => $hierarchyFtth
],200);
} catch (\Exception $e) {
return response()->json([
'message' => 'Failed to create hierarchy FTTH.',
'error' => $e->getMessage()
], 500);
}
}
public function show($id)
{
$hierarchyFtth = HierarchyFtth::where('id', $id)->first();
try {
return response()->json($hierarchyFtth);
} catch (\Exception $e) {
return response()->json([
'message' => 'Failed to retrieve hierarchy FTTH.',
'error' => $e->getMessage()
], 500);
}
}
public function update(Request $request, $id)
{
$hierarchyFtth = HierarchyFtth::where('id', $id)->first();
try {
$hierarchyFtth->update($request->all());
return response()->json([
'message' => 'Hierarchy FTTH updated successfully.',
'data' => $hierarchyFtth
],200);
} catch (\Exception $e) {
return response()->json([
'message' => 'Failed to update hierarchy FTTH.',
'error' => $e->getMessage()
], 500);
}
}
public function destroy($id)
{
$hierarchyFtth = HierarchyFtth::where('id', $id)->first();
try {
$hierarchyFtth->delete();
return response()->json([
'message' => 'Hierarchy FTTH deleted successfully.',
'data' => $hierarchyFtth
],200);
} catch (\Exception $e) {
return response()->json([
'message' => 'Failed to delete hierarchy FTTH.',
'error' => $e->getMessage()
], 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);
if (VersionGantt::where('hierarchy_ftth_id', $objRow->id)->exists()) {
$dataGantt = VersionGantt::where('hierarchy_ftth_id', $objRow->id)->get();
foreach ($dataGantt as $gantt) {
$gantt->key = $objRow->key;
}
$objRow->dataGantt = $dataGantt;
}
$objRow->children = $this->getChildren($project_id, $objRow->id);
$finalData[] = $objRow;
}
return $finalData;
}
}