-
Notifications
You must be signed in to change notification settings - Fork 0
/
products.php
74 lines (62 loc) · 2.27 KB
/
products.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
<?php
/* PRODUCTS */
// Import classes from the Psr library (standardised HTTP requests and responses)
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
function selectProductById ($conn, $id) {
$stmt = $conn->prepare("SELECT * FROM products WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$response = array();
while ($row = $result->fetch_assoc()) {
if ($row) {
$response[] = $row;
}
};
if (empty($response)) {
return null;
}
return $response;
}
$app->put('/products', function (Request $req, Response $res) use($conn) {
$post = $req->getParsedBody();
$stmt = $conn->prepare("INSERT into products (name, type, price, created_at, updated_at) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $post["name"], $post["type"], $post["price"], date("Y-m-d H:i:s"), date("Y-m-d H:i:s"));
$stmt->execute();
$stmt->close();
$stmt = $conn->prepare("SELECT * FROM products");
$stmt->execute();
$result = $stmt->get_result();
$products = array();
while($row = $result->fetch_assoc()) {
if($row) {
$products[] = $row;
}
else {
return $res->withJson(null);
}
};
$stmt->close();
return $res->withJson($products);
});
$app->post('/products', function (Request $req, Response $res) use($conn) {
$post = $req->getParsedBody();
// Fetch the product by id from the database
$response = selectProductById($conn, $post["id"]);
// If we're forcing the change then skip the check for updated content
if (!$post["force"] && $post["updated_at"]) {
if ($response) {
if ($post["updated_at"] < $response[0]['updated_at']) {
return $res->withStatus(429)->withJson($response);
}
} else {
return $res->withJson(null);
}
}
$stmt = $conn->prepare("UPDATE products SET name = ?, type = ?, price = ?, updated_at = ? WHERE id = ?");
$stmt->bind_param("ssssi", $post["name"], $post["type"], $post["price"], date("Y-m-d H:i:s"), $post["id"]);
$stmt->execute();
$stmt->close();
return $res->withJson(selectProductById($conn, $post["id"]));
});