-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
196 lines (190 loc) · 6.48 KB
/
app.js
File metadata and controls
196 lines (190 loc) · 6.48 KB
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
import express from 'express'
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import * as DataBase from './database.js';
import session from 'express-session';
const app = express();
// for getting directory path
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Creating pool of connection to mysql
app.use(express.static('public'));
app.use("/scripts", express.static(__dirname + '/scripts'));
app.use(express.json());
app.use(session({
secret: "authentication_codes",
cookie: { maxAge: 1000 * 60 * 15 },
saveUninitialized: false,
resave: true
}));
// view engine
app.set('view engine', 'ejs');
app.listen(3000, () => console.log('Listening...'));
// app.use((err, req, res) => {
// console.log(err.stack)
// res.status(500).send('Something Broken wait ...')
// })
app.get('/', (req, res) => {
res.render('index', { page_title: 'StudyHelper' })
});
app.get('/login', async (req, res) => {
if (req.session.authenticated) {
const courses = await DataBase.getCoursesOfStudent(req.session.userId)
res.render('course', { page_title: "Course Page", userName: req.session.userName, courses: courses })
// TODO send course page with revelant links I mean handle here
} else {
res.render('login', { page_title: "Login | Signup" })
}
});
app.get('/signup', (req, res) => {
res.render('signup', { page_title: 'Sign Up' })
})
// TODO sign up later
app.post('/signup', async (req, res) => {
const { username, roll_no, age, email, password } = req.body;
const isAccountCreated = await DataBase.createUser(username, age, email, roll_no, password);
if (isAccountCreated.failed) {
res.json({ 'emailExsisting': 'true' })
} else {
req.session.authenticated = true;
req.session.userId = isAccountCreated.id
req.session.userName = isAccountCreated.username
res.json({ 'success': 'true' })
}
})
app.post('/login', async (req, res) => {
const { email, password } = req.body;
const result = await DataBase.checkUser(email, password)
if (result.failed) {
console.log('failed');
res.json({ failed: true })
} else {
console.log('User Authenticated...')
req.session.authenticated = true;
req.session.userId = result.id
req.session.userName = result.username
res.json({ success: true })
}
})
app.get('/logout', (req, res) => {
req.session.destroy((err) => {
if (err) {
console.log(err);
} else {
res.render('index', { page_title: 'StudyHelper' })
}
})
})
function isAuthenticated(req, res, next) {
if (req.session.authenticated) {
return next();
} else {
res.redirect('/login')
}
}
app.get('/course', isAuthenticated, async (req, res) => {
const courses = await DataBase.getCoursesOfStudent(req.session.userId);
res.render('course', { page_title: "Course Page", userName: req.session.userName, courses: courses })
})
app.post('/getcourse', isAuthenticated, (req, res) => {
const { course_id } = req.body;
req.session.courseId = course_id;
res.send({ yes: 'Yes' })
})
app.post('/createCourse', isAuthenticated, async (req, res) => {
const { newCourseName } = req.body;
const result = await DataBase.createCourse(req.session.userId, newCourseName)
if (result.insertId) {
res.json({ id: result.insertId, success: true });
} else {
res.json({ failed: true });
}
})
app.post('/editCourse', isAuthenticated, async (req, res) => {
const { newName, id } = req.body;
const result = await DataBase.updateCourse(id, newName);
if (result.changedRows) {
res.json({ success: true });
} else {
res.json({ failed: true });
}
})
app.post('/deleteCourse', isAuthenticated, async (req, res) => {
const { id } = req.body;
const result = await DataBase.deleteCourse(id);
if (result.affectedRows) {
console.log('yes?')
res.json({ success: true });
} else {
console.log('no?')
res.json({ failed: true });
}
})
app.get('/chapter', isAuthenticated, async (req, res) => {
const chapters = await DataBase.getChapters(req.session.courseId);
res.render('chapter', { page_title: 'Chapter Page', userName: req.session.userName, chapters: chapters });
})
app.post('/createChapter', isAuthenticated, async (req, res) => {
const { newChapterName } = req.body;
const result = await DataBase.createChapter(req.session.courseId, newChapterName)
if (result.insertId) {
res.json({ id: result.insertId, success: true });
} else {
res.json({ failed: true });
}
})
app.post('/editChapter', isAuthenticated, async (req, res) => {
const { newName, id } = req.body;
const result = await DataBase.updateChapter(id, newName);
if (result.changedRows) {
res.json({ success: true });
} else {
res.json({ failed: true });
}
})
app.post('/loadchapter', isAuthenticated, (req, res) => {
const { chapter_id } = req.body;
req.session.chapter_id = chapter_id;
res.send({ yes: 'yes' })
})
app.post('/deleteChapter', isAuthenticated, async (req, res) => {
const { id } = req.body;
const result = await DataBase.deleteChapter(id);
if (result.affectedRows) {
res.send({ success: true });
} else {
res.send({ failed: false });
}
})
app.get('/notes', isAuthenticated, async (req, res) => {
const notes = await DataBase.getNotes(req.session.chapter_id);
res.render('notes', { page_title: 'Notes Page', userName: req.session.userName, notes: notes });
})
app.post('/createNotes', isAuthenticated, async (req, res) => {
const { newNotesTitle, notes } = req.body;
const result = await DataBase.createNotes(req.session.chapter_id, newNotesTitle, notes);
if (result.insertId) {
res.json({ id: result.insertId, success: true });
} else {
res.json({ failed: true });
}
});
app.post('/editNotes', isAuthenticated, async (req, res) => {
const { id, topic, notes } = req.body;
const result = await DataBase.editNotes(id, topic, notes);
if (result.changedRows) {
res.send({ success: true });
} else {
res.send({ failed: true });
}
})
app.post('/deleteNotes', isAuthenticated, async (req, res) => {
const { id } = req.body;
// affectedRows
const result = await DataBase.deleteNotes(id);
if (result.affectedRows) {
res.send({ success: true });
} else {
res.send({ failed: true });
}
})