Browse Source

update validation name

pull/1/head
farhantock 1 year ago
parent
commit
6bb424b6fd
  1. 81
      app/Http/Controllers/DivisiController.php
  2. 71
      app/Http/Controllers/RoleController.php

81
app/Http/Controllers/DivisiController.php

@ -8,21 +8,23 @@ use App\Models\Divisi;
class DivisiController extends Controller class DivisiController extends Controller
{ {
private function getAllChildren($divisi, $depth = 0, $array = []) { private function getAllChildren($divisi, $depth = 0, $array = [])
{
$divisi->depth = $depth; $divisi->depth = $depth;
array_push($array, $divisi); array_push($array, $divisi);
foreach($divisi->children as $child){ foreach ($divisi->children as $child) {
$array = $this->getAllChildren($child, $depth + 1, $array); $array = $this->getAllChildren($child, $depth + 1, $array);
} }
return $array; return $array;
} }
public function add(Request $request){ public function add(Request $request)
{
$this->validate($request, [ $this->validate($request, [
'name' => 'string|required|unique:m_divisi,name', 'name' => 'string|required|unique:m_divisi,name,NULL,id,company_id,' . $request->input('company_id'),
'description' => 'nullable|string', 'description' => 'nullable|string',
'parent' => 'nullable|integer', 'parent' => 'nullable|integer',
'color'=>'nullable|string|max:10' 'color' => 'nullable|string|max:10'
]); ]);
$data = $request->all(); $data = $request->all();
@ -30,15 +32,16 @@ class DivisiController extends Controller
$result = Divisi::create($data); $result = Divisi::create($data);
if(!$result) if (!$result)
return response()->json(['status'=>'failed','message'=>'Failed to add data','code'=> 500]); return response()->json(['status' => 'failed', 'message' => 'Failed to add data', 'code' => 500]);
return response()->json(['status'=>'success','message'=>'Data created!','code'=>200]); return response()->json(['status' => 'success', 'message' => 'Data created!', 'code' => 200]);
} }
public function update(Request $request, $id){ 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); if (empty($id) || !is_int((int)$id))
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
$this->validate($request, [ $this->validate($request, [
'name' => 'string|required', 'name' => 'string|required',
@ -47,46 +50,46 @@ class DivisiController extends Controller
]); ]);
$data = Divisi::find($id); $data = Divisi::find($id);
$request->name !== $data['name'] ? $this->validate($request,['name'=>'unique:m_divisi,name']) : ''; $request->name !== $data['name'] ? $this->validate($request, ['name' => 'unique:m_divisi,name']) : '';
if(!$data) if (!$data)
return response()->json(['status'=>'failed','message'=> 'Data not found!','code'=> 404], 404); return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404);
$result = $data->update($request->all()); $result = $data->update($request->all());
if(!$result) if (!$result)
return response()->json(['status'=>'failed','message'=> 'Update failed!','code'=> 500], 500); return response()->json(['status' => 'failed', 'message' => 'Update failed!', 'code' => 500], 500);
return response()->json(['status'=>'success','message'=>'Data added!','code'=>200], 200); return response()->json(['status' => 'success', 'message' => 'Data added!', 'code' => 200], 200);
} }
public function delete($id) public function delete($id)
{ {
if(empty($id) || !is_int((int)$id)) if (empty($id) || !is_int((int)$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);
$data = Divisi::find($id); $data = Divisi::find($id);
if(!$data) if (!$data)
return response()->json(['status'=>'failed','message'=> 'Data not found!','code'=> 404], 404); return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404);
$delete = $data->delete(); $delete = $data->delete();
if(!$delete) if (!$delete)
return response()->json(['status'=>'failed','message'=> 'Delete failed!','code'=> 500], 500); return response()->json(['status' => 'failed', 'message' => 'Delete failed!', 'code' => 500], 500);
return response()->json(['status'=>'success','message'=> 'Data deleted!','code'=> 200], 200); return response()->json(['status' => 'success', 'message' => 'Data deleted!', 'code' => 200], 200);
} }
public function search(Request $request) public function search(Request $request)
{ {
$payload = $request->all(); $payload = $request->all();
$dataBuilder = $this->setUpPayload($payload, 'm_divisi'); $dataBuilder = $this->setUpPayload($payload, 'm_divisi');
$builder = $dataBuilder['builder']; $builder = $dataBuilder['builder'];
$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);
//return $this->list(); //return $this->list();
// cant use builder for this case // cant use builder for this case
} }
@ -95,20 +98,20 @@ class DivisiController extends Controller
{ {
$parentMenus = Divisi::whereNull('parent')->with('children')->get(); $parentMenus = Divisi::whereNull('parent')->with('children')->get();
$divisions = []; $divisions = [];
foreach($parentMenus as $menu){ foreach ($parentMenus as $menu) {
$childs = $this->getAllChildren($menu); $childs = $this->getAllChildren($menu);
foreach($childs as $d){ foreach ($childs as $d) {
$d->displayName = ' ' . $d->name; $d->displayName = ' ' . $d->name;
for($i=0; $i < $d->depth; $i++){ for ($i = 0; $i < $d->depth; $i++) {
$d->displayName = '--' . $d->displayName ; $d->displayName = '--' . $d->displayName;
} }
array_push($divisions, $d); array_push($divisions, $d);
} }
} }
$countData = count($divisions); $countData = count($divisions);
if($countData == 0) if ($countData == 0)
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404);
return response()->json(['status'=>'success','code'=>200,'data'=> $divisions, 'totalRecord'=> $countData], 200); return response()->json(['status' => 'success', 'code' => 200, 'data' => $divisions, 'totalRecord' => $countData], 200);
} }
} }

71
app/Http/Controllers/RoleController.php

@ -11,76 +11,77 @@ class RoleController extends Controller
public function add(Request $request) public function add(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'name' => 'string|required|unique:m_roles,name', 'name' => 'string|required|unique:m_roles,name,NULL,id,company_id,' . $request->input('company_id'),
'description' => 'required' 'description' => 'required'
]); ]);
$data = $request->all(); $data = $request->all();
$data['created_by'] = $this->currentName; $data['created_by'] = $this->currentName;
if (!isset($data['default_page'])) { if (!isset($data['default_page'])) {
$data['default_page'] = 29; // dashboard $data['default_page'] = 29; // dashboard
} }
$result = Role::create($data); $result = Role::create($data);
if(!$result) if (!$result)
return response()->json(['status'=>'failed','message'=>'Failed to add data!','code'=>500], 500); return response()->json(['status' => 'failed', 'message' => 'Failed to add data!', 'code' => 500], 500);
return response()->json(['status'=>'success','message'=>'Data added!','code'=>200], 200); return response()->json(['status' => 'success', 'message' => 'Data added!', 'code' => 200], 200);
} }
public function edit( $id){ public function edit($id)
if(empty($id) || !is_int((int)$id)) {
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); if (empty($id) || !is_int((int)$id))
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
$result = Role::find($id); $result = Role::find($id);
if(!$result) if (!$result)
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=>404], 404); return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404);
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200);
} }
public function update(Request $request, $id) public function update(Request $request, $id)
{ {
if(empty($id) || !is_int((int)$id)) if (empty($id) || !is_int((int)$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, [
'name' => 'string|required', 'name' => 'string|required',
'description' => 'required' 'description' => 'required'
]); ]);
$data = Role::find($id); $data = Role::find($id);
$request->name !== $data['name'] ? $this->validate($request,['name'=>'unique:m_roles,name']) : ''; $request->name !== $data['name'] ? $this->validate($request, ['name' => 'unique:m_roles,name']) : '';
if(!$data) if (!$data)
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=>404], 404); return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404);
$result = $data->update($request->all()); $result = $data->update($request->all());
if(!$result) if (!$result)
return response()->json(['status'=>'failed','message'=>'Failed to update the data!','code'=>500], 500); return response()->json(['status' => 'failed', 'message' => 'Failed to update the data!', 'code' => 500], 500);
return response()->json(['status'=>'success','message'=>'Data updated!','code'=>200], 200); return response()->json(['status' => 'success', 'message' => 'Data updated!', 'code' => 200], 200);
} }
public function delete($id) public function delete($id)
{ {
if(empty($id) || !is_int((int)$id)) if (empty($id) || !is_int((int)$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);
$data = Role::find($id); $data = Role::find($id);
if(!$data) if (!$data)
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=>404], 404); return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404);
$delete = $data->delete(); $delete = $data->delete();
if(!$delete) if (!$delete)
return response()->json(['status'=>'failed','message'=>'Failed to delete!','code'=>500], 500); return response()->json(['status' => 'failed', 'message' => 'Failed to delete!', 'code' => 500], 500);
return response()->json(['status'=>'success','message'=>'Data deleted!','code'=>200], 200); return response()->json(['status' => 'success', 'message' => 'Data deleted!', 'code' => 200], 200);
} }
public function search(Request $request) public function search(Request $request)
@ -93,7 +94,7 @@ class RoleController extends Controller
$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 list() public function list()
@ -101,9 +102,9 @@ class RoleController extends Controller
$data = Role::all(); $data = Role::all();
$countData = $data->count(); $countData = $data->count();
if(!$data) if (!$data)
return response()->json(['status'=>'failed','message'=>'failed get list role, please try again later!','code'=>400], 400); return response()->json(['status' => 'failed', 'message' => 'failed get list role, please try again later!', 'code' => 400], 400);
return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200); return response()->json(['status' => 'success', 'code' => 200, 'data' => $data, 'totalRecord' => $countData], 200);
} }
} }

Loading…
Cancel
Save