Browse Source

update CRUD permission

pull/1/head
farhantock 11 months ago
parent
commit
f88ce60ba8
  1. 94
      app/Http/Controllers/MenuController.php
  2. 2
      app/Models/RoleMenu.php

94
app/Http/Controllers/MenuController.php

@ -23,32 +23,33 @@ class MenuController extends Controller
$result = Menu::create($data); $result = Menu::create($data);
if($result){ if ($result) {
return response()->json(['status'=>'success','message'=>'add data menu successfully!','code'=>200], 200); return response()->json(['status' => 'success', 'message' => 'add data menu successfully!', 'code' => 200], 200);
}else{ } else {
return response()->json(['status'=>'failed','message'=>'add data menu failed!','code'=>400], 400); return response()->json(['status' => 'failed', 'message' => 'add data menu failed!', 'code' => 400], 400);
} }
} }
public function edit($id){ public function edit($id)
if(!$id || (int) $id < 0 || $id==""){ {
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); if (!$id || (int) $id < 0 || $id == "") {
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
die(); die();
} }
$result = Menu::find($id); $result = Menu::find($id);
if($result){ if ($result) {
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200);
}else{ } else {
return response()->json(['status'=>'failed','message'=>'failed get data menu, please try again later!','code'=>400], 400); return response()->json(['status' => 'failed', 'message' => 'failed get data menu, please try again later!', 'code' => 400], 400);
} }
} }
public function update(Request $request, $id) public function update(Request $request, $id)
{ {
if(!$id || (int) $id < 0 || $id==""){ if (!$id || (int) $id < 0 || $id == "") {
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
} }
$this->validate($request, [ $this->validate($request, [
'icon' => 'required', 'icon' => 'required',
@ -58,21 +59,21 @@ class MenuController extends Controller
]); ]);
$data = Menu::find($id); $data = Menu::find($id);
$request->name !== $data['name'] ? $this->validate($request,['name'=>'unique:m_menu,name']) : ''; $request->name !== $data['name'] ? $this->validate($request, ['name' => 'unique:m_menu,name']) : '';
$request->url !== $data['url'] ? $this->validate($request,['url'=>'unique:m_menu,url']) : ''; $request->url !== $data['url'] ? $this->validate($request, ['url' => 'unique:m_menu,url']) : '';
if($data){ if ($data) {
$result = $data->update($request->all()); $result = $data->update($request->all());
}else{ } else {
return response()->json(['status'=>'failed','message'=>'data menu not found!','code'=>400], 400); return response()->json(['status' => 'failed', 'message' => 'data menu not found!', 'code' => 400], 400);
die(); die();
} }
if($result){ if ($result) {
return response()->json(['status'=>'success','message'=>'data menu successfully updated!','code'=>200], 200); return response()->json(['status' => 'success', 'message' => 'data menu successfully updated!', 'code' => 200], 200);
}else{ } else {
return response()->json(['status'=>'failed','message'=>'data menu failed updated!','code'=>400], 400); return response()->json(['status' => 'failed', 'message' => 'data menu failed updated!', 'code' => 400], 400);
} }
} }
@ -80,18 +81,18 @@ class MenuController extends Controller
{ {
$data = Menu::find($id); $data = Menu::find($id);
if($data){ if ($data) {
$delete = $data->delete(); $delete = $data->delete();
}else{ } else {
return response()->json(['status'=>'failed','message'=>'data menu not found!','code'=>400], 400); return response()->json(['status' => 'failed', 'message' => 'data menu not found!', 'code' => 400], 400);
die(); die();
} }
if($delete){ if ($delete) {
return response()->json(['status'=>'success','message'=>'data menu successfully deleted!','code'=>200], 200); return response()->json(['status' => 'success', 'message' => 'data menu successfully deleted!', 'code' => 200], 200);
}else{ } else {
return response()->json(['status'=>'failed','message'=>'data menu failed deleted!','code'=>400], 400); return response()->json(['status' => 'failed', 'message' => 'data menu failed deleted!', 'code' => 400], 400);
} }
} }
@ -103,12 +104,18 @@ class MenuController extends Controller
$countBuilder = $dataBuilder['count']; $countBuilder = $dataBuilder['count'];
$dataGet = $builder->get(); $dataGet = $builder->get();
$totalRecord = $countBuilder->count(); $totalRecord = $countBuilder->count();
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); return response()->json(['status' => 'success', 'code' => 200, 'data' => $dataGet, 'totalRecord' => $totalRecord], 200);
} }
public function listMenu($id) public function listMenu($id)
{ {
$dataParent = RoleMenu::select("m_menu.*") $dataParent = RoleMenu::select(
'm_menu.*',
"t_roles_menu.create",
"t_roles_menu.read",
"t_roles_menu.update",
"t_roles_menu.delete"
)
->join('m_menu', 'm_menu.id', '=', 't_roles_menu.menu_id') ->join('m_menu', 'm_menu.id', '=', 't_roles_menu.menu_id')
->join('m_roles', 'm_roles.id', '=', 't_roles_menu.role_id') ->join('m_roles', 'm_roles.id', '=', 't_roles_menu.role_id')
->whereNull('m_menu.parent_id') ->whereNull('m_menu.parent_id')
@ -117,17 +124,23 @@ class MenuController extends Controller
->get(); ->get();
$finalData = []; $finalData = [];
foreach($dataParent as $data){ foreach ($dataParent as $data) {
$data->children = $this->getChildrenMenu($data->id, $id); $data->children = $this->getChildrenMenu($data->id, $id);
$finalData[] = $data; $finalData[] = $data;
} }
return response()->json(['status'=>'success','code'=>200,'data'=>$finalData], 200); return response()->json(['status' => 'success', 'code' => 200, 'data' => $finalData], 200);
} }
protected function getChildrenMenu($id, $role_id) protected function getChildrenMenu($id, $role_id)
{ {
$dataChildren = RoleMenu::select("m_menu.*") $dataChildren = RoleMenu::select(
'm_menu.*',
"t_roles_menu.create",
"t_roles_menu.read",
"t_roles_menu.update",
"t_roles_menu.delete"
)
->join('m_menu', 'm_menu.id', '=', 't_roles_menu.menu_id') ->join('m_menu', 'm_menu.id', '=', 't_roles_menu.menu_id')
->join('m_roles', 'm_roles.id', '=', 't_roles_menu.role_id') ->join('m_roles', 'm_roles.id', '=', 't_roles_menu.role_id')
->where('m_roles.id', $role_id) ->where('m_roles.id', $role_id)
@ -135,9 +148,8 @@ class MenuController extends Controller
->orderBy('m_menu.sequence', 'ASC') ->orderBy('m_menu.sequence', 'ASC')
->get(); ->get();
$children = []; $children = [];
if($dataChildren){ if ($dataChildren) {
foreach($dataChildren as $data) foreach ($dataChildren as $data) {
{
$data->children = $this->getChildrenMenu($data->id, $role_id); $data->children = $this->getChildrenMenu($data->id, $role_id);
$children[] = $data; $children[] = $data;
} }
@ -150,10 +162,10 @@ class MenuController extends Controller
$data = Menu::all(); $data = Menu::all();
$countData = $data->count(); $countData = $data->count();
if($data){ if ($data) {
return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200); return response()->json(['status' => 'success', 'code' => 200, 'data' => $data, 'totalRecord' => $countData], 200);
}else{ } else {
return response()->json(['status'=>'failed','message'=>'failed get list menu, please try again later!','code'=>400], 400); return response()->json(['status' => 'failed', 'message' => 'failed get list menu, please try again later!', 'code' => 400], 400);
} }
} }
} }

2
app/Models/RoleMenu.php

@ -12,6 +12,6 @@ class RoleMenu extends Model
const UPDATED_AT = 'updated_at'; const UPDATED_AT = 'updated_at';
protected $fillable = [ protected $fillable = [
'role_id', 'menu_id', 'created_at', 'created_by', 'updated_at', 'updated_by' 'role_id', 'menu_id', 'create', 'read', 'update', 'delete', 'created_at', 'created_by', 'updated_at', 'updated_by'
]; ];
} }

Loading…
Cancel
Save