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.
130 lines
4.3 KiB
130 lines
4.3 KiB
<?php |
|
|
|
namespace App\Http\Controllers\API; |
|
|
|
use App\Http\Controllers\Controller; |
|
use Illuminate\Http\Request; |
|
use App\Models\Divisi; |
|
use Illuminate\Support\Facades\Validator; |
|
|
|
class DivisiController extends Controller |
|
{ |
|
|
|
private function getAllChildren($divisi, $depth = 0, $array = []) |
|
{ |
|
$divisi->depth = $depth; |
|
array_push($array, $divisi); |
|
foreach ($divisi->children as $child) { |
|
$array = $this->getAllChildren($child, $depth + 1, $array); |
|
} |
|
return $array; |
|
} |
|
|
|
public function add(Request $request) |
|
{ |
|
$validator = Validator::make($request->all(), [ |
|
'name' => 'required|string|max:200', |
|
'description' => 'nullable', |
|
'parent' => 'nullable|integer', |
|
'created_by' => 'nullable|string|max:80', |
|
'updated_by' => 'nullable|string|max:80' |
|
]); |
|
|
|
if ($validator->fails()) { |
|
return response()->json(['status' => 'failed', 'message' => 'Failed to add data', 'code' => 500]); |
|
} |
|
$validated = $validator->validated(); |
|
|
|
$validated['created_by'] = $this->currentName; |
|
|
|
$result = Divisi::create($validated); |
|
|
|
if (!$result) { |
|
return response()->json(['status' => 'failed', 'message' => 'Failed to add data', 'code' => 500]); |
|
} else { |
|
return response()->json(['status' => 'success', 'message' => 'Data created!', 'code' => 200]); |
|
} |
|
} |
|
|
|
public function update(Request $request, $id) |
|
{ |
|
if (empty($id)) { |
|
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); |
|
} |
|
$data = Divisi::find($id); |
|
|
|
$validator = Validator::make($request->all(), [ |
|
'name' => 'required|string|max:200', |
|
'description' => 'nullable', |
|
'parent' => 'nullable|integer', |
|
'created_by' => 'nullable|string|max:80', |
|
'updated_by' => 'nullable|string|max:80' |
|
]); |
|
|
|
if ($validator->fails()) { |
|
return response()->json(['status' => 'failed', 'message' => 'Update failed!', 'code' => 500], 500); |
|
} |
|
$validated = $validator->validated(); |
|
|
|
if (!$data) { |
|
return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); |
|
} |
|
|
|
$result = $data->update($validated); |
|
|
|
if (!$result) { |
|
return response()->json(['status' => 'failed', 'message' => 'Update failed!', 'code' => 500], 500); |
|
} |
|
|
|
return response()->json(['status' => 'success', 'message' => 'Data added!', 'code' => 200], 200); |
|
} |
|
|
|
public function delete($id) |
|
{ |
|
if (empty($id)) { |
|
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); |
|
} |
|
|
|
$data = Divisi::find($id); |
|
|
|
if (!$data) { |
|
return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); |
|
} |
|
|
|
$delete = $data->delete(); |
|
|
|
if (!$delete) { |
|
return response()->json(['status' => 'failed', 'message' => 'Delete failed!', 'code' => 500], 500); |
|
} else { |
|
return response()->json(['status' => 'success', 'message' => 'Data deleted!', 'code' => 200], 200); |
|
} |
|
} |
|
|
|
public function search() |
|
{ |
|
return $this->list(); |
|
// cant use builder for this case |
|
} |
|
|
|
public function list() |
|
{ |
|
$parentMenus = Divisi::whereNull('parent')->with('children')->get(); |
|
$divisions = []; |
|
foreach ($parentMenus as $menu) { |
|
$childs = $this->getAllChildren($menu); |
|
foreach ($childs as $d) { |
|
$d->displayName = ' ' . $d->name; |
|
for ($i = 0; $i < $d->depth; $i++) { |
|
$d->displayName = '--' . $d->displayName; |
|
} |
|
array_push($divisions, $d); |
|
} |
|
} |
|
$countData = count($divisions); |
|
if (empty($countData)) { |
|
return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); |
|
} else { |
|
return response()->json(['status' => 'success', 'code' => 200, 'data' => $divisions, 'totalRecord' => $countData], 200); |
|
} |
|
} |
|
}
|
|
|