-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql.js
151 lines (137 loc) · 6.06 KB
/
mysql.js
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
var mysql = require("mysql");
var queries = {};
var connection = mysql.createConnection({
host: "vi4.host.cs.st-andrews.ac.uk",
user: "vi4",
password: "Uru8kLxGX6!gFg",
database: "vi4_cs3101_db"
});
connection.connect( function (err) {
if (err) {
console.log("\nCould not connect to database...");
console.log(err);
return;
}
console.log("Connected");
});
queries.signUp = function(res, user) {
connection.query("INSERT INTO `vi4_cs3101_db`.`Customer` (`Name`, `DOB`, `email`, `password`) "
+ "VALUES (\'" + user.name + "\', \'" + formatDate(user.dob) + "\', "
+ "\'" + user.email + "\', \'" + user.password + "\');",
function(err, rows) {
if (err) throw err;
res.render('newuser');
});
}
queries.login = function(res, email, password, req) {
connection.query("SELECT Customer.CustomerID, Customer.Name, Customer.email "
+ "FROM vi4_cs3101_db.Customer as Customer "
+ "WHERE Customer.email = \'" + email + "\' AND "
+ "Customer.password = \'" + password + "\' "
+ "GROUP BY Customer.Name;",
function(err, rows) {
if (err) throw err;
req.session.userID = rows[0].CustomerID;
req.session.username = rows[0].Name;
req.session.userEmail = rows[0].email;
res.render('successlogin', {rows: rows, user: req.session.username});
});
}
queries.bookReviews = function(res, isbn) {
connection.query("SELECT Reviews.Rating, Reviews.Comment, "
+ "Customer.Name, Books.ISBN, Books.Title, "
+ "GROUP_CONCAT(Distinct Contributer.Name SEPARATOR ', ') as Authors "
+ "FROM vi4_cs3101_db.Reviews as Reviews "
+ "INNER JOIN vi4_cs3101_db.Books as Books "
+ "ON Books.ISBN = Reviews.ISBN "
+ "INNER JOIN vi4_cs3101_db.Authors as Authors "
+ "ON Books.ISBN = Authors.ISBN "
+ "INNER JOIN vi4_cs3101_db.Contributer as Contributer "
+ "ON Contributer.ContributerID = Authors.ContributerID "
+ "INNER JOIN vi4_cs3101_db.Customer as Customer "
+ "ON Customer.CustomerID = Reviews.CustomerID "
+ "WHERE Reviews.ISBN = \'" + isbn + "\'"
+ "GROUP BY Reviews.CustomerID;",
function (err, rows) {
if (err) throw err;
res.render('reviews', {rows: rows});
});
}
queries.buyBook = function(res, isbn, email) {
connection.query("INSERT INTO `vi4_cs3101_db`.`Purchases` (`CustomerID`, `ISBN`, `Date Purchased`) "
+ "VALUES ((SELECT Customer.CustomerID from `vi4_cs3101_db`.`Customer` as Customer "
+ "WHERE UPPER(Customer.email) = UPPER(\'" + email + "\')), "
+ "\'" + isbn + "\', \'" + formatDate(new Date()) + "\');",
function (err, rows) {
if (err) throw err;
res.render('newpurchase', {rows: rows});
});
}
queries.allBooks = function(res) {
connection.query("SELECT * FROM Books", function (err, rows) {
if (err) throw err;
res.render('audiobooks', {rows: rows});
});
}
queries.getBook = function(res, isbn) {
connection.query("SELECT Books.Title, Books.ISBN, Books.Price, "
+ "Books.`Publisher Name`, Books.`Publication Date`, Books.Narrator, "
+ "Books.`Running Time`, Books.`Age Rating`, "
+ "GROUP_CONCAT(Contributer.Name SEPARATOR ', ') as Authors FROM vi4_cs3101_db.`Books` as Books "
+ "INNER JOIN vi4_cs3101_db.Authors as Authors "
+ "ON Books.ISBN = Authors.ISBN "
+ "INNER JOIN vi4_cs3101_db.Contributer as Contributer "
+ "ON Contributer.ContributerID = Authors.ContributerID "
+ "WHERE Books.ISBN = \'" + isbn + "\';",
function (err, rows) {
if (err) throw err;
res.render('purchases', {rows: rows, isbn: isbn});
});
}
queries.authorBooks = function(res, author) {
connection.query("SELECT Contributer.ContributerID, Contributer.Name, Contributer.Biography, Books.ISBN, Books.Title, "
+ "Books.`Publisher Name`, Books.`Publication Date`, Books.Narrator, Books.`Running Time`, Books.`Age Rating`, Books.Price "
+ "FROM vi4_cs3101_db.Contributer as Contributer "
+ "INNER JOIN vi4_cs3101_db.Authors as Authors "
+ "ON Contributer.ContributerID = Authors.ContributerID "
+ "INNER JOIN vi4_cs3101_db.Books "
+ "ON Books.ISBN = Authors.ISBN "
+ "WHERE UPPER(Contributer.Name) = UPPER(\"" + author + "\") "
+ "ORDER BY Contributer.ContributerID;",
function(err, rows) {
if (err) throw err;
res.render("viewauthor", {author: author, rows: rows});
});
}
queries.popularBooks = function(res) {
connection.query("SELECT Popular.Title, Popular.ISBN, Popular.`Number Purchases`, Books.Price, "
+ "Books.`Publisher Name`, Books.`Publication Date`, Books.Narrator, "
+ "Books.`Running Time`, Books.`Age Rating`, "
+ "GROUP_CONCAT(Contributer.Name SEPARATOR ', ') as Authors FROM vi4_cs3101_db.`Most Purchased` as Popular "
+ "INNER JOIN vi4_cs3101_db.Books as Books "
+ "ON Books.ISBN = Popular.ISBN "
+ "INNER JOIN vi4_cs3101_db.Authors as Authors "
+ "ON Books.ISBN = Authors.ISBN "
+ "INNER JOIN vi4_cs3101_db.Contributer as Contributer "
+ "ON Contributer.ContributerID = Authors.ContributerID "
+ "GROUP BY Popular.Title "
+ "ORDER BY Popular.`Number Purchases` DESC "
+ "LIMIT 10;",
function(err, rows) {
if (err) throw err;
res.render("popular", {rows: rows});
});
}
/**
* Allows for the formatting of dates to the format YYYY-MM-DD
*/
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
module.exports = queries;