-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
247 lines (204 loc) · 6.17 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
var express = require('express');
var app = express();
var bcrypt = require('bcrypt');
var conn = require('./db');
var session = require('express-session');
app.use(
session({
secret: 'secret',
resave: true,
saveUninitialized: true,
}),
);
// Set up the view engine
app.set('view engine', 'ejs');
// Middleware
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Routes home
app.get('/', function (req, res) {
res.render('home');
});
// Routes login
app.get('/login', function (req, res) {
res.render('login');
});
// Routes register
app.get('/register', function (req, res) {
res.render('register');
});
// Routes product page
app.get('/product', function (req, res) {
if (!req.session.loggedIn) {
res.redirect('/login');
res.end();
} else {
res.render('products');
}
});
// Route for logout
app.get('/logout', function (req, res) {
req.session.destroy();
res.redirect('/');
res.end();
console.log('User logged out');
});
// Route admin dashboard
app.get('/dashboard', function (req, res) {
if (!req.session.loggedIn) {
res.redirect('/login');
res.end();
} else {
// Check if the user is an admin
// Get the user from the database
conn.query('SELECT * FROM users WHERE username = ?', [req.session.username], function (error, results, fields) {
if (error) throw error;
console.log('User found in database', results);
if (results.length > 0) {
var user = results[0];
console.log('User', user);
if (user.is_admin) {
console.log('User is admin');
// Fetch all the ratings from the database
conn.query('SELECT * FROM ratings', function (error, results, fields) {
if (error) throw error;
console.log('Ratings From database', results);
// Create a histogram of the ratings
var histogram = {
'1': { '1': 0, '2': 0, '3': 0, '4': 0, '5': 0 },
'2': { '1': 0, '2': 0, '3': 0, '4': 0, '5': 0 },
'3': { '1': 0, '2': 0, '3': 0, '4': 0, '5': 0 },
};
for (var i = 0; i < results.length; i++) {
var rating = results[i];
histogram[rating.product_id][rating.rating]++;
}
console.log('Histogram', histogram);
res.render('dashboard', { ratings: histogram });
});
} else {
console.log('User is not admin');
res.redirect('/product');
res.end();
}
} else {
console.log('User not found');
res.redirect('/');
res.end();
}
});
}
});
// Registration Process
app.post('/reg', function (request, response) {
console.log('Register Request', request.body);
if (request.body.password != request.body.password_confirm) {
console.log('Password not match');
response.redirect('/register');
response.end();
} else {
console.log('Password match');
// Hash the password
var hashedPassword = bcrypt.hashSync(request.body.password, 10);
console.log('Hashed Password', hashedPassword);
// ADD TO DATABASE
conn.query(
'INSERT INTO users (username, password) VALUES (?, ?)',
[request.body.username, hashedPassword],
function (error, results, fields) {
if (error) throw error;
console.log('User added to database');
response.redirect('/login');
},
);
}
});
// Login Process
app.post('/auth', function (request, response) {
console.log('Login Request', request.body);
conn.query('SELECT * FROM users WHERE username = ?', [request.body.username], function (error, results, fields) {
if (error) throw error;
console.log('User found in database', results);
if (results.length > 0) {
var user = results[0];
console.log('User', user);
var passwordMatch = bcrypt.compareSync(request.body.password, user.password);
console.log('Password Match', passwordMatch);
if (passwordMatch) {
request.session.username = request.body.username;
request.session.loggedIn = true;
response.redirect('/product');
response.end();
} else {
response.redirect('/login');
response.end();
}
} else {
response.send('User not found');
}
});
});
// Process rating submission
app.post('/submit_ratings', function (req, res) {
if (!req.session.loggedIn) {
res.redirect('/login');
res.end();
}
console.log('Rating Submission', req.body);
// Who rated the product
console.log('User', req.session.username);
// TODO: check if the user has already rated the product
// If the user has already rated the product, update the rating
// Process the rating submission
var ratings = [
{
'product_id': 1,
'rating': req.body.rating_product1,
},
{
'product_id': 2,
'rating': req.body.rating_product2,
},
{
'product_id': 3,
'rating': req.body.rating_product3,
},
];
console.log('Ratings', ratings);
// Add to database
for (var i = 0; i < ratings.length; i++) {
conn.query(
'INSERT INTO ratings (product_id, rating, user) VALUES (?, ?, ?)',
[ratings[i].product_id, ratings[i].rating, req.session.username],
function (error, results, fields) {
if (error) throw error;
console.log('Rating added to database');
},
);
}
});
app.get('/comments', function (req, res) {
res.render('comments');
});
app.post('/addcomments', function (req, res) {
console.log('Comment Submission', req.body);
conn.query(
'INSERT INTO comments (title, message, email) VALUES (?, ?, ?)',
[req.body.title, req.body.message, req.body.email],
function (error, results, fields) {
if (error) throw error;
console.log('Comments added to database');
res.redirect('/comments');
},
);
});
app.get('/admin', function (req, res) {
// Fetch all the comments from the database
conn.query('SELECT * FROM comments', function (error, results, fields) {
if (error) throw error;
console.log('Comments From database', results);
res.render('admin', { commentsData: results });
});
});
app.listen(3000);
console.log('Server started on port 3000');