-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
555 lines (417 loc) · 16.5 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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
var mysql = require('mysql2');
var dbms = mysql.createConnection({
host: "localhost",
user: "admin",
password: "admin", // Change password for you!!!
database: "Platform",
dateStrings: 'date'
});
dbms.connect((err) => {
//trial query to check connection
dbms.query("select * from User", (result) => {
if(err) throw err;
console.log(">> Successfully connected to Platform Database");
});
});
//Till here was just connecting to database portfolio
var express = require('express');
var logger = require('morgan');
var bp = require('body-parser');
var getJSON = require('get-json');
var path = require('path');
const { writer } = require('repl');
const { query } = require('express');
var app = express();
var activeUsers = {};
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.use(express.urlencoded({extended: true}));
app.use(express.json());
app.use(logger('dev'));
app.get('/', (req, res) => {
res.render('index')
});
app.get('/login', (req, res) => {
res.render('login', {message: "", username: ""})
});
app.post('/login', (req, res) => {
var query = "select userID, password from User where username = \"" + req.body.username + "\";";
dbms.query(query, (err, result, fields) => {
if(err) throw err;
if(result.length == 0) {
res.render('login', {message: "Invalid Username", username: req.body.username})
}
else if(req.body.password != result[0].password) {
res.render('login', {message: "Wrong Password", username: req.body.username});
}
else {
activeUsers[result[0].userID] = req.socket.remoteAddress;
var travel;
if(result[0].userID == 1) {
travel = '/control';
}
else {
travel = '/profile/' + result[0].userID;
}
res.redirect(travel);
}
});
})
app.get('/register', (req, res) => {
res.render('register', {message: "", username: ""});
});
app.post('/register', (req, res) => {
var query = "select username from User where username = \"" + req.body.username + "\";";
dbms.query(query, (err, result, fields) => {
if(err) throw err;
if(result.length == 1) {
res.render('register', {message: "Username Exists", username: req.body.username});
}
else if(req.body.username == "") {
res.render('register', {message: "Username Empty", username: req.body.username});
}
else if(req.body.password == "") {
res.render('register', {message: "Password Empty", username: req.body.username});
}
else if(req.body.password != req.body.confirm_password) {
res.render('register', {message: "Passwords are different", username: req.body.username});
}
else {
var new_record = "INSERT INTO User(username, password)" +
" VALUES(\"" + req.body.username + "\", \"" + req.body.password + "\");";
dbms.query(new_record, (err, result, fields) => {
if(err) throw err;
var findID = "SELECT * from User WHERE username=\"" + req.body.username + "\";";
dbms.query(findID, (err, result, fields) => {
if(err) throw err;
activeUsers[result[0].userID] = req.socket.remoteAddress;
var travel = '/profile/' + result[0].userID;
res.redirect(travel);
});
});
}
});
});
app.get('/profile/:id', (req, res) => {
//console.log(req.params.id);
var key = parseInt(req.params.id);
if(activeUsers[key] == undefined) {
return res.redirect('/login');
}
// VERY VERY IMPORTANT QUERY
var findID = "with UserStocks(userID,units,stockname) as ( SELECT userID, units,stockname from Transactions natural join User having userID = "+key+") select sum(units)as num_stocks , sum(units)*unitprice as stocksworth, stockname, sector from UserStocks natural join Stocks group by stockname ;";
var username ;
var find_user = "SELECT username from User WHERE userID = " + key + ";"
dbms.query(find_user , (err,result,feilds)=>{
if(err) throw err;
username = result[0].username;
});
dbms.query(findID, (err, result, fields) => {
if(err) throw err;
// console.log(result[0].stockname);
res.render('profile', {username: username,shares:result, key: key});
});
});
app.get('/buy/:id', (req, res) => {
//console.log(req.params.id);
var key = req.params.id;
if(activeUsers[key] == undefined) {
return res.redirect('/login');
}
var findID = "SELECT * from User WHERE userID = " + key + ";";
var userID = key;
var stocks, username, userID;
dbms.query(findID, (err, result, fields) => { // showing profile page
if(err) throw err;
username = result[0].username;
});
var stocksQuery = 'SELECT * FROM Stocks;';
dbms.query(stocksQuery, (err, result, fields) => { // showing drop down menu
if(err) throw err;
stocks = result;
res.render('buy', {userID: userID, username: username, stocks: stocks, statusMessage: ""});
});
});
app.post('/buy/:id', (req, res) =>{
// req.body object has your form values
var key = req.params.id;
if(isNaN(parseInt(req.body.sharesBought))){ // no option was selected
res.redirect('/buy/' + key);
return;
}
var sharesBought = parseInt(req.body.sharesBought);
var chosenStockID = parseInt(req.body.chosenStockID);
var userID = parseInt(req.params.id);
var stockName, totalPrice;
var statusMessage = "";
var username = req.body.username;
var stockQuery =
`SELECT * FROM Stocks
WHERE stockID = ${chosenStockID};`;
dbms.query(stockQuery, (err, result, fields) =>{
if(err) throw err;
totalPrice = sharesBought*result[0].unitPrice;
stockName = result[0].stockName;
var insertQuery =
`INSERT INTO Transactions(userID,stockName,units,totalValue,transacted) VALUES
(${userID}, '${stockName}', ${sharesBought}, ${totalPrice}, NOW());`;
dbms.query(insertQuery, (err2, result2, fields2)=>{ // inserting tuple into transactions
if(err2) throw err2;
});
var allStocksQuery = 'SELECT * FROM Stocks;';
dbms.query(allStocksQuery, (err2, result2, fields) =>{
if(err2) throw err2;
statusMessage = `${sharesBought} Shares of ${stockName} worth ${totalPrice} USD Successfully Bought!`;
res.render('buy', {userID: userID, username: username, stocks: result2, statusMessage: statusMessage});
});
});
});
app.get('/sell/:id', (req, res) => {
//console.log(req.params.id);
var key = req.params.id;
if(activeUsers[key] == undefined) {
return res.redirect('/login');
}
var findID = "SELECT * from User WHERE userID = " + key + ";";
var userID = key;
var stocks, username, userID;
dbms.query(findID, (err, result, fields) => { // showing profile page
if(err) throw err;
username = result[0].username;
});
var stocksQuery = 'SELECT * FROM Stocks;';
dbms.query(stocksQuery, (err, result, fields) => { // showing drop down menu
if(err) throw err;
stocks = result;
res.render('sell', {userID: userID, username: username, stocks: stocks, statusMessage: ""});
});
});
app.post('/sell/:id', (req, res) =>{
// req.body object has your form values
var key = req.params.id;
if(isNaN(parseInt(req.body.sharesSold))){ // no option was selected
res.redirect('/sell/' + key);
return;
}
var sharesSold = parseInt(req.body.sharesSold);
var chosenStockID = parseInt(req.body.chosenStockID);
var userID = parseInt(req.params.id);
var stockName, totalPrice;
var statusMessage = "";
var username = req.body.username;
var allStocksQuery = `SELECT * FROM Stocks;`;
var stockQuery =
`SELECT * FROM Stocks
WHERE stockID = ${chosenStockID};`;
dbms.query(stockQuery, (err, result, fields) =>{
if(err) throw err;
totalPrice = sharesSold*result[0].unitPrice;
stockName = result[0].stockName;
var sumOfSharesQuery =
`SELECT SUM(units) as sum FROM Transactions
WHERE userID = ${userID} AND stockName = '${stockName}';`;
dbms.query(sumOfSharesQuery, (err2, result2, fields2) =>{
//console.log(sumOfShares);
//console.log(result2);
//console.log(sumOfSharesQuery);
if(err2) throw err2;
var sumOfShares = parseInt(result2[0].sum);
if(isNaN(sumOfShares) || sumOfShares < sharesSold){ // 0 stocks or less than required
statusMessage = `You do not have so many stocks to sell. Try again`;
dbms.query(allStocksQuery, (err3, result3, fields3) => { // showing drop down menu
if(err3) throw err3;
res.render('sell', {userID: userID, username: username, stocks: result3, statusMessage: statusMessage});
});
return;
}
var insertQuery =
`INSERT INTO Transactions(userID,stockName,units,totalValue,transacted) VALUES
(${userID}, '${stockName}', -${sharesSold}, -${totalPrice}, NOW());`; // 2 '-' to indicate minus
dbms.query(insertQuery, (err3, result3, fields3)=>{ // inserting tuple into transactions
if(err3) throw err3;
});
dbms.query(allStocksQuery, (err3, result3, fields3) => { // showing drop down menu
if(err3) throw err3;
statusMessage = `${sharesSold} Shares of ${stockName} worth ${totalPrice} USD Successfully Sold!`;
res.render('sell', {userID: userID, username: username, stocks: result3, statusMessage: statusMessage});
});
});
});
});
app.get('/quote/:id', (req, res) => {
//console.log(req.params.id);
var key = parseInt(req.params.id);
if(activeUsers[key] == undefined) {
return res.redirect('/login');
}
var findID = "SELECT * from Stocks;";
dbms.query(findID, (err, result, fields) => {
if(err) throw err;
res.render('quote', {key: key, inc: "",unitprice: "", shares: result});
});
});
app.post('/quote/:id', (req, res) => {
var query = "SELECT unitprice from Stocks where stockname = \"" + req.body.chosenStockName + "\";";
var key = parseInt(req.params.id);
var quantity = parseInt(req.body.stockUnits);
var inc = req.body.chosenStockName;
dbms.query(query, (err, result, fields) => {
if(err) throw err;
//console.log(result);
var stockPrice = "select * from StockPrice where stockName = \"" + req.body.chosenStockName +"\" order by time DESC;";
dbms.query(stockPrice , (err , stockPrices , feilds)=>{
var findID = "SELECT * from Stocks;";
console.log(stockPrices);
dbms.query(findID, (err, answer, fields) => {
if(err) throw err;
res.render('quote', {key: key, quantity: quantity, price: result[0].unitprice, inc: inc, shares: answer , sharePrize:stockPrices});
});
})
});
})
app.get('/history/:id', (req, res) => {
//console.log(req.params.id);
var key = parseInt(req.params.id);
if(activeUsers[key] == undefined) {
return res.redirect('/login');
}
var findID = "SELECT * from User WHERE userID = " + key + ";";
dbms.query(findID, (err, result, fields) => {
if(err) throw err;
var username = result[0].username;
var userHistory = "SELECT * from Transactions where userID = " + key + " ORDER BY transactionID DESC LIMIT 5;";
dbms.query(userHistory, (err, result, fields) => {
if(err) throw err;
res.render('history', {data: result, username: username, key: key});
});
});
});
app.get('/watchlist/:id', (req, res) => {
var key = parseInt(req.params.id);
if(activeUsers[key] == undefined) {
return res.redirect('/login');
}
var query = "SELECT stockname, sector, unitPrice from WatchList inner join Stocks using (stockName) where userID = " + key + ";";
var findID = "SELECT * from Stocks;";
dbms.query(query, (err, result, field) => {
if (err) throw err;
dbms.query(findID, (err, answer, field) => {
if (err) throw err;
res.render('watchlist', {key: key, shares: answer, data: result});
});
});
})
app.post('/watchlist/:id', (req, res) => {
var op = req.body.operation;
var inc = req.body.chosenStockName;
var key = parseInt(req.params.id);
var change;
if(op == "ADD"){
change = `INSERT into WatchList (userID, stockName) values (${key}, '${inc}');`;
} else if(op == "REMOVE"){
change = `DELETE from WatchList where userID = ${key} and stockName = '${inc}';`;
}
if(op == "ADD" || op == "REMOVE"){
dbms.query(change, (err, answer, field) => {
if (err) throw err;
});
}
var query = "SELECT stockname, sector, unitPrice from WatchList inner join Stocks using (stockName) where userID = " + key + ";";
var findID = "SELECT * from Stocks;";
dbms.query(query, (err, result, field) => {
if (err) throw err;
dbms.query(findID, (err, answer, field) => {
if (err) throw err;
res.render('watchlist', {key: key, shares: answer, data: result});
});
});
})
app.get('/discussion/:id', (req, res) => {
var key = parseInt(req.params.id);
if(activeUsers[key] == undefined) {
return res.redirect('/login');
}
var getMessages = `
SELECT * FROM Discussion, User
WHERE User.userID = Discussion.userID;
`;
dbms.query(getMessages, (err, result, field) =>{
if(err) throw err;
res.render('discussion', {userID: key, messages: result});
});
})
app.post('/discussion/:id', (req, res) =>{
var key = parseInt(req.params.id);
var message = req.body.message;
if(message === ""){
res.redirect('/discussion/' + key);
return;
}
console.log(message);
var addRow =
`
INSERT INTO Discussion(userID, message, posted) VALUES
(${key}, '${message}', NOW());
`;
dbms.query(addRow, (err, result, field) =>{
if(err) throw err;
});
res.redirect('/discussion/' + key);
})
app.get('/logout/:id', (req, res) => {
var key = parseInt(req.params.id);
activeUsers[key] = undefined;
res.redirect('/')
})
app.get('/control', (req, res) => {
find = "SELECT * from Stocks;";
dbms.query(find, (err, result, fields) => {
if(err) throw err;
res.render('control', {data: result});
});
})
app.get('/update', (req, res) => {
find = "SELECT * from Stocks;";
dbms.query(find, (err, result, fields) => {
if(err) throw err;
res.render('update', {data: result});
});
})
app.post('/update', (req, res) => {
var inc = req.body.chosenStockName;
var value = parseInt(req.body.sharePrice);
q2 = `UPDATE Stocks set unitPrice = ${value} where stockName = '${inc}';`;
console.log(q2);
dbms.query(q2, (err, result, fields) => {
if (err) throw err;
q1 = `Insert into StockPrice (stockName, time, Price) values ('${inc}',NOW(), ${value})`;
dbms.query(q1, (err, result, fields) => {
if (err) throw err;
});
find = "SELECT * from Stocks;";
dbms.query(find, (err, answer, field) => {
if(err) throw err;
res.render('update', {data: answer});
});
});
})
app.get('/add', (req, res) => {
res.render('add');
})
app.post('/add', (req, res) => {
var inc = req.body.symbol;
var sec = req.body.sector;
var val = parseInt(req.body.shares);
q2 = `INSERT into Stocks (stockName, sector, unitPrice) values ('${inc}', '${sec}', ${val});`;
dbms.query(q2, (err, result, fields) => {
if (err) throw err;
q1 = `Insert into StockPrice (stockName, time, Price) values ('${inc}',NOW(), ${val})`;
dbms.query(q1, (err, result, fields) => {
if (err) throw err;
});
res.render('add');
});
})
app.listen(4007, () => {
console.log('server started on port 4007')
});