From 63200efb88a4b4453621b8e5dc74e7234b565913 Mon Sep 17 00:00:00 2001 From: ajikamaludin Date: Wed, 15 Mar 2023 20:30:38 +0700 Subject: [PATCH] fix template --- app/Http/Controllers/Api/RoleController.php | 21 ++++ app/Http/Controllers/GeneralController.php | 18 ++++ app/Http/Controllers/RoleController.php | 113 ++++++++++++++++++++ app/Http/Controllers/UserController.php | 89 +++++++++++++++ resources/js/Pages/Maintance.jsx | 27 +++++ 5 files changed, 268 insertions(+) create mode 100644 app/Http/Controllers/Api/RoleController.php create mode 100644 app/Http/Controllers/GeneralController.php create mode 100644 app/Http/Controllers/RoleController.php create mode 100644 app/Http/Controllers/UserController.php create mode 100644 resources/js/Pages/Maintance.jsx diff --git a/app/Http/Controllers/Api/RoleController.php b/app/Http/Controllers/Api/RoleController.php new file mode 100644 index 0000000..0554a3f --- /dev/null +++ b/app/Http/Controllers/Api/RoleController.php @@ -0,0 +1,21 @@ +q) { + $query->where('name', 'like', "%{$request->q}%"); + } + + return $query->get(); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/GeneralController.php b/app/Http/Controllers/GeneralController.php new file mode 100644 index 0000000..8a24598 --- /dev/null +++ b/app/Http/Controllers/GeneralController.php @@ -0,0 +1,18 @@ +user()->allow('view-role', true); + + $query = Role::query(); + + if ($request->q) { + $query->where('name', 'like', "%{$request->q}%"); + } + + $query->orderBy('created_at', 'desc'); + + return inertia('Role/Index', [ + 'data' => $query->paginate(10), + ]); + } + + public function create(): Response + { + return inertia('Role/Form', [ + 'permissions' => Permission::all(), + ]); + } + + public function store(Request $request): RedirectResponse + { + $request->validate([ + 'name' => 'string|required|max:255', + 'permissions' => 'array|required', + 'permissions.*.id' => 'uuid|required|exists:permissions,id', + ]); + + DB::beginTransaction(); + $role = Role::create(['name' => $request->name]); + foreach ($request->permissions as $permission) { + RolePermission::create([ + 'role_id' => $role->id, + 'permission_id' => $permission['id'], + ]); + } + DB::commit(); + + return redirect()->route('roles.index') + ->with('message', ['type' => 'success', 'message' => 'Item has beed saved']); + } + + public function edit(Role $role): Response + { + return inertia('Role/Form', [ + 'role' => $role->load(['permissions']), + 'permissions' => Permission::all(), + ]); + } + + public function update(Request $request, Role $role): RedirectResponse + { + $request->validate([ + 'name' => 'string|required|max:255', + 'permissions' => 'array|required', + 'permissions.*.id' => 'uuid|required|exists:permissions,id', + ]); + + if ($role->flag == 1) { + return redirect()->route('roles.index') + ->with('message', ['type' => 'error', 'message' => 'Item default can\'t updated']); + } + + DB::beginTransaction(); + $role->update([ + 'name' => $request->name, + ]); + + RolePermission::where('role_id', $role->id)->delete(); + + foreach ($request->permissions as $permission) { + RolePermission::create([ + 'role_id' => $role->id, + 'permission_id' => $permission['id'], + ]); + } + DB::commit(); + + return redirect()->route('roles.index') + ->with('message', ['type' => 'success', 'message' => 'Item has beed updated']); + } + + public function destroy(Role $role): RedirectResponse + { + $deleted = $role->delete(); + + if ($deleted) { + return redirect()->route('roles.index') + ->with('message', ['type' => 'success', 'message' => 'Item has beed deleted']); + } + + return redirect()->route('roles.index') + ->with('message', ['type' => 'error', 'message' => 'Item default can\'t deleted']); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php new file mode 100644 index 0000000..b176634 --- /dev/null +++ b/app/Http/Controllers/UserController.php @@ -0,0 +1,89 @@ +with(['role']); + + if ($request->q) { + $query->where('name', 'like', "%{$request->q}%"); + } + + $query->orderBy('created_at', 'desc'); + + return inertia('User/Index', [ + 'data' => $query->paginate(10), + ]); + } + + public function store(Request $request): RedirectResponse + { + $request->validate([ + 'name' => 'required|string|max:255', + 'email' => 'required|email|unique:users,email', + 'password' => 'required|string|max:255', + 'role_id' => 'required|uuid|exists:roles,id', + ]); + + User::create([ + 'name' => $request->name, + 'email' => $request->email, + 'password' => bcrypt($request->password), + 'role_id' => $request->role_id, + ]); + + return redirect()->route('user.index') + ->with('message', ['type' => 'success', 'message' => 'Item has beed saved']); + } + + public function update(Request $request, User $user): RedirectResponse + { + $request->validate([ + 'name' => 'required|string|max:255', + 'email' => 'required|email|unique:users,email,'.$user->id, + 'password' => 'nullable|string|max:255', + ]); + + if ($user->role != null) { + $request->validate([ + 'role_id' => 'required|uuid|exists:roles,id', + ]); + } + + $user->fill([ + 'email' => $request->email, + 'name' => $request->name, + 'role_id' => $request->role_id, + ]); + + if ($request->password != '') { + $user->password = bcrypt($request->password); + } + + $user->save(); + + return redirect()->route('user.index') + ->with('message', ['type' => 'success', 'message' => 'Item has beed updated']); + } + + public function destroy(User $user): RedirectResponse + { + if ($user->role_id == null) { + return redirect()->route('user.index') + ->with('message', ['type' => 'error', 'message' => 'Item default can\'t deleted']); + } + + $user->delete(); + + return redirect()->route('user.index') + ->with('message', ['type' => 'success', 'message' => 'Item has beed deleted']); + } +} \ No newline at end of file diff --git a/resources/js/Pages/Maintance.jsx b/resources/js/Pages/Maintance.jsx new file mode 100644 index 0000000..56a5c91 --- /dev/null +++ b/resources/js/Pages/Maintance.jsx @@ -0,0 +1,27 @@ +import React from 'react'; +import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout'; +import { Head } from '@inertiajs/react'; +import { HiFire } from 'react-icons/hi'; + +export default function Maintance(props) { + return ( + + + +
+
+
+ +
Fitur Dalam Pengembangan
+
+
+
+
+ ); +} \ No newline at end of file