From 981fc3591742b15acf1ad7e17b31ceea5e6e4e74 Mon Sep 17 00:00:00 2001 From: Supan Adit Pratama Date: Wed, 24 Jun 2020 13:09:01 +0700 Subject: [PATCH] Update migration --- app/Http/Controllers/TodoController.php | 71 ++++++++++++++++++- .../2020_06_23_170808_create_todos_table.php | 4 +- resources/views/home.blade.php | 2 +- resources/views/layout/admin.blade.php | 8 ++- routes/web.php | 12 ++-- 5 files changed, 86 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/TodoController.php b/app/Http/Controllers/TodoController.php index d7475ea..1c03ff4 100755 --- a/app/Http/Controllers/TodoController.php +++ b/app/Http/Controllers/TodoController.php @@ -11,8 +11,77 @@ class TodoController extends Controller return view('home'); } - public function createTodo() + public function todoList(Request $request) { + return \App\Todo::where('user_id', $request->session()->get('user'))->get(); + } + public function todoManipulate(Request $request, $id) + { + $todo = new \App\Todo(); + if ($id != null) { + $todo = \App\Todo::find($id); + } + $todo['name'] = $request->input('name'); + $todo['description'] = $request->input('description'); + $todo['user_id'] = $request->session()->get('user'); + if ($todo->save()) { + return response()->json(array( + "message" => "Success " . ($id != null ? "edit" : "create") . " todo" + ), 200); + } else { + return response()->json(array( + "message" => "Failed create todo" + ), 500); + } + } + + public function todoCreate(Request $request) + { + $request->validate([ + "name" => "required", + ]); + return $this->todoManipulate($request, null); + } + + public function todoEdit(Request $request) + { + $request->validate([ + "name" => "required", + "id" => "required" + ]); + return $this->todoManipulate($request, $request->input('id')); + } + + public function todoView(Request $request, $id) + { + $model = \App\Todo::find($id); + if ($model == null) { + return response()->json(array( + "message" => "There's no todo with id $id" + ), 400); + } else { + return $model->with('todo_items'); + } + } + + public function todoDelete(Request $request, $id) + { + $model = \App\Todo::find($id); + if ($model == null) { + return response()->json(array( + "message" => "There's no todo with id $id" + ), 400); + } else { + if ($model->delete()) { + return response()->json(array( + "message" => "Success delete todo" + ), 200); + } else { + return response()->json(array( + "message" => "Failed delete todo" + ), 500); + } + } } } diff --git a/database/migrations/2020_06_23_170808_create_todos_table.php b/database/migrations/2020_06_23_170808_create_todos_table.php index b120ed8..767da8b 100644 --- a/database/migrations/2020_06_23_170808_create_todos_table.php +++ b/database/migrations/2020_06_23_170808_create_todos_table.php @@ -18,9 +18,9 @@ class CreateTodosTable extends Migration $table->timestamps(); $table->string("name"); - $table->text("Description"); + $table->text("description")->nullable(true); - $table->unsignedBigInteger('user_id')->default(false); + $table->unsignedBigInteger('user_id'); }); Schema::table('todos', function (Blueprint $table) { $table->foreign('user_id')->references('id')->on('users'); diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index 5d48a24..ee07cd6 100755 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -10,7 +10,7 @@ @section('top-button') @endsection diff --git a/resources/views/layout/admin.blade.php b/resources/views/layout/admin.blade.php index 77b7eb4..692c24d 100755 --- a/resources/views/layout/admin.blade.php +++ b/resources/views/layout/admin.blade.php @@ -51,9 +51,11 @@ diff --git a/routes/web.php b/routes/web.php index 88599e3..f6ea18b 100755 --- a/routes/web.php +++ b/routes/web.php @@ -18,11 +18,15 @@ Route::get('/login', "SecurityController@viewLogin")->name('login'); Route::middleware('auth.web')->group(function () { Route::get('/home', "TodoController@viewHome"); -}); - -// This group is used for internal API provided by session -Route::prefix('/web')->group(function () { + // This group is used for internal API provided by session + Route::prefix('/web')->group(function () { + Route::get('/todo', "TodoController@todoList"); + Route::post('/todo', "TodoController@todoCreate"); + Route::put('/todo', "TodoController@todoEdit"); + Route::get('/todo/{id}', "TodoController@todoView"); + Route::delete('/todo/{id}', "TodoController@todoDelete"); + }); }); Route::post('/web/login', "SecurityController@formLogin");