-
Notifications
You must be signed in to change notification settings - Fork 0
/
CarController.php
150 lines (118 loc) · 4.23 KB
/
CarController.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
<?php
class CarController
{
public function index()
{
global $pdo;
$stmt = $pdo->query("SELECT * FROM cars");
$cars = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($cars);
}
public function store()
{
global $pdo;
$data = json_decode(file_get_contents("php://input"), true);
$stmt = $pdo->prepare("INSERT INTO cars (make, model, year, price, color, mileage, engine_type, transmission_type)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([$data["make"], $data["model"], $data["year"], $data["price"],
$data["color"], $data["mileage"], $data["engine_type"], $data["transmission_type"]]);
echo json_encode(["message" => "Car added successfully"]);
}
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 car ID']);
return;
}
$stmt = $pdo->prepare("SELECT * FROM cars WHERE id = ?");
$stmt->execute([$id]);
$car = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$car) {
http_response_code(404);
echo json_encode(['error' => 'Car not found']);
return;
}
echo json_encode($car);
}
/**
* Update details of a specific car by ID
*/
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 car ID']);
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 = ['make', 'model', 'year', 'price', 'color', 'mileage', 'engine_type', 'transmission_type'];
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 cars SET " . implode(', ', $setClauses) . " WHERE id = ?";
$paramsToBind[] = $id;
$stmt = $pdo->prepare($sql);
$stmt->execute($paramsToBind);
echo json_encode(['message' => 'Car updated successfully']);
}
/**
* Delete a specific car by ID
*/
public function destroy($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 car ID']);
return;
}
$stmt = $pdo->prepare("DELETE FROM cars WHERE id = ?");
$stmt->execute([$id]);
echo json_encode(['message' => 'Car deleted successfully']);
}
}
?>