-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
145 lines (127 loc) · 4.63 KB
/
app.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
let express = require("express");
let session = require("express-session");
let bodyParser = require("body-parser");
let db = require("./dataBase");
//setup basic things (express app, body parser, sessions)
const PORT = 3000;
let app = express();
let urlencodedParser = bodyParser.urlencoded({ extended: false });
app.set('view engine', 'pug');
app.set('views', './views');
app.use(session({
secret: "BruhahA2642",
cookie: { maxAge: 3600000 },
resave: false,
saveUninitialized: false
}));
//redirect if not loggen in funtion
let redirect_unauth = function (req, res, callback) {
if (req.session.user === undefined) {
res.redirect("/login")
} else {
callback();
}
};
//redirect if visiting base site
app.get("/", function (req, res) {
redirect_unauth(req, res, function () {
res.redirect("/search");
});
});
//login / register code
app.get("/login", function (req, res) {
res.render("login");
});
app.get("/register", function (req, res) {
res.render("register");
});
app.post("/login", urlencodedParser, function (req, res) {
let username = req.body.username;
let pw = req.body.password;
if (username === "" || pw === "") {
res.render("login", {message: "You must enter a Username and a password!", lastusername: username});
} else {
db.findUser(username, function (dbResRows) {
if (dbResRows[0] === undefined) {
res.render("login", {message: "User does not exist. You may sign up!", lastusername: username});
} else {
let userid = dbResRows[0].id;
let passhash = dbResRows[0].passhash;
db.comparePasswords(pw, passhash, function (result) {
if (result === false) {
res.render("login", {message: "Wrong password!!!", lastusername: username});
} else {
req.session.user = username;
req.session.userid = userid;
res.redirect("/search")
}
});
}
});
}
});
app.post("/register", urlencodedParser, function (req, res) {
let username = req.body.username;
let pw = req.body.password;
let pw2 = req.body.confirmPassword;
if (username === "" || pw === "") {
res.render("register", {message: "You must enter a Username and a password!", lastusername: username});
} else if (pw !== pw2) {
res.render("register", {message: "Your password does not match with the confirmation.", lastusername: username});
} else {
db.findUser(username, function (dbResRows) {
if (dbResRows[0] !== undefined) {
res.render("register", {message: "That username is already taken. :(", lastusername: username});
} else {
db.addUser(username, pw, function () {
res.render("welcome", {user: username})
});
}
});
}
});
//logout
app.get("/logout", function (req, res) {
req.session.user = undefined;
req.session.userid = undefined;
res.redirect("/login");
});
//Book search site
app.get("/search", urlencodedParser, function (req, res) {
redirect_unauth(req, res, function () {
res.render("search", {user: req.session.user});
});
});
app.post("/search", urlencodedParser, function (req, res) {
redirect_unauth(req, res, function () {
let searchterm = req.body.searchterm;
db.searchBooks(searchterm, function (dbResRows) {
res.render("search", {user: req.session.user, books: dbResRows});
});
});
});
//Detailed book site
app.get("/book/:id", urlencodedParser, function (req, res) {
redirect_unauth(req, res, function () {
let bookid = req.params.id;
db.getBookById(bookid, function (dbResBook) {
db.getBookCriticsByBookId(bookid, function (dbResCritics, avgRating) {
db.getBookPictureURLbyTitle(dbResBook.title, function (bookPicURL) {
res.render("detailed_view", {user: req.session.user, book: dbResBook, critics: dbResCritics, avgRating: avgRating, bookIMG: bookPicURL});
});
});
});
});
});
app.post("/book/:id", urlencodedParser, function (req, res) {
redirect_unauth(req, res, function () {
let bookid = req.params.id;
db.setOrInsertBookCritic(bookid, req.session.userid, req.body.user_review, req.body.user_rating, function () {
res.redirect("/book/" + bookid);
});
});
});
//start server
app.listen(PORT, function() {
console.log(`Server running and listening on port ${PORT}`);
});