wahyuun
1 year ago
1 changed files with 112 additions and 99 deletions
@ -1,99 +1,112 @@
|
||||
<?php |
||||
|
||||
namespace App\Http\Controllers; |
||||
|
||||
use Illuminate\Http\Request; |
||||
use App\Models\Divisi; |
||||
|
||||
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){ |
||||
$this->validate($request, [ |
||||
'name' => 'required', |
||||
'description' => 'string', |
||||
'parent' => 'integer' |
||||
]); |
||||
|
||||
$data = $request->all(); |
||||
$data['created_by'] = $this->currentName; |
||||
|
||||
$result = Divisi::create($data); |
||||
|
||||
if(!$result) |
||||
return response()->json(['status'=>'failed','message'=>'Failed to add data','code'=> 500]); |
||||
|
||||
return response()->json(['status'=>'success','message'=>'Data created!','code'=>200]); |
||||
} |
||||
|
||||
public function update(Request $request, $id){ |
||||
if(empty($id) || !is_int((int)$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); |
||||
|
||||
$result = $data->update($request->all()); |
||||
|
||||
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) || !is_int((int)$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); |
||||
|
||||
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($countData == 0) |
||||
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); |
||||
|
||||
return response()->json(['status'=>'success','code'=>200,'data'=> $divisions, 'totalRecord'=> $countData], 200); |
||||
} |
||||
} |
||||
<?php |
||||
|
||||
namespace App\Http\Controllers; |
||||
|
||||
use Illuminate\Http\Request; |
||||
use App\Models\Divisi; |
||||
|
||||
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){ |
||||
$this->validate($request, [ |
||||
'name' => 'string|required|unique:m_divisi,name', |
||||
'description' => 'nullable|string', |
||||
'parent' => 'nullable|integer' |
||||
]); |
||||
|
||||
$data = $request->all(); |
||||
$data['created_by'] = $this->currentName; |
||||
|
||||
$result = Divisi::create($data); |
||||
|
||||
if(!$result) |
||||
return response()->json(['status'=>'failed','message'=>'Failed to add data','code'=> 500]); |
||||
|
||||
return response()->json(['status'=>'success','message'=>'Data created!','code'=>200]); |
||||
} |
||||
|
||||
public function update(Request $request, $id){ |
||||
if(empty($id) || !is_int((int)$id)) |
||||
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); |
||||
|
||||
$this->validate($request, [ |
||||
'name' => 'string|required', |
||||
'description' => 'nullable|string', |
||||
'parent' => 'nullable|integer' |
||||
]); |
||||
$data = Divisi::find($id); |
||||
|
||||
$request->name !== $data['name'] ? $this->validate($request,['name'=>'unique:m_divisi,name']) : ''; |
||||
|
||||
if(!$data) |
||||
return response()->json(['status'=>'failed','message'=> 'Data not found!','code'=> 404], 404); |
||||
|
||||
$result = $data->update($request->all()); |
||||
|
||||
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) || !is_int((int)$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); |
||||
|
||||
return response()->json(['status'=>'success','message'=> 'Data deleted!','code'=> 200], 200); |
||||
} |
||||
|
||||
public function search(Request $request) |
||||
{ |
||||
$payload = $request->all(); |
||||
$dataBuilder = $this->setUpPayload($payload, 'm_divisi'); |
||||
$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 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($countData == 0) |
||||
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); |
||||
|
||||
return response()->json(['status'=>'success','code'=>200,'data'=> $divisions, 'totalRecord'=> $countData], 200); |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue