-
Notifications
You must be signed in to change notification settings - Fork 0
/
dragonpool.js
416 lines (362 loc) · 11.5 KB
/
dragonpool.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
/*Variables with <> or all caps should be replaced when
* HTML and mySQL variables are known
*/
var express = require('express');
var request = require('request');
var cors = require('cors');
var app = express();
var mysql = require('mysql');
var bodyParser = require("body-parser");
app.use(cors());
app.use(express.static('.'));
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
var con = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'school'
});
con.connect(function(err) {
if (err) {
console.log(err);
}
else {
console.log('Database successfully connected');
}
});
var session = require('client-sessions');
app.use(session({
cookieName: 'session',
secret: 'edwardallenpoe1234',
duration: 30*60*1000,
activeDuration: 5*60*1000,
}));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/', function (req, res){
if(req.session.msg){
res.write(req.session.msg);
delete req.session.msg;
}
req.session.destroy();
return res.redirect('/DemoCreateAccount.html');
});
//Will redirect to the main html page
app.get("/home",function(req,res){
return res.redirect('/DemoMainPage.html');
});
//Loads all of the posts
app.get("/posts",function(req,res){
var posts_str = "<ul>";
con.query("SELECT * FROM posts, account WHERE id = account_id",
function(err,rows,fields)
{
if (err)
{
console.log(err);
}
else
{
for (var i=0;i<rows.length;i++)
{
var date = rows[i].date;
var month = date.getMonth();
var day = date.getDate();
var year = date.getFullYear();
var date_str = month + "/" + day + "/" + year;
posts_str += "<li><b>" + rows[i].type + " Passengers in " + rows[i].from_loc + " to " + rows[i].to_loc + "</b><br>User: " + rows[i].email + "<br>Description: " + rows[i].description + "<br>Date: " + date_str + "</li>";
}
posts_str += "</ul>";
}
res.send(posts_str);
});
});
//Get list of distinct 'from cities' - this can be used to generate a dropdown of cities for the filter
app.get("/citylist", function(req,res){
var citylist = "<select>";
con.query("SELECT DISTINCT(from_loc) from POSTS",
function(err,rows,fields)
{
if (err)
{
console.log(err);
}
else
{
for (var i=0;i<rows.length;i++)
{
citylist += "<option value=\"" + rows[i].from_loc + "\">" + rows[i].from_loc + "</option>";
}
citylist += "</select>"; res.send(citylist);
}
});
});
//Filter posts by city/state
app.get("/filter",function(req,res){
var from = req.query.from;
var posts_str = "<ul>";
var quer = "SELECT * FROM posts WHERE from_loc = " + con.escape(from);
con.query(quer,
function(err,rows,fields)
{
if (err)
{
console.log(err);
}
else
{
for (var i=0;i<rows.length;i++)
{
var date = rows[i].date;
var month = date.getMonth();
var day = date.getDate();
var year = date.getFullYear();
var date_str = month + "/" + day + "/" + year;
posts_str += "<li><b>" + rows[i].type + " Passengers in " + rows[i].from_loc + " to " + rows[i].to_loc + "</b><br>User: " + rows[i].email + "<br>Description: " + rows[i].description + "<br>Date: " + date_str + "</li>";
}
posts_str += "</ul>";
}
res.send(posts_str);
});
});
//Adds an account to the database if username/email is currently not being used
app.post("/addaccount",function(req,res){
//Account information that has been recieved by the webpage
//Changes these variables if necessary
var userQuery = req.body.username;
var passQuery = req.body.password;
var emailQuery = req.body.email;
var firstQuery = req.body.firstname;
var lastQuery = req.body.lastname;
var phoneQuery = req.body.phone;
var found = 0;
con.query('SELECT * FROM ACCOUNT',
function(err,rows,fields){
if(err){
console.log(err);
}
else{
//checks if username or email already exists inside of the database
var i = 0;
while(i < rows.length && found == 0){
if(rows[i].username == userQuery || rows[i].email == emailQuery){
found = 1;
}
i++;
}
if(found == 0){
if(phoneQuery != ""){
con.query('INSERT INTO account (username,password,email,phone,first_name,last_name) VALUES ('+con.escape(userQuery)+','+con.escape(passQuery)+','+con.escape(emailQuery)+','+con.escape(phoneQuery)+','+con.escape(firstQuery)+','+con.escape(lastQuery)+')',
function(err,result){
if(err){
console.log(err);
}
else{
console.log('Account created without phone number');
}
});
}
else{
con.query('INSERT INTO ACCOUNT (username,password,email,first_name,last_name) VALUES ('+con.escape(userQuery)+','+con.escape(passQuery)+','+con.escape(emailQuery)+','+con.escape(firstQuery)+','+con.escape(lastQuery)+')',
function(err,result){
if(err){
console.log(err);
}
else{
console.log('Account created with phone number');
}
});
}
}
else if(found == 1) {
res.send('Username/Email currently in use');
}
}
});
});
//Add a post
//Added code that pulls the ID from the database based on the username (passed in through query) - may not be necessary later on
app.post("/addpost",function(req,res){
console.log(req.body);
var quer = "INSERT INTO posts (account_id, from_loc, to_loc, type, date, description, num_riders) VALUES (";
quer += con.escape(req.session.userid) + "," + con.escape(req.body.from_loc) + "," + con.escape(req.body.to_loc) + "," + con.escape(req.body.type) + "," + con.escape(req.body.date) + "," + con.escape(req.body.description) + "," + con.escape(req.body.num_riders) + ")";
con.query(quer, function(err,result) {
if (err) {
console.log(err);
}
else {
res.send("Success");
}
});
});
//Edit account information from the database
app.post("/edit",function(req,res){
//Account information that has been recieved by the webpage
//Changes these variables if necessary
var userQuery = req.body.username;
var passQuery = req.body.password;
var emailQuery = req.body.email;
var firstQuery = req.body.first_name;
var lastQuery = req.body.last_name;
var phoneQuery = req.body.phone;
//Each if statment checks if the variable is not null
if(passQuery != ""){
//each con.query updates a column of the database that is associated with the username
con.query('UPDATE student SET password= CASE WHEN NOT password = "'+passQuery+'" THEN "'+passQuery+ '" ELSE password END WHERE username="'+userQuery+'"',
function(err,result){
if(err)
console.log(err);
else
console.log("Password changed");
})
}
if(emailQuery != ""){
con.query('UPDATE student SET email= CASE WHEN NOT email = "'+emailQuery+'" THEN "'+emailQuery+ '" ELSE email END WHERE username="'+userQuery+'"',
function(err,result){
if(err)
console.log(err);
else
console.log("email changed");
})
}
if(firstQuery != ""){
con.query('UPDATE student SET first_name= CASE WHEN NOT first_name = "'+firstQuery+'" THEN "'+firstQuery+ '" ELSE first_name END WHERE username="'+userQuery+'"',
function(err,result){
if(err)
console.log(err);
else
console.log("first name changed");
})
}
if(lastQuery != ""){
con.query('UPDATE student SET last_name= CASE WHEN NOT last_name = "'+lastQuery+'" THEN "'+lastQuery+ '" ELSE last_name END WHERE username="'+userQuery+'"',
function(err,result){
if(err)
console.log(err);
else
console.log("last name changed");
})
}
if(phoneQuery != ""){
con.query('UPDATE student SET phone= CASE WHEN NOT phone = "'+phoneQuery+'" THEN "'+phoneQuery+ '" ELSE phone END WHERE username="'+userQuery+'"',
function(err,result){
if(err)
console.log(err);
else
console.log("phone number changed");
})
}
})
//Removes a post
app.get("/deletepost",function(req,res){
var quer = "DELETE FROM posts WHERE post_id =" + req.query.postid + " AND account_id =" + req.session.userid;
con.query(quer, function(err, result) {
if(err){
console.log(err);
} else {
console.log(result.affectedRows);
}
});
});
//Edits a post only if it was created by the user
app.post("/editpost",function(req,res){
var quer = "SELECT account_id FROM posts WHERE post_id=" + req.query.postid;
var account = "";
con.query(quer, function(err, rows, fields) {
if(err){
console.log(err);
} else {
account = rows[0].account_id;
}
});
if(account == req.session.userid){
var quer = "UPDATE posts SET ";
var count = 0;
for (var param in req.query){
if(param != "account_id" && count != 0){
quer += " AND " + param + "=" + con.escape(req.query[param]);
} else if (param != "account_id") {
quer += param + "=" + con.escape(req.query[param]);
}
}
con.query(quer, function(err, result){
if(err){
console.log(err);
} else {
console.log(result.affectedRows);
}
});
}
});
//Loads account information
app.get("/loadaccount",function(req,res){
var quer = "SELECT * FROM account WHERE id = " + req.session.userid;
var listStr = "";
con.query(quer, function(err, rows, fields) {
if(err){
console.log(err);
} else {
listStr += '<h3>User Information</h3><ul>';
for (var i = 0; i < rows.length; i++){
listStr += "<li>Username: " + rows[i].username + "</li>";
listStr += "<li>First Name: " + rows[i].first_name + "</li>";
listStr += "<li>Last Name: " + rows[i].last_name + "</li>";
listStr += "<li>Email: " + rows[i].email + "</li>";
listStr += "<li>Phone No.: " + rows[i].phone + "</li>";
}
listStr += "</ul>";
}
});
quer = "SELECT * FROM posts WHERE account_id=" + req.session.userid;
con.query(quer, function(err, rows, fields) {
if(err){
console.log(err);
} else {
listStr += "<h3>User Posts</h3><ul>";
for (var i = 0; i < rows.length; i++){
var date = rows[i].date;
var month = date.getMonth();
var day = date.getDate();
var year = date.getFullYear();
var date_str = month + "/" + day + "/" + year;
listStr += "<li>Post ID: " + rows[i].post_id + "</li>";
listStr += "<li>From: " + rows[i].from_loc + "</li>";
listStr += "<li>To: " + rows[i].to_loc + "</li>";
listStr += "<li>Type: " + rows[i].type + "</li>";
listStr += "<li>Date: " + date_str + "</li>";
listStr += "<li>Desc: " + rows[i].description + "</li>";
listStr += "<li>Riders: " + rows[i].num_riders + "</li>";
}
}
res.send(listStr);
});
listStr += "</ul>";
});
app.post('/login', function(req, res) {
var quer = "SELECT id FROM account WHERE username =" + con.escape(req.body.username) + " AND password =" + con.escape(req.body.password);
con.query(quer, function(err, rows, fields) {
if(err){
req.session.msg("Invalid login");
} else {
if(rows.length > 0){
req.session.userid=rows[0].id;
res.redirect("http://localhost:8080/DemoMainPage.html");
} else {
req.session.msg = "Invalid login";
return res.redirect('/');
}
}
});
});
app.get('/logout', function(req, res) {
req.session.reset();
req.session.msg = 'You logged out';
return res.redirect('/');
});
app.listen(8080, function(){
console.log('Server Started...');
});