-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
202 lines (172 loc) · 6.59 KB
/
api.php
File metadata and controls
202 lines (172 loc) · 6.59 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
// api.php - Main API File for TaskFlow
// Save this as: api.php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
require_once 'config.php';
// Get request method
$method = $_SERVER['REQUEST_METHOD'];
// Get the endpoint
$request = isset($_GET['request']) ? $_GET['request'] : '';
// Database connection
$database = new Database();
$db = $database->getConnection();
// Response array
$response = array();
// ============ HANDLE API REQUESTS ============
switch($method) {
case 'GET':
if ($request === 'tasks') {
// Get all tasks
$query = "SELECT * FROM tasks ORDER BY created_at DESC";
$stmt = $db->prepare($query);
$stmt->execute();
$tasks = $stmt->fetchAll(PDO::FETCH_ASSOC);
$response = array(
"success" => true,
"tasks" => $tasks
);
}
elseif (preg_match('/^tasks\/(\d+)$/', $request, $matches)) {
// Get single task by ID
$id = $matches[1];
$query = "SELECT * FROM tasks WHERE id = :id";
$stmt = $db->prepare($query);
$stmt->bindParam(":id", $id);
$stmt->execute();
$task = $stmt->fetch(PDO::FETCH_ASSOC);
if ($task) {
$response = array(
"success" => true,
"task" => $task
);
} else {
http_response_code(404);
$response = array(
"success" => false,
"message" => "Task not found"
);
}
}
break;
case 'POST':
if ($request === 'tasks') {
// Create new task
$data = json_decode(file_get_contents("php://input"));
if (!empty($data->title)) {
$query = "INSERT INTO tasks (title, priority, due_date, completed)
VALUES (:title, :priority, :due_date, :completed)";
$stmt = $db->prepare($query);
$title = htmlspecialchars(strip_tags($data->title));
$priority = isset($data->priority) ? $data->priority : 'medium';
$due_date = isset($data->due_date) ? $data->due_date : null;
$completed = isset($data->completed) ? $data->completed : 0;
$stmt->bindParam(":title", $title);
$stmt->bindParam(":priority", $priority);
$stmt->bindParam(":due_date", $due_date);
$stmt->bindParam(":completed", $completed);
if ($stmt->execute()) {
http_response_code(201);
$response = array(
"success" => true,
"message" => "Task created successfully",
"taskId" => $db->lastInsertId()
);
} else {
http_response_code(500);
$response = array(
"success" => false,
"message" => "Unable to create task"
);
}
} else {
http_response_code(400);
$response = array(
"success" => false,
"message" => "Title is required"
);
}
}
break;
case 'PUT':
if (preg_match('/^tasks\/(\d+)$/', $request, $matches)) {
// Update task
$id = $matches[1];
$data = json_decode(file_get_contents("php://input"));
$updates = array();
$params = array();
if (isset($data->title)) {
$updates[] = "title = :title";
$params[':title'] = htmlspecialchars(strip_tags($data->title));
}
if (isset($data->priority)) {
$updates[] = "priority = :priority";
$params[':priority'] = $data->priority;
}
if (isset($data->due_date)) {
$updates[] = "due_date = :due_date";
$params[':due_date'] = $data->due_date;
}
if (isset($data->completed)) {
$updates[] = "completed = :completed";
$params[':completed'] = $data->completed;
}
if (count($updates) > 0) {
$query = "UPDATE tasks SET " . implode(", ", $updates) . " WHERE id = :id";
$stmt = $db->prepare($query);
$params[':id'] = $id;
if ($stmt->execute($params)) {
$response = array(
"success" => true,
"message" => "Task updated successfully"
);
} else {
http_response_code(500);
$response = array(
"success" => false,
"message" => "Unable to update task"
);
}
} else {
http_response_code(400);
$response = array(
"success" => false,
"message" => "No fields to update"
);
}
}
break;
case 'DELETE':
if (preg_match('/^tasks\/(\d+)$/', $request, $matches)) {
// Delete task
$id = $matches[1];
$query = "DELETE FROM tasks WHERE id = :id";
$stmt = $db->prepare($query);
$stmt->bindParam(":id", $id);
if ($stmt->execute()) {
$response = array(
"success" => true,
"message" => "Task deleted successfully"
);
} else {
http_response_code(500);
$response = array(
"success" => false,
"message" => "Unable to delete task"
);
}
}
break;
default:
http_response_code(405);
$response = array(
"success" => false,
"message" => "Method not allowed"
);
break;
}
// Output response
echo json_encode($response);
?>