-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductController.php
119 lines (92 loc) · 3.32 KB
/
ProductController.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
<?php
class ProductController
{
public function index()
{
global $pdo;
$stmt = $pdo->query("SELECT * FROM `products_tbl`");
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($products);
}
public function store()
{
global $pdo;
$data = json_decode(file_get_contents("php://input"), true);
$stmt = $pdo->prepare("INSERT INTO `products_tbl` (`name`, `category`, `price`, `qty`)
VALUES (?, ?, ?, ?)");
$stmt->execute([$data["name"], $data["category"], $data["price"], $data["qty"]]);
echo json_encode(["message" => "Product successfully added!"]);
}
public function update($params)
{
global $pdo;
if (!isset($params['id'])) {
http_response_code(400);
echo json_encode(['error' => 'Invalid request']);
return;
}
$id = $params['id'];
// Validate that $id is a positive integer
if (!ctype_digit((string)$id) || $id <= 0) {
http_response_code(400);
echo json_encode(['error' => 'Invalid request']);
return;
}
$data = json_decode(file_get_contents("php://input"), true);
// Validate that required fields are present in the request
if (empty($data)) {
http_response_code(400);
echo json_encode(['error' => 'Invalid request. Request body is empty.']);
return;
}
$setClauses = [];
$paramsToBind = [];
foreach ($data as $key => $value) {
// Only include valid fields that can be updated
$allowedFields = ['qty'];
if (in_array($key, $allowedFields)) {
$setClauses[] = "$key = ?";
$paramsToBind[] = $value;
}
}
// If no valid fields are found for update
if (empty($setClauses)) {
http_response_code(400);
echo json_encode(['error' => 'Invalid request. No valid fields provided for update.']);
return;
}
// Prepare the SQL query dynamically based on the fields to update
$sql = "UPDATE `products_tbl` SET " . implode(', ', $setClauses) . " WHERE `id` = ?";
$paramsToBind[] = $id;
$stmt = $pdo->prepare($sql);
$stmt->execute($paramsToBind);
echo json_encode(['message' => 'Product successfully updated!']);
}
public function show($params)
{
global $pdo;
// Check if 'id' is present in the parameters
if (!isset($params['id'])) {
http_response_code(400);
echo json_encode(['error' => 'Invalid request']);
return;
}
$id = $params['id'];
// Validate that $id is a positive integer
if (!ctype_digit((string)$id) || $id <= 0) {
http_response_code(400);
echo json_encode(['error' => 'Invalid product ID']);
return;
}
$stmt = $pdo->prepare("SELECT * FROM `products_tbl` WHERE id = ?");
$stmt->execute([$id]);
$product = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$product) {
http_response_code(404);
echo json_encode(['error' => 'Product not found']);
return;
}
echo json_encode($product);
}
}
?>