-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTodoController.php
More file actions
129 lines (77 loc) · 2.73 KB
/
TodoController.php
File metadata and controls
129 lines (77 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace App\Controller;
use App\{Model\Todo,Helper\AuthHelper};
use Core\View;
class TodoController extends \Core\Controller {
const defaultMethod = 'listing';
public function initialize() {
AuthHelper::getInstance()->authAreaControl();
}
public function listing() {
$modelTodo = new Todo();
$todos = $modelTodo->getTodosByUserId(AuthHelper::getInstance()->get('user'));
View::render('todo-listing.php',['todos'=>$todos]);
}
public function add() {
View::render('todo-add.php');
}
public function edit() {
if(!isset($this->params[0]))
header('Location: ../todo/listing');
$modelTodo = new Todo();
$todo = $modelTodo->getTodoById((int)$this->params[0]);
if($todo==false)
header('Location: ../todo/listing');
View::render('todo-edit.php',['todo'=>$todo]);
}
public function addAction() {
header('Content-Type: application/json');
$response = ['status' => false];
try {
if (empty($_POST) || !isset($_POST['task']))
throw new \BadMethodCallException(__METHOD__ . ': eksik parametre');
$_POST['user_id'] = AuthHelper::getInstance()->get('user');
$modelTodo = new Todo;
$modelTodo -> addTodo($_POST);
$response['status'] = true;
} catch(\Exception | \Error $e) {
$response['error'] = $e -> getMessage();
}
echo json_encode($response);
}
public function editAction() {
header('Content-Type: application/json');
$response = ['status' => false];
try {
if (empty($_POST) || !isset($_POST['id']))
throw new \BadMethodCallException(__METHOD__ . ': eksik parametre');
$modelTodo = new Todo;
$isUserTodo = $modelTodo->getTodoById($_POST['id']);
if($isUserTodo==false || $isUserTodo['user_id']!=AuthHelper::getInstance()->get('user'))
throw new \Exception(__METHOD__ . ': yetki sorunu');
$modelTodo -> updateTodo($isUserTodo['id'],$_POST);
$response['status'] = true;
} catch(\Exception | \Error $e) {
$response['error'] = $e -> getMessage();
}
echo json_encode($response);
}
public function removeAction() {
header('Content-Type: application/json');
$response = ['status' => false];
try {
if(!isset($this->params[0]))
throw new \BadMethodCallException(__METHOD__ . ': eksik parametre');
$todoId = (int)$this->params[0];
$modelTodo = new Todo;
$isUserTodo = $modelTodo->getTodoById($todoId);
if($isUserTodo==false || $isUserTodo['user_id']!=AuthHelper::getInstance()->get('user'))
throw new \Exception(__METHOD__ . ': yetki sorunu');
$modelTodo -> removeTodoById($todoId);
$response['status'] = true;
} catch(\Exception | \Error $e) {
$response['error'] = $e -> getMessage();
}
echo json_encode($response);
}
}