-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
216 lines (176 loc) · 5.47 KB
/
api.php
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
# Errors
error_reporting(E_ALL);
ini_set('display_errors', 1);
# Headers setup
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Methods: *");
header('Content-Type: application/json; charset=utf-8');
# Composer & vendor
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require __DIR__ . '/vendor/autoload.php';
}
# SQLite database 'app.db'
try {
$conn = new PDO('sqlite:app.db');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (\Exception $e) {
echo "Database Error: " . $e->getMessage();
}
# Namespaces
use Ramsey\Uuid\Uuid;
# GET | POST | PUT | DELETE
$method = $_SERVER['REQUEST_METHOD'];
switch($method) {
case "GET":
echo json_encode(getUsers());
break;
case "POST":
echo json_encode(insertUser());
break;
case "PUT":
echo json_encode(updateUser());
break;
case "DELETE":
echo json_encode(deleteUser());
break;
default :
$response = [
'status' => 418,
'statusText' => 'Default setting, I am a teapot',
'payload' => [],
];
echo json_encode($response);
}
# App codebase
/**
* Get all users / Get user by id in URL
*/
function getUsers(): array {
global $conn;
$sql = "SELECT * FROM users WHERE deleted_at IS NULL";
$path = explode('/', $_SERVER['REQUEST_URI']);
if (
isset($path[2])
&& is_numeric($path[2])
) {
# By id
$sql .= " AND id = :id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $path[2]);
$stmt->execute();
$users = $stmt->fetch(PDO::FETCH_ASSOC);
} else {
# All
$stmt = $conn->prepare($sql);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
return [
'status' => 200,
'statusText' => '',
'payload' => $users,
];
}
/**
* Insert user
*/
function insertUser(): array {
global $conn;
$user = json_decode(file_get_contents('php://input'));
# The RETURNING syntax has been supported by SQLite since version 3.35.0
# $sql = "INSERT INTO users(id, name, email, phone, token, created_at) VALUES(null, :name, :email, :phone, :token, :created_at) RETURNING *";
$sql = "INSERT INTO users(id, name, email, phone, password, token, created_at) VALUES(null, :name, :email, :phone, :password, :token, :created_at)";
$token = Uuid::uuid7()->__toString();
$date = date('Y-m-d');
$stmt = $conn->prepare($sql);
$stmt->bindParam(':name', $user->name);
$stmt->bindParam(':email', $user->email);
$stmt->bindParam(':phone', $user->phone);
$stmt->bindParam(':token', $token);
$stmt->bindParam(':password', $user->password);
$stmt->bindParam(':created_at', $date);
if ($stmt->execute()) {
$sql = "SELECT * FROM users WHERE id = (SELECT last_insert_rowid()) ";
$stmt = $conn->prepare($sql);
$stmt->execute();
$response = [
'status' => 201,
'statusText' => 'Record created successfully.',
'payload' => $stmt->fetch(PDO::FETCH_ASSOC),
];
} else {
$response = [
'status' => 409,
'statusText' => 'Failed to create record.',
'payload' => [],
];
}
return $response;
}
/**
* Update user
*/
function updateUser(): array {
global $conn;
$user = json_decode( file_get_contents('php://input') );
# The RETURNING syntax has been supported by SQLite since version 3.35.0
# $sql = "UPDATE users SET name= :name, email =:email, phone =:phone, updated_at =:updated_at WHERE id = :id RETURNING *";
$sql = "UPDATE users SET name= :name, email =:email, phone =:phone, updated_at =:updated_at WHERE id = :id";
$path = explode('/', $_SERVER['REQUEST_URI']);
$updated_at = date('Y-m-d');
$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $path[2]);
$stmt->bindParam(':name', $user->name);
$stmt->bindParam(':email', $user->email);
$stmt->bindParam(':phone', $user->phone);
$stmt->bindParam(':updated_at', $updated_at);
if ($stmt->execute()) {
$sql = "SELECT * FROM users WHERE id = :id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $path[2]);
$stmt->execute();
$response = [
'status' => 200,
'statusText' => 'Record updated successfully.',
'payload' => $stmt->fetch(PDO::FETCH_ASSOC),
];
} else {
$response = [
'status' => 400,
'statusText' => 'Failed to update record.',
'payload' => [],
];
}
return $response;
}
/**
* Delete user - id in URL
*/
function deleteUser(): array {
global $conn;
# Hard delete
# $sql = "DELETE FROM users WHERE id = :id";
# Soft delete
$sql = "UPDATE users SET deleted_at =:deleted_at WHERE id = :id";
$path = explode('/', $_SERVER['REQUEST_URI']);
$deleted_at = date('Y-m-d');
$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $path[2]);
$stmt->bindParam(':deleted_at', $deleted_at);
if ($stmt->execute()) {
$response = [
'status' => 200,
'statusText' => 'Record deleted successfully.',
'payload' => [],
];
} else {
$response = [
'status' => 409,
'statusText' => 'Failed to delete record.',
'payload' => [],
];
}
return $response;
}