-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseAdapter.php
More file actions
160 lines (139 loc) · 5.52 KB
/
Copy pathDatabaseAdapter.php
File metadata and controls
160 lines (139 loc) · 5.52 KB
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
<?php
class DatabaseAdapter
{
private $DB;
// The instance variable used in every method
// Connect to an existing data based named 'first'
public function __construct()
{
// $dataBase = 'mysql:dbname=imdb_small;charset=utf8;host=127.0.0.1';
$dataBase = 'mysql:dbname=online_recipes;charset=utf8;host=127.0.0.1';
$user = 'root';
$password = ''; // Empty string with XAMPP install
try {
$this->DB = new PDO($dataBase, $user, $password);
$this->DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo ('Error establishing Connection');
exit();
}
}
// . . . continued
public function getAllUsers()
{
$stmt = $this->DB->prepare("SELECT * FROM customers");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function getAllRecipes()
{
$stmt = $this->DB->prepare("SELECT * FROM recipes");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function getUserID($Name)
{
$stmt = $this->DB->prepare("SELECT ID FROM customers WHERE Username = '" . $Name . "'");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function getRecipeID($Name)
{
$stmt = $this->DB->prepare("SELECT ID FROM recipes WHERE Name = '" . $Name . "'");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function addRecipe($ID, $Name)
{
$stmt = $this->DB->prepare("INSERT INTO recipes values(" . $ID . ", '" . $Name . "', 0)");
$stmt->execute();
// return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function addFavorite($CustomerID, $RecipeID)
{
$stmt = $this->DB->prepare("UPDATE recipes SET Favorited = Favorited+1 WHERE ID = " . $RecipeID . "");
$stmt->execute();
$stmt = $this->DB->prepare("UPDATE recipes SET Favorited = Favorited+1 WHERE ID = " . $RecipeID . ";INSERT INTO favorites values (" . $CustomerID . ", " . $RecipeID . ")");
$stmt->execute();
// return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function removeFavorite($CustomerID, $RecipeID)
{
$stmt = $this->DB->prepare("UPDATE recipes SET Favorited = Favorited-1 WHERE ID = " . $RecipeID . "");
$stmt->execute();
$stmt = $this->DB->prepare("DELETE FROM favorites WHERE customerID = " . $CustomerID . " AND recipeID = " . $RecipeID . "");
$stmt->execute();
// return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function getFavorites($ID)
{
$stmt = $this->DB->prepare("SELECT recipes.Name FROM recipes JOIN favorites ON favorites.recipeID = recipes.ID JOIN customers on customers.ID = favorites.customerID WHERE customers.ID = " . $ID . "");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function countFavorites ($ID, $RecipeID){
$stmt = $this->DB->prepare( "select count(recipeID) from favorites where recipeID = ". $RecipeID ." AND customerID = ". $ID .";");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function FavoriteList()
{ // returns a list of recipes ordered by favorited
$stmt = $this->DB->prepare("SELECT * FROM recipes ORDER BY Favorited DESC;");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function numRecipes()
{
$stmt = $this->DB->prepare("select COUNT(*) FROM recipes;");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function logIn($email, $password)
{
$stmt = $this->DB->prepare("SELECT * FROM accounts WHERE email = '$email'");
$stmt->execute();
$stmt = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (password_verify($password, $stmt[0]['password']) == 1) {
$_SESSION['firstName'] = $stmt[0]['firstName'];
$_SESSION['lastName'] = $stmt[0]['lastName'];
$_SESSION['email'] = $stmt[0]['email'];
return TRUE;
} else {
session_destroy();
session_unset();
return FALSE;
}
}
// ????
public function validUser($userName)
{
$stmt = $this->DB->prepare("SELECT email FROM accounts WHERE email = :user ;");
$stmt->bindParam('user', $userName);
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (count($row) == 0)
return "true";
return "false";
}
public function numUsers()
{
$stmt = $this->DB->prepare("select COUNT(*) FROM customers;");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function addUser($first, $last, $user, $pass)
{
$stmt = $this->DB->prepare("select COUNT(*) FROM customers;");
$stmt->execute();
$ID = $stmt->fetchAll(PDO::FETCH_ASSOC);
$ID = $ID[0]['COUNT(*)'];
$hashed_pwd = password_hash($pass, PASSWORD_DEFAULT);
$stmt = $this->DB->prepare("INSERT INTO customers VALUES (" . $ID . ", '" . $user . "', '" . $hashed_pwd . "')");
$stmt->execute();
// return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
// $theDBA = new DatabaseAdapter();
// $arr = $theDBA->FavoriteList();
// print_r($arr);
?>