-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
106 lines (86 loc) · 3.02 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
<?php
/**
* Created by PhpStorm.
* User: HoangAnhKhoa
* Date: 4/13/16
* Time: 4:51 PM
*/
include 'database.php';
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
$query = "SELECT * FROM products";
$result = $con->query($query)->fetchAll(PDO::FETCH_ASSOC);
$return = [];
foreach ($result as $row) {
$return[] = [
'id' => $row['id'],
'name' => $row['name'],
'description' => $row['description'],
'price' => $row['price']
];
}
header('Content-type: application/json');
print json_encode($return);
break;
case 'PUT':
// $pathSegments = explode('/', $_SERVER['REQUEST_URI']);
// $id = $pathSegments[2];
$entityBody = file_get_contents('php://input');
$array = json_decode($entityBody, true);
$query = "UPDATE products
SET name=:name, description=:description, price=:price
WHERE id = :id";
$array_updated = [];
foreach ($array as $value) {
$stmt = $con->prepare($query);
$stmt->bindParam(':name', $value['name']);
$stmt->bindParam(':description', $value['description']);
$stmt->bindParam(':price', $value['price']);
$stmt->bindParam(':id', $value['id']);
if ($stmt->execute()) {
$array_updated[] = $value;
}
}
header('Content-type: application/json');
print json_encode($array_updated);
break;
case 'DELETE':
$pathSegments = explode('/', $_SERVER['REQUEST_URI']);
$id = $pathSegments[2];
$query = "DELETE FROM products WHERE id = ?";
$stmt = $con->prepare($query);
$stmt->bindParam(1, $id);
$array_deleted = [];
if ($stmt->execute()) {
$array_deleted[] = $id;
}
print json_encode($array_deleted);
break;
case 'POST':
$entityBody = file_get_contents('php://input');
$array = json_decode($entityBody, true);
$query = "INSERT INTO products SET name=:name, description=:description, price=:price, created=:created";
// prepare query for execution
$stmt = $con->prepare($query);
// bind the parameters
$array_added = [];
foreach ($array as $value) {
$stmt = $con->prepare($query);
$stmt->bindParam(':name', $value['name']);
$stmt->bindParam(':description', $value['description']);
$stmt->bindParam(':price', $value['price']);
$created = date('Y-m-d H:i:s');
$stmt->bindParam(':created', $created);
if ($stmt->execute()) {
$value['id'] = $con->lastInsertId();
$array_added[] = $value;
}
}
print json_encode($array_added);
break;
default:
header('HTTP/1.1 405 Method Not Allowed');
header('Allow: GET, PUT, DELETE');
break;
}