|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Models\Role;
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
|
|
|
class RoleController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
public function add(Request $request)
|
|
|
|
{
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
|
|
'name' => 'required',
|
|
|
|
'description' => 'required',
|
|
|
|
'created_by' => '',
|
|
|
|
'updated_by' => ''
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return response()->json(['status' => 'failed', 'message' => 'Failed to add data!', 'code' => 500], 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
$validated = $validator->validated();
|
|
|
|
$validated['created_by'] = $this->currentName;
|
|
|
|
|
|
|
|
Role::create($validated);
|
|
|
|
return response()->json(['status' => 'success', 'message' => 'Data added!', 'code' => 200], 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit($id)
|
|
|
|
{
|
|
|
|
if (empty($id)) {
|
|
|
|
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = Role::find($id);
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404);
|
|
|
|
} else {
|
|
|
|
return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
|
|
|
if (empty($id)) {
|
|
|
|
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = Role::find($id);
|
|
|
|
$validated = $request->validate([
|
|
|
|
'name' => 'required',
|
|
|
|
'description' => 'required',
|
|
|
|
'created_by' => '',
|
|
|
|
'updated_by' => ''
|
|
|
|
]);
|
|
|
|
|
|
|
|
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' => 'Failed to update the data!', 'code' => 500], 500);
|
|
|
|
} else {
|
|
|
|
return response()->json(['status' => 'success', 'message' => 'Data updated!', 'code' => 200], 200);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($id)
|
|
|
|
{
|
|
|
|
if (empty($id)) {
|
|
|
|
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = Role::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' => 'Failed to delete!', 'code' => 500], 500);
|
|
|
|
} else {
|
|
|
|
return response()->json(['status' => 'success', 'message' => 'Data deleted!', 'code' => 200], 200);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function search(Request $request)
|
|
|
|
{
|
|
|
|
$payload = $request->all();
|
|
|
|
|
|
|
|
$dataBuilder = $this->setUpPayload($payload, 'm_roles');
|
|
|
|
$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()
|
|
|
|
{
|
|
|
|
$countData = Role::count();
|
|
|
|
|
|
|
|
if (empty($countData)) {
|
|
|
|
return response()->json(['status' => 'failed', 'message' => 'failed get list role, please try again later!', 'code' => 400], 400);
|
|
|
|
} else {
|
|
|
|
return response()->json(['status' => 'success', 'code' => 200, 'data' => $data, 'totalRecord' => $countData], 200);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|