-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavorites.class.php
115 lines (87 loc) · 3.28 KB
/
favorites.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types = 1);
class Favorites {
public int $favid;
public int $userid;
public int $rid;
public int $did;
public function __construct(int $favid, int $userid, int $rid, int $did)
{
$this->favid = $favid;
$this->userid = $userid;
$this->rid = $rid;
$this->did = $did;
}
static function countFavorites(PDO $db){
$stmt = $db->prepare('SELECT MAX(FavId) FROM Favorites');
$stmt->execute(array());
$max = $stmt->fetch();
return $max['MAX(FavId)'];
}
static function addFavoriteR(PDO $db, int $userid, int $id){
$stmt = $db->prepare('INSERT INTO Favorites (FavId, UserId, RestaurantId) VALUES (?,?,?)');
$stmt->execute(array(Favorites::countFavorites($db)+1,$userid,$id));
}
static function addFavoriteD(PDO $db, int $userid, int $rid, int $id){
$stmt = $db->prepare('INSERT INTO Favorites (FavId, UserId, RestaurantId, DishId) VALUES (?,?,?,?)');
$stmt->execute(array(Favorites::countFavorites($db)+1,$userid,$rid,$id));
}
static function removeFavoriteR(PDO $db, int $userid, int $id){
$stmt = $db->prepare('DELETE FROM Favorites WHERE UserId = ? AND RestaurantId = ? AND DishId = 0');
$stmt->execute(array($userid,$id));
}
static function removeFavoriteD(PDO $db, int $userid, int $rid, int $id){
$stmt = $db->prepare('DELETE FROM Favorites WHERE UserId = ? AND RestaurantId = ? AND DishId = ?');
$stmt->execute(array($userid,$rid,$id));
}
static function getFavoriteR(PDO $db, int $id) : array {
$stmt = $db->prepare('SELECT RestaurantId FROM Favorites WHERE UserId = ? AND DishId = 0');
$stmt->execute(array($id));
$Favorites = [];
while($fav = $stmt->fetch()){
$Favorites[] = intval($fav['RestaurantId']);
}
return $Favorites;
}
static function getFavoriteD(PDO $db, int $id) : array {
$stmt = $db->prepare('SELECT FavId, UserId, RestaurantId, DishId FROM Favorites WHERE UserId = ? AND DishId <> 0');
$stmt->execute(array($id));
$Favorites = [];
while($fav = $stmt->fetch()){
array_push($Favorites, intval($fav['DishId']));
}
return $Favorites;
}
static function verifyFavoriteR(PDO $db, int $id){
$stmt = $db->prepare('SELECT MAX(RestaurantId) FROM Favorites WHERE UserId = ? AND DishId = 0');
$stmt->execute(array($id));
$max = $stmt->fetch();
if($max['MAX(RestaurantId)'] == 0){
return False;
}
return True;
}
static function verifyFavoriteD(PDO $db, int $id){
$stmt = $db->prepare('SELECT MAX(DishId) FROM Favorites WHERE UserId = ? AND DishId <> 0');
$stmt->execute(array($id));
$max = $stmt->fetch();
if($max['MAX(DishId)'] == 0){
return False;
}
return True;
}
static function getFavorites(PDO $db, int $id) : array {
$stmt = $db->prepare('SELECT * FROM Favorites WHERE UserId = ?');
$stmt->execute(array($id));
$Favorites = [];
while($fav = $stmt->fetch()){
$Favorites[] = new Favorites(
intval($fav['FavId']),
intval($fav['UserId']),
intval($fav['RestaurantId']),
intval($fav['DishId'])
);
}
return $Favorites;
}
}