-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.class.php
96 lines (77 loc) · 2.74 KB
/
order.class.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
<?php
declare(strict_types = 1);
class Order {
public int $id;
public string $status;
public int $restaurant;
public int $userid;
public int $quantity;
public function __construct(int $id, string $status, int $restaurant, int $userid, int $quantity) {
$this->id = $id;
$this->status = $status;
$this->restaurant = $restaurant;
$this->userid = $userid;
$this->quantity = $quantity;
}
static function countOrders(PDO $db){
$stmt = $db->prepare('
SELECT MAX(OrderId)
FROM Orders
');
$stmt->execute(array());
$max = $stmt->fetch();
return $max['MAX(OrderId)'];
}
static function addDishOrder(PDO $db, int $dishid, int $orderid) {
$stmt = $db->prepare('INSERT INTO DishOrder(DishId, OrderId) VALUES (?, ?)');
$stmt->execute(array($dishid, $orderid));
}
static function addOrder(PDO $db, int $id, string $status, int $restaurantid, int $userid, int $quantity, int $dishid){
$stmt = $db->prepare('INSERT INTO Orders(OrderId, OrderStatus, RestaurantId, UserId, Quantity) VALUES (?, ?, ?, ?, ?)');
$stmt->execute(array($id, $status, $restaurantid, $userid, $quantity));
Order::addDishOrder($db, $dishid, $id);
}
static function removeOrder(PDO $db, int $id){
$stmt = $db->prepare('DELETE FROM Orders WHERE OrderId = ?');
$stmt->execute(array($id));
$stmt = $db->prepare('DELETE FROM DishOrder WHERE OrderId = ?');
$stmt->execute(array($id));
}
static function getOrders(PDO $db, int $count) : array {
$stmt = $db->prepare('SELECT OrderId, OrderStatus, RestaurantId, UserId, Quantity FROM Orders LIMIT ?');
$stmt->execute(array($count));
$orders = [];
while ($order = $stmt->fetch()) {
$orders[] = new Order(
intval($order['OrderId']),
$order['OrderStatus'],
intval($order['RestaurantId']),
intval($order['UserId']),
intval($order['Quantity'])
);
}
return $orders;
}
static function updateOrderStatus(PDO $db, $status, $id){
$stmt = $db->prepare('UPDATE Orders SET OrderStatus = ? WHERE OrderId = ?');
$stmt->execute(array($status,$id));
}
static function getDishOrder(PDO $db, int $id){
$stmt = $db->prepare('
SELECT DishId, DishName, DishDescription, Price, MenuId, DishPhoto
FROM Dish natural join DishOrder natural join Orders
WHERE OrderId = ?
');
$stmt->execute(array($id));
$Dish = $stmt->fetch();
return $Dish = new Dish(
intval($Dish['DishId']),
$Dish['DishName'],
$Dish['DishDescription'],
floatval($Dish['Price']),
intval($Dish['MenuId']),
$Dish['DishPhoto']
);
}
}
?>