mirror of
https://github.com/supanadit/todo.git
synced 2024-11-22 01:56:21 +00:00
Update migration
This commit is contained in:
parent
bf08bdc026
commit
981fc35917
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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');
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
@section('top-button')
|
||||
<button class="btn btn-info btn-sm" data-toggle="modal" data-target="#modal-default">
|
||||
Create Directory
|
||||
Create Todo
|
||||
</button>
|
||||
@endsection
|
||||
|
||||
|
@ -51,9 +51,11 @@
|
||||
<!-- Collect the nav links, forms, and other content for toggling -->
|
||||
<div class="collapse navbar-collapse pull-left" id="navbar-collapse">
|
||||
<ul class="nav navbar-nav">
|
||||
<li><a href="/home"><i class="fa fa-home"></i> Home</a></li>
|
||||
<li><a href="#" class="logout-button visible-xs"><i class="fa fa-sign-out"></i> Sign
|
||||
Out</a>
|
||||
<li><a href="/home">Home</a></li>
|
||||
<li>
|
||||
<a href="#" class="logout-button visible-xs">
|
||||
Sign Out
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
@ -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");
|
||||
|
Loading…
Reference in New Issue
Block a user