-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.php
executable file
·324 lines (274 loc) · 9.51 KB
/
handler.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
// error_reporting(-1);
// Connect to the database
$db_conn = new SQLite3('garage.db');
// Create the table if it doesn't exist
$sql = "CREATE TABLE IF NOT EXISTS cars (id INTEGER PRIMARY KEY AUTOINCREMENT, model TEXT, color TEXT, price INTEGER, brand TEXT, year INTEGER, fuel INTEGER, mileage INTEGER, status INTEGER, purchase_date INTEGER, vin TEXT, primary_driver TEXT)";
$db_conn->exec($sql);
// POST implies creating a new car
if (isset($_POST['submit'])) {
// convert the following to uppercase
$model = (string) strtoupper($_POST['model']);
$brand = (string) strtoupper($_POST['brand']);
$color = (string) strtoupper($_POST['color']);
$vin = (string) strtoupper($_POST['vin']);
$primary_driver = (string) strtoupper($_POST['primary_driver']) || null;
// Convert the price to an integer and round to the nearest $500
$price = (int) $_POST['price'];
$price = round($price / 500) * 500;
$year = (int) $_POST['year'];
$fuel = (int) $_POST['fuel']; // 1 Gasoline, 2 Diesel, 3 Electric, 4 Hybrid
$mileage = (int) $_POST['mileage'];
$status = (int) $_POST['status'];
$purchase_date = (int) $_POST['purchase_date'];
// Clean the input
$model = $db_conn->escapeString($model) || null;
$brand = $db_conn->escapeString($brand) || null;
$color = $db_conn->escapeString($color) || null;
$price = $db_conn->escapeString($price) || null;
$year = $db_conn->escapeString($year) || null;
$fuel = $db_conn->escapeString($fuel) || null;
$mileage = $db_conn->escapeString($mileage) || null;
$status = $db_conn->escapeString($status) || null;
$purchase_date = $db_conn->escapeString($purchase_date) || null;
$vin = $db_conn->escapeString($vin) || null;
$primary_driver = $db_conn->escapeString($primary_driver) || null;
// Create a new car
$newCar = new Car($model, $color, $price, $brand, $year, $fuel, $mileage, $status, $purchase_date, $vin, $primary_driver);
}
// GET implies filtering the displayed cars (so get all cars and then filter)
// By default, return all cars as JSON
if(isset($_GET['filter'])) {
$filter = $_GET['filter'];
// if filterType is not set, default to EXACT
$filterType = $_GET['filterType'] || 'EXACT';
// explode the filter string into an array
$filter = explode(",", $filter);
$cars = Car::getAllCars();
$filteredCars = array();
// compare the filters to each car and return the cars that match according to the filterType
foreach ($cars as $car) {
if($filterType === 'EXACT') {
if (in_array($car->getbrand(), $filter) && in_array($car->getfuel(), $filter) && in_array($car->getstatus(), $filter)) {
array_push($filteredCars, $car);
}
} else if ($filterType === 'ANY') {
if (in_array($car->getbrand(), $filter) || in_array($car->getfuel(), $filter) || in_array($car->getstatus(), $filter)) {
array_push($filteredCars, $car);
}
}
}
return $filteredCars;
} else {
$cars = Car::getAllCars();
// header('Content-Type: application/json');
echo json_encode($cars);
}
// DELETE implies removing a car
if($_SERVER['REQUEST_METHOD'] === 'DELETE') {
$id = $_GET['id'];
$cars = Car::deleteCar($id);
header('http 1.1 999 Car deleted');
echo json_encode($cars);
die(True);
}
class Car {
public $model;
public $color;
public $price;
public $brand;
public $year;
public $fuel;
public $mileage;
public $status;
public $purchase_date;
public $vin;
public $primary_driver;
public $id;
public function __construct($model="NA", $color="NA", $price="0", $brand="NA", $year="0000", $fuel="5", $mileage, $status, $purchase_date, $vin, $primary_driver) {
$this->model = $model;
$this->color = $color;
$this->price = $price;
$this->brand = $brand;
$this->year = $year;
$this->fuel = $fuel;
$this->mileage = $mileage;
$this->status = $status;
$this->purchase_date = $purchase_date;
$this->id = $this->create( $model, $color, $price, $brand, $year, $fuel, $mileage, $status, $purchase_date, $vin, $primary_driver);
}
// Create, Read, Update, Delete (CRUD) methods
// MODEL: string type
public function getmodel() {
return $this->model;
}
public function setmodel( $model ) {
$this->model = $model;
$db_conn = new SQLite3('garage.db');
$sql = "UPDATE cars SET model = '$model' WHERE id = $this->id";
$db_conn->exec($sql);
}
// COLOR: string type
public function getcolor() {
return $this->color;
}
public function setcolor( $color ) {
$this->color = $color;
$db_conn = new SQLite3('garage.db');
$sql = "UPDATE cars SET color = '$color' WHERE id = $this->id";
$db_conn->exec($sql);
}
// PRICE: integer type: rounded to the nearest $500
public function getprice() {
return $this->price;
}
public function setprice( $price ) {
$this->price = $price;
$db_conn = new SQLite3('garage.db');
$sql = "UPDATE cars SET price = $price WHERE id = $this->id";
$db_conn->exec($sql);
}
// BRAND: string type
public function getbrand() {
return $this->brand;
}
public function setbrand( $brand ) {
$this->brand = $brand;
$db_conn = new SQLite3('garage.db');
$sql = "UPDATE cars SET brand = '$brand' WHERE id = $this->id";
$db_conn->exec($sql);
}
// YEAR: integer type
public function getyear() {
return $this->year;
}
public function setyear( $year ) {
$this->year = $year;
$db_conn = new SQLite3('garage.db');
$sql = "UPDATE cars SET year = $year WHERE id = $this->id";
$db_conn->exec($sql);
}
// FUEL: integer type: 1 Gasoline, 2 Diesel, 3 Electric, 4 Hybrid
public function getfuel() {
return $this->fuel;
}
public function setfuel( $fuel ) {
$this->fuel = $fuel;
$db_conn = new SQLite3('garage.db');
$sql = "UPDATE cars SET fuel = $fuel WHERE id = $this->id";
$db_conn->exec($sql);
}
// MILEAGE: integer type
public function getmileage() {
return $this->mileage;
}
public function setmileage( $mileage ) {
$this->mileage = $mileage;
$db_conn = new SQLite3('garage.db');
$sql = "UPDATE cars SET mileage = $mileage WHERE id = $this->id";
$db_conn->exec($sql);
}
// STATUS: binary type: 1 ready, 0 out of service
public function getstatus() {
return $this->status;
}
public function setstatus( $status ) {
$this->status = $status;
$db_conn = new SQLite3('garage.db');
$sql = "UPDATE cars SET status = $status WHERE id = $this->id";
$db_conn->exec($sql);
}
public function removestatus() {
$this->status = 0;
$db_conn = new SQLite3('garage.db');
$sql = "UPDATE cars SET status = 0 WHERE id = $this->id";
$db_conn->exec($sql);
}
// PURCHASE_DATE: integer type
public function getpurchase_date() {
return $this->purchase_date;
}
public function setpurchase_date( $purchase_date ) {
$this->purchase_date = $purchase_date;
$db_conn = new SQLite3('garage.db');
$sql = "UPDATE cars SET purchase_date = $purchase_date WHERE id = $this->id";
$db_conn->exec($sql);
}
// VIN: string type
public function getvin() {
return $this->vin;
}
public function setvin( $vin ) {
$this->vin = $vin;
$db_conn = new SQLite3('garage.db');
$sql = "UPDATE cars SET vin = '$vin' WHERE id = $this->id";
$db_conn->exec($sql);
}
// CREATE (and return the id of the new car)
public function create( $model, $color, $price, $brand, $year, $fuel, $mileage, $status, $purchase_date, $vin, $primary_driver) {
$this->model = $model;
$this->color = $color;
$this->price = $price;
$this->brand = $brand;
$this->year = $year;
$this->fuel = $fuel;
$this->mileage = $mileage;
$this->status = $status;
$this->purchase_date = $purchase_date;
$this->vin = $vin;
$this->primary_driver = $primary_driver;
// Save the new car to the database
$db_conn = new SQLite3('garage.db');
// Clean the input
$model = $db_conn->escapeString($model) || null;
$brand = $db_conn->escapeString($brand) || null;
$color = $db_conn->escapeString($color) || null;
$price = $db_conn->escapeString($price) || null;
$year = $db_conn->escapeString($year) || null;
$fuel = $db_conn->escapeString($fuel) || null;
$mileage = $db_conn->escapeString($mileage) || null;
$status = $db_conn->escapeString($status) || null;
$purchase_date = $db_conn->escapeString($purchase_date) || null;
$vin = $db_conn->escapeString($vin) || null;
$primary_driver = $db_conn->escapeString($primary_driver) || null;
$sql = "INSERT INTO cars (model, color, price, brand, year, fuel, mileage, status, purchase_date, vin, primary_driver) VALUES ('$model', '$color', '$price', '$brand', '$year', '$fuel', '$mileage', '$status', '$purchase_date', '$vin', '$primary_driver')";
$db_conn->exec($sql);
// Get the id of the new car and return it
$id = $db_conn->lastInsertRowID();
return $id;
}
// READ
public static function getCar( $id ) {
$db_conn = new SQLite3('garage.db');
$sql = "SELECT * FROM cars WHERE id = $id";
$result = $db_conn->query($sql);
$car = $result->fetchArray();
return $car;
}
// READ ALL
public static function getAllCars() {
$db_conn = new SQLite3('garage.db');
$sql = "SELECT * FROM cars";
$result = $db_conn->query($sql);
$cars = array();
while ($row = $result->fetchArray()) {
$car = array("model"=>$row['model'],"color"=>$row['color'],"price"=>$row['price'],"brand"=>$row['brand'],"year"=>$row['year'],"fuel"=>$row['fuel'],"mileage"=>$row['mileage'],"status"=>$row['status'],"purchase_date"=>$row['purchase_date'],"id"=>$row['id'], "vin"=>$row['vin']);
array_push($cars, $car);
}
return $cars;
}
// DELETE
public static function deleteCar( $id ) {
$db_conn = new SQLite3('garage.db');
$sql = "DELETE FROM cars WHERE id = $id";
try {
$db_conn->exec($sql);
return True;
} catch (Exception $e) {
echo $e;
return False;
}
}
}