todo/app/Http/Controllers/TodoController.php

203 lines
5.9 KiB
PHP
Raw Permalink Normal View History

2020-06-23 04:40:37 +00:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TodoController extends Controller
{
public function viewHome()
{
return view('home');
}
2020-06-23 17:45:04 +00:00
2020-06-24 13:13:34 +00:00
// Todo
2020-06-24 06:09:01 +00:00
public function todoList(Request $request)
2020-06-23 17:45:04 +00:00
{
2020-06-24 13:13:34 +00:00
$search = $request->query('search');
$model = \App\Todo::with('todoItems')->where('user_id', $request->session()->get('user'));
if ($search != null && $search != "") {
$model = $model->where("name", "like", "%$search%")->orWhere("description", "like", "%$search%");
}
return $model->get();
2020-06-24 06:09:01 +00:00
}
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);
}
2020-06-23 17:45:04 +00:00
2020-06-24 06:09:01 +00:00
public function todoEdit(Request $request)
{
$request->validate([
"name" => "required",
"id" => "required"
]);
return $this->todoManipulate($request, $request->input('id'));
}
public function todoView(Request $request, $id)
{
2020-06-24 13:13:34 +00:00
$model = \App\Todo::with('todoItems')->find($id);
2020-06-24 06:09:01 +00:00
if ($model == null) {
return response()->json(array(
"message" => "There's no todo with id $id"
), 400);
} else {
2020-06-24 13:13:34 +00:00
return $model;
2020-06-24 06:09:01 +00:00
}
}
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);
}
}
2020-06-23 17:45:04 +00:00
}
2020-06-24 13:13:34 +00:00
// End Todo
// Todo Items
public function todoItemList(Request $request, $todo_id)
{
$search = $request->query('search');
$model = \App\TodoItem::where('todo_id', $todo_id);
if ($search != null && $search != "") {
$model = $model->where("name", "like", "%$search%");
}
return $model->get();
}
public function todoItemManipulate(Request $request, $id)
{
$todo = new \App\TodoItem();
if ($id != null) {
$todo = \App\TodoItem::find($id);
}
$todo['name'] = $request->input('name');
$todo['complete'] = ($request->input('complete') != null) ? $request->input('complete') : false;
$todo['todo_id'] = $request->input('todo_id');
if ($todo->save()) {
return response()->json(array(
"message" => "Success " . ($id != null ? "edit" : "create") . " todo item"
), 200);
} else {
return response()->json(array(
"message" => "Failed create todo item"
), 500);
}
}
public function todoItemCreate(Request $request)
{
$request->validate([
"name" => "required",
"todo_id" => "required",
]);
return $this->todoItemManipulate($request, null);
}
public function todoItemEdit(Request $request)
{
$request->validate([
"name" => "required",
"id" => "required",
"todo_id" => "required",
]);
return $this->todoItemManipulate($request, $request->input('id'));
}
public function todoItemView(Request $request, $id)
{
$model = \App\TodoItem::find($id);
if ($model == null) {
return response()->json(array(
"message" => "There's no todo item with id $id"
), 400);
} else {
return $model;
}
}
public function todoItemDelete(Request $request, $id)
{
$model = \App\TodoItem::find($id);
if ($model == null) {
return response()->json(array(
"message" => "There's no todo item with id $id"
), 400);
} else {
if ($model->delete()) {
return response()->json(array(
"message" => "Success delete todo item"
), 200);
} else {
return response()->json(array(
"message" => "Failed delete todo item"
), 500);
}
}
}
public function todoItemMark(Request $request, $id, $complete = true)
{
$todo = \App\TodoItem::find($id);
$todo['complete'] = $complete;
if ($todo->save()) {
return response()->json(array(
"message" => "Success mark " . ($complete ? "complete" : "not complete")
), 200);
} else {
return response()->json(array(
"message" => "Failed mark todo item"
), 500);
}
}
public function todoItemMarkComplete(Request $request, $id)
{
return $this->todoItemMark($request, $id, true);
}
public function todoItemMarkNotComplete(Request $request, $id)
{
return $this->todoItemMark($request, $id, false);
}
// End Todo Items
2020-06-23 04:40:37 +00:00
}