-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
443 lines (330 loc) · 12.6 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
const express=require('express');
const mysql=require('mysql');
const path=require('path');
const session = require('express-session');
//create connection
const db = mysql.createConnection({
host:'localhost',
user:'root',
password:'viratkohli18',
database:'elibrary',
multipleStatements:true,
});
db.connect((err)=>{
if(err){
throw err;
}
console.log('connected');
});
//run every time a request is made
const logger=(req,res,next)=>{
console.log(`${req.protocol}://${req.get('host')}${req.originalUrl}`);
next();
};
const app=express();
const bodyparser=require('body-parser');
app.use(bodyparser.json());
app.use(express.static(path.join(__dirname,'global')));
app.use(logger);
app.set('view engine','ejs');
app.set('views',path.join(__dirname,'views'));
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
app.use(bodyparser.urlencoded({extended : true}));
//routes
app.get('/home',(req,res)=>{
if(req.session.loggedin){
let sql;
if(req.session.key){
sql="Select book.Book_name,author.name,book.price from author join written_by on author.id=written_by.author_id join book on written_by.book_id=book.id where book.Book_name like '%"+req.session.keyword+"%'";
req.session.key=false;
}else {
sql="Select book.Book_name,author.name,book.price,book.version,book.size from author join written_by on author.id=written_by.author_id join book on written_by.book_id=book.id order by book.id";
}
db.query(sql,(err,rows,fields)=>{
if(err) throw err;
let name,username;
console.log(name,username);
res.render('home',{
rows:rows,
username:req.session.username
});
});
}else {
res.send("Please login to view this page");
}
});
app.post('/pay',(req,res)=>{
let sql="INSERT INTO `elibrary`.`payment` (`Id`, `Date`, `Total`, `Cart_id`) VALUES (?, (Select Date from cart where id=?), ?, ?);";
sql+="DELETE FROM cart where id=?;"
let c=req.session.cart;
console.log(c);
req.session.cart++;
let t=req.session.total;
req.session.total=0;
db.query(sql,[c,c,t,c],(err,rows,fields)=>{
res.redirect('/user');
});
});
app.get('/cart',(req,res)=>{
let sql;
sql="Select book.id,book.Book_name,book.price from book join added on book.id=added.Book_id where added.id=?";
db.query(sql,[req.session.cart],(err,rows,fields)=>{
if(err) throw err;
sql="Select sum(book.price) as total from book join added on book.id=added.Book_id where added.id=?;";
db.query(sql,[req.session.cart],(err,results,fields)=>{
req.session.total=results[0].total;
res.render('cart',{
rows:rows,
total:results[0].total
});
})
});
});
app.post('/buy/:id?', (req, res)=> {
var bookid=req.params.id;
let sql="INSERT INTO `elibrary`.`added` (`Id`, `Username`, `Book_id`, `Price`) VALUES (?, ?, ?, (Select book.price from book where book.id=?));";
db.query(sql,[req.session.cart,req.session.name,bookid,bookid],(err,rows,fields)=>{
res.redirect('/user');
});
});
app.post('/remove/:id?', (req, res)=> {
var bookid=req.params.id;
let sql="DELETE FROM `elibrary`.`added` WHERE added.Book_id=?;";
db.query(sql,[bookid],(err,rows,fields)=>{
res.redirect('/cart');
});
});
app.get('/user',(req,res)=>{
let v=db.query("Select * from book");
console.log(req.session.cart);
if(req.session.loggedin){
let sql;
if(req.session.key){
sql="Select book.id,book.Book_name,author.name,book.price from author join written_by on author.id=written_by.author_id join book on written_by.book_id=book.id where book.Book_name like '%"+req.session.keyword+"%'";
req.session.key=false;
}else {
sql="Select book.id,book.Book_name,author.name,book.price,book.version,book.size from author join written_by on author.id=written_by.author_id join book on written_by.book_id=book.id order by book.id";
}
db.query(sql,(err,rows,fields)=>{
if(err) throw err;
let name,username;
console.log(name,username);
res.render('user',{
rows:rows,
username:req.session.username
});
});
}else {
res.send("Please login to view this page");
}
});
app.get('/log', function(req, res) {
res.render('login');
});
app.post('/out', function(req, res) {
req.session.loggedin=false;
req.session.username=undefined;
req.session.name=undefined;
let sql="DELETE FROM cart";
db.query(sql);
res.redirect('/');
});
app.post('/auth', (req, res)=> {
var username0 = req.body.admin;
var password0 = req.body.code;
if (username0 && password0) {
db.query('SELECT * FROM user join admin on user.username=admin.username WHERE user.username = ? AND password = ?', [username0, password0],(error, results, fields)=> {
if (results.length > 0) {
console.log(results);
req.session.loggedin = true;
req.session.name=results[0].Username;
req.session.username=results[0].Name;
req.session.key=false;
res.redirect('/home');
} else {
res.send('Incorrect Username and/or Password!');
res.end();
}
res.end();
});
}else {
res.send('Please enter Username and Password!');
res.end();
}
});
app.post('/member', (req, res)=> {
var username1 = req.body.user;
var password1 = req.body.pass;
if (username1 && password1) {
db.query('SELECT * FROM user join elibrary.members on user.username=members.username WHERE user.username = ? AND password = ?', [username1, password1],(error, results, fields)=> {
if (results.length > 0) {
req.session.loggedin = true;
req.session.name=results[0].Username;
req.session.username=results[0].Name;
req.session.key=false;
sql="SELECT * from payment";
db.query(sql,(err,rows,fields)=>{
let i=rows.length+1;
req.session.cart=i;
sql="DELETE FROM added where added.id=?";
db.query(sql,[i]);
sql="insert into cart select * from ( select ?, date(now())) as temp where not exists (Select * from cart where cart.id=?) limit 1; "
db.query(sql,[i,i]);
res.redirect('/user');
res.end();
});
} else {
res.send('Incorrect Username and/or Password!');
res.end();
}
});
} else {
res.send('Please enter Username and Password!');
res.end();
}
});
app.post('/up',(req,res)=>{
let username=req.body.username;
let fullname=req.body.fullname;
let contact=req.body.contact;
let email=req.body.email;
let password=req.body.password;
let sql="SELECT * FROM user WHERE Username = ?";
db.query(sql,[username],(err,rows,fields)=>{
if(rows.length>0){
res.send("Username Already Taken");
}else {
sql="INSERT INTO `elibrary`.`user` (`Username`, `Name`, `Contact`, `Email`, `Password`) VALUES (?, ?, ?, ?, ?); INSERT INTO `elibrary`.`members` (`Username`, `Download`, `Spent`) VALUES (?, '0', '0');";
db.query(sql,[username,fullname,contact,email,password,username]);
res.redirect('/');
}
});
});
app.post('/search',(req, res) => {
req.session.keyword=req.body.keyword;
req.session.key=true;
res.redirect('/home');
});
app.post('/nosearch',(req, res) => {
req.session.keyword=req.body.keyword;
req.session.key=true;
res.redirect('/user');
});
app.get('/popular',(req,res)=>{
let sql="Select book.Book_name, count(added.Book_id) as num from added join book on book.id=added.Book_id group By book.id order by num desc;";
db.query(sql,(err,rows,fields)=>{
if(err) throw err;
res.render('popular',{
rows:rows
});
});
});
app.get('/author',(req,res)=>{
let sql="Select * from author";
db.query(sql,(err,rows,fields)=>{
if(err) throw err;
res.render('author',{
rows:rows
});
});
});
app.get('/noauthor',(req,res)=>{
let sql="Select * from author";
db.query(sql,(err,rows,fields)=>{
if(err) throw err;
res.render('noauthor',{
rows:rows
});
});
});
app.get('/noabout',(req,res)=>{
let sql="Select Name from user join admin where user.Username=admin.Username";
db.query(sql,(err,rows,fields)=>{
if(err) throw err;
res.render('noabout',{
rows:rows
});
});
});
app.get('/sales',(req,res)=>{
let sql="SELECT Id as Cart_id, DATE_FORMAT(Date,'%y/%m/%d') as date, Total as Revenue FROM elibrary.payment;";
db.query(sql,(err,rows,fields)=>{
if(err) throw err;
res.render('sales',{
rows:rows
});
});
});
app.get('/demand',(req,res)=>{
res.render('demand');
});
app.get('/book',(req,res)=>{
res.render('book');
});
app.get('/list',(req,res)=>{
let sql="SELECT * from demand;";
db.query(sql,(err,rows,fields)=>{
if(err) throw err;
res.render('list',{
rows:rows
});
});
});
app.post('/need',(req,res)=>{
let id,version,authorname,bookname;
version=req.body.version;
authorname=req.body.authorname;
bookname=req.body.bookname;
let sql="SELECT * from demand";
db.query(sql,(err,rows,fields)=>{
if(err) throw err;
id=rows.length+1;
db.query(sql,(err,rows,fields)=>{
if(err) throw err;
sql="INSERT INTO `elibrary`.`demand` (`Serial`, `Author`, `Version`, `Book_name`) VALUES (?, ?, ?, ?);";
db.query(sql,[id,authorname,version,bookname]);
res.redirect('/user');
});
});
});
app.post('/add',(req,res)=>{
let id,price,version,size,authorname,bookname,auth_id,pubname;
price=req.body.price;
version=req.body.version;
size=req.body.size;
authorname=req.body.authorname;
bookname=req.body.bookname;
pubname=req.body.pubname;
let sql="SELECT * from book";
db.query(sql,(err,rows,fields)=>{
if(err) throw err;
id=rows.length+1;
sql="SELECT * from author";
db.query(sql,(err,rows,fields)=>{
if(err) throw err;
let i=rows.length;
sql="SELECT * from author where name=?";
my="INSERT INTO `elibrary`.`book` (`id`, `Price`, `Version`, `Size`, `Author`, `Book_name`, `Author_id`, `Publisher_name`, `Username`) VALUES (?,?,?,?,?,?,?,?,?); ";
db.query(sql,[authorname],(err,rows,fields)=>{
if(err) throw err;
if(rows.length>0){
auth_id=rows[0].Id;
console.log(rows);
}else {
auth_id=(i+1);
}
my+="INSERT INTO `elibrary`.`author` (`Name`, `Id`, `Genre`) SELECT * FROM (SELECT ?, ?, 'any') as temp where not exists ( select * from author where name=?) limit 1;";
my+="INSERT INTO `elibrary`.`written_by` (`author_Id`, `book_id`) VALUES (?, ?)";
db.query(my,[id,price,version,size,authorname,bookname,auth_id,pubname,req.session.name,authorname,auth_id,authorname,auth_id,id]);
res.redirect('/home');
});
});
});
});
app.listen('3300', ()=>{
console.log("server started");
});