forked from ZiyiAn/DiningPal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
483 lines (446 loc) · 15 KB
/
index.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000
const { Pool } = require('pg');
var bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const session = require('express-session')
const sessionFiles = require('session-file-store')(session)
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: true
});
var app = express()
express()
.use(express.static(path.join(__dirname, 'public')))
.use(bodyParser.json())
.use(cookieParser('dining pal'))
.use(session({
secret: 'dining pal',
//resave: true,
saveUninitialized: false,
store: new sessionFiles({logFn:()=>{}}),//replace the logFn option with an empty function
cookie: { user:"default", maxAge:3600000 }
}))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/', (req, res ) => {
//console.log("Welcome back")
var myUser = req.session.myUser
//var isAdmin = req.session.isAdmin
if(myUser){
console.log(myUser)//show session content
if(myUser.isadmin){
console.log("homepage_admin. adminname:"+myUser.username)
//console.log("All users:"+req.session.allUsers)
res.redirect('/NewUI/new_homepage.html')
}
else {
console.log("homepage_user. username:"+myUser.username)
res.redirect('/NewUI/new_homepage_user.html')
}
}
else {
//res.render('pages/index')
res.redirect('/startpage.html')
}
})
// .get('/home', (req, res ) => {//for testing
// //console.log("Welcome back")
// var myUser = req.session.myUser
// //var isAdmin = req.session.isAdmin
// if(myUser){
// console.log(myUser)//show session content
// if(myUser.isadmin){
// console.log("homepage_admin. adminname:"+myUser.username)
// //console.log("All users:"+req.session.allUsers)
// res.redirect('/NewUI/new_homepage.html')
// }
// else {
// console.log("homepage_user. username:"+myUser.username)
// res.redirect('/NewUI/new_homepage_user.html')
// }
// }
// else {
// //res.render('pages/index')
// res.redirect('/startpage.html')
// }
// })
.get('/signin', async (req,res)=>{
try{
const client = await pool.connect()
var query = "select * from users where email=($1) and password=($2)";
var info = [req.query.email, req.query.password];
await client.query(query, info, async function(err, result){
if (err||!result.rows[0]){
console.log("Query error: " + err )
console.log(result.rows[0] )
/*do something if Username or password incorrect
res.send("Query error: " + err);
*/
res.render('pages/error',{message:"E-mail or password incorrect"})
}
else {
console.log("signin succeed")
if(result.rows[0].isadmin){//admin user
//query = "select * from users"
//const result = await client.query(query)
console.log('admin:',info[0])
//console.log('allusers:',result2)
//const results = { 'results': (result) ? result.rows : null}
req.session.regenerate((err)=>{
if(err){
console.log(err)
res.render('pages/error',{message:"Cookie function fail"})
}
})
req.session.myUser = {
username:result.rows[0].username,
email:result.rows[0].email,
isadmin:result.rows[0].isadmin,//true
western:result.rows[0].western,
europe:result.rows[0].europe,
asia:result.rows[0].asia,
indian:result.rows[0].indian,
mexican:result.rows[0].mexican,
}
//req.session.allUsers = { 'results': (result) ? result.rows : null}
//console.log(results)
res.redirect('/NewUI/new_homepage.html')
}
else{//regular user
console.log("user:",result.rows[0].username)
req.session.regenerate((err)=>{
if(err){
console.log(err)
res.render('pages/error',{message:"Cookie function fail"})
}
})
req.session.myUser = {
username:result.rows[0].username,
email:result.rows[0].email,
isadmin:result.rows[0].isadmin, //false
western:result.rows[0].western,
europe:result.rows[0].europe,
asia:result.rows[0].asia,
indian:result.rows[0].indian,
mexican:result.rows[0].mexican,
//and any other info useful
}
res.redirect('/NewUI/new_homepage_user.html')
}
client.release();
}
res.end()
})
} catch (err){
console.error(err);//database not connected
// res.send("DB connection error: " + err );
res.render('pages/error',{message:"Database connection fail"})
}
})
.get('/signup', async (req,res)=>{
try{
const client = await pool.connect()
var query = "insert into users values($1, $2, $3, FALSE) ";
var info = [req.query.username, req.query.password, req.query.email];
await client.query(query, info, function(err, result){
if (err){
console.log("Query error: " + err );
/*do something if username exist
// res.send("Query error: " + err);
*/
res.render('pages/error',{message:"E-mail already exist"})
}
else {
console.log("signup succeed")
req.session.regenerate((err)=>{
if(err){
res.render('pages/error',{message:"Cookie function fail"})
return console.log(err)
}
})
req.session.myUser = {
username:req.query.username,
email:req.query.email,
isadmin:false,//false
western:req.query.western,
europe:req.query.europe,
asia:req.query.asia,
indian:req.query.indian,
mexican:req.query.mexican,
//and any other info useful
}
//var userinfo = {username:req.query.username, password:req.query.password, email:req.query.email, isadmin:false}
res.redirect('/NewUI/new_homepage_user.html')
client.release();
}
res.end()
})
} catch (err){
console.error(err);//database not connected
res.render('pages/error',{message:"Database connection fail"})
}
})
.get('/checkLogin',(req, res)=>{
var myUser = req.session.myUser
if(myUser){
console.log("user logged in. username:"+myUser.username)
res.send({isLogin: true})
}
else {
console.log("user not logged in")
res.send({isLogin: false})
}
})
.get('/getUsername',(req, res)=>{
var myUser = req.session.myUser
if(myUser){
console.log("send username:"+myUser.username)
res.send(myUser)
}
else {
console.log("user not logged in")
res.send({username:"None"})
}
})
.get('/update',async(req, res)=>{
var myUser = req.session.myUser
const client = await pool.connect()
var query = "update users set username=($2),western=($3),europe=($4),asia=($5),indian=($6),mexican=($7)"
var where = " where email=($1)"
western= req.query.western==undefined? false:req.query.western
europe= req.query.europe ==undefined? false:req.query.europe
asia= req.query.asia ==undefined? false:req.query.asia
indian= req.query.indian ==undefined? false:req.query.indian
mexican= req.query.mexican ==undefined? false:req.query.mexican
var info = [req.query.email,req.query.username,western,europe,asia,indian,mexican];
if (req.query.password != ""){
query += ",password=($8)"
info.push(req.query.password)
}
query += where;
// console.log(req.query.mexican)
await client.query(query, info, function(err, result){
if (err){
console.log("Query error: " + err );
res.render('pages/error',{message:"Update failed!"})
}
else {
res.clearCookie()
if (myUser.isadmin){
req.session.regenerate((err)=>{
if(err){
console.log(err)
res.render('pages/error',{message:"Cookie function fail"})
}
})
req.session.myUser = {
username:req.query.username,
email:req.query.email,
isadmin:true,//true
western:req.query.western,
europe:req.query.europe,
asia:req.query.asia,
indian:req.query.indian,
mexican:req.query.mexican,
}
console.log("Update succeed")
res.redirect('/NewUI/form-validation.html')
client.release();
}
else {
res.clearCookie()
req.session.regenerate((err)=>{
if(err){
console.log(err)
res.render('pages/error',{message:"Cookie function fail"})
}
})
req.session.myUser = {
username:req.query.username,
email:req.query.email,
isadmin:false,//true
western:req.query.western,
europe:req.query.europe,
asia:req.query.asia,
indian:req.query.indian,
mexican:req.query.mexican,
}
console.log("Update succeed")
res.redirect('/NewUI/form-validation_user.html')
client.release();
}
}
res.end()
})
})
.get('/sendLocation', async (req, res)=>{
var myUser = req.session.myUser
if(myUser){
try{
const client = await pool.connect()
var query = "update users set x=($1), y=($2) where email=($3) ";
var info = [req.query.x, req.query.y, myUser.email];
console.log("email: "+myUser.email)
console.log("x: "+req.query.x)
console.log("y: "+req.query.y)
await client.query(query, info, function(err, result){
if (err){
console.log("Query error: " + err );
//res.render('pages/error',{message:"E-mail already exist"})
}
else {
console.log("location update succeed")
//var userinfo = {username:req.query.username, password:req.query.password, email:req.query.email, isadmin:false}
//res.redirect('/NewUI/new_homepage_user.html')
client.release();
}
res.end()
})
} catch (err){
console.error(err);//database not connected
//res.render('pages/error',{message:"Database connection fail"})
}
}
else {
console.log("user not logged in")
//res.send({username:"None"})
}
})
.get('/logout', (req, res)=>{
req.session.destroy((err)=>{
if(err){
console.log("Fail to destroy cookie")
res.render('pages/error',{message:"Fail to destroy cookie"})
}
console.log("logging out")
res.clearCookie()
res.redirect('/')
})
})
.get('/numOfUser', async (req, res)=>{
// console.log('1');
var myUser = req.session.myUser
if(myUser){
// console.log('2');
const client = await pool.connect();
var query = "select * from users where email = ($1)"
var info = [myUser.email]
const result = await client.query(query, info)
//const results = {'results':(result)? result.rows:null}
var query2 = "select * from users"
const result2 = await client.query(query2, [])
// console.log('3');
//const results2 = {'results':(result)? result.rows:null}
var x = result.rows[0].x
var y = result.rows[0].y
var num=0;
if(result && result2){
for (var i=0; i<result2.rows.length; i++){
if (result2.rows[i].x && result2.rows[i].y){
X = result2.rows[i].x
Y = result2.rows[i].y
if (Math.sqrt((X-x)*(X-x)+(Y-y)*(Y-y))<1){
num++;
}
}
}
}
if (num!=0){
num--;
}
res.send({numUsers:num})
}
else {
console.log("user not logged in")
res.send(null)
}
})
.get('/nearbyUserList', async (req, res)=>{
// console.log('1');
var myUser = req.session.myUser
if(myUser){
// console.log('2');
const client = await pool.connect();
var query = "select * from users where not (email=($1))"
var info = [myUser.email]
const result = await client.query(query, info)
//const results = {'results':(result)? result.rows:null}
res.send({userList:result.rows})
}
else {
console.log("user not logged in")
res.send(null)
}
})
.get('/nearbyUsers', async (req, res)=>{
console.log('1')
var myUser = req.session.myUser
if(myUser){
const client = await pool.connect()
var query = "select x,y,username from users where not (email=($1))"
var info = [myUser.email]
const result = await client.query(query, info)
if(result){
res.send({users:result.rows})
}
else{
res.send(null)
}
}
else {
console.log("user not logged in")
res.send(null)
}
})
.get('/deleteUser', async (req,res)=>{
try{
const client = await pool.connect()
var query = "delete from users where email=($1)";
var info = [req.query.email];
//console.log("useremail is "+req.query)
await client.query(query, info, function(err, result){
if (err){
console.log("Query error: " + err );
/*do something if username exist
// res.send("Query error: " + err);
*/
res.render('pages/error',{message:"Fail to delete user"})
}
else {
console.log("delete succeed")
//var userinfo = {username:req.query.username, password:req.query.password, email:req.query.email, isadmin:false}
res.redirect('/database')
client.release();
}
res.end()
})
} catch (err){
console.error(err);//database not connected
res.render('pages/error',{message:"Database connection fail"})
}
})
.get('/database', async(req,res)=>{
var myUser = req.session.myUser
if(myUser && myUser.isadmin){
console.log('get database')
try{
const client = await pool.connect()
const result = await client.query('SELECT * FROM users')
const results = {'results':(result)? result.rows:null}
res.render('pages/table-dynamic',results)
client.release()
}
catch(err){
console.error(err)
res.render('pages/error',{message:"Database connection fail"})
}
}
else{//not admin loginned
console.log('Not loginned or not admin')
res.redirect('/')
}
})
.listen(PORT, () => console.log(`Listening on ${ PORT }`))
module.exports = app