-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
79 lines (70 loc) · 1.6 KB
/
controller.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
const express = require('express');
const model = require('./model.js');
const router = express.Router();
router.get('/', (req, res)=>{
if(req.session.active_id){
res.redirect('/index');
}
else{
res.redirect('/login');
}
})
router.get('/login', (req, res)=>{
//req.session.active_id = 1;
if(req.session.active_id){
res.redirect('/index');
}else{
res.render('login');
}
})
router.post('/login', (req, res)=>{
console.log(req.body);
model.login_validation(req.body)
.then((ret)=>{
console.log(ret);
req.session.active_id = ret.Id;
res.redirect('/')
})
.catch((err)=>{
console.log(err);
res.redirect('/');
})
})
router.get('/index', (req, res)=>{
if(req.session.active_id){
model.get_all_books()
.then((books)=>{
// console.log(books);
res.render('index', {data:books});
})
.catch((err)=>{
console.log(err);
})
}
else{
res.redirect('/login');
}
})
router.post('/book', (req, res)=>{
console.log(req.body);
model.get_reviews(req.body)
.then((reviews)=>{
res.render('book', {book:req.body, reviews:reviews});
})
.catch((err)=>{
console.log(err);
})
})
router.post('/post', (req, res)=>{
console.log(req.body);
model.add_review(req.body, req.session.active_id)
.then((msg)=>{
console.log(msg);
res.redirect('/index');
})
})
router.get('/logout', (req, res)=>{
req.session.destroy();
res.redirect('/')
})
module.exports = router;