-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
414 lines (318 loc) · 14.2 KB
/
server.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
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const mysql = require('mysql2');
const session = require('express-session');
const flash = require('express-flash');
const nodemailer = require('nodemailer');
const randomstring = require('randomstring');
const multer = require('multer');
const bcrypt = require('bcrypt');
const app = express();
const port = 8081;
app.use(session({ secret: 'your-secret-key', resave: false, saveUninitialized: true }));
app.use(flash());
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'nisarga',
database: 'details',
port: 3305,
namedPlaceholders: true
});
db.connect(err => {
if (err) {
console.error('Error connecting to MariaDB database:', err);
} else {
console.log('Connected to MariaDB database');
}
});
app.use(express.static('C:/Users/nisar/Desktop/Elearning'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'lzkjukwbhsbiukbv'
}
});
app.post('/index', (req, res) => {
const username = req.body.username;
const email = req.body.email;
const password = req.body.password;
if (!username || !email || !password) {
req.flash('error', 'Please fill in all the fields.');
res.redirect('/page-register.html');
return;
}
const checkEmailSql = 'SELECT * FROM signup WHERE email = ?';
db.query(checkEmailSql, [email], (checkEmailError, emailResults) => {
if (checkEmailError) {
console.error('Error checking email existence:', checkEmailError);
req.flash('error', 'Error during registration. Please try again.');
res.redirect('/page-register.html');
return;
}
if (emailResults.length > 0) {
req.flash('error', 'Email already exists. Please choose a different email.');
req.flash('existingEmail', email);
res.redirect('/page-register.html');
return;
}
const verificationCode = randomstring.generate(6);
const insertSql = 'INSERT INTO signup (username, email, password, verification_code) VALUES (?, ?, ?, ?)';
db.query(insertSql, [username, email, password, verificationCode], (insertError, results) => {
if (insertError) {
console.error('Error inserting data into the database:', insertError);
req.flash('error', 'Error during registration. Please try again.');
res.redirect('/page-register.html');
} else {
console.log('User data inserted successfully');
const mailOptions = {
from: '[email protected]',
to: email,
subject: 'Email Verification',
text: `Your verification code is: ${verificationCode}`
};
transporter.sendMail(mailOptions, (emailError, info) => {
if (emailError) {
console.error('Error sending verification email:', emailError);
req.flash('error', 'Error sending verification email. Please contact support.');
res.redirect('/otp.html');
} else {
console.log('Verification email sent:', info.response);
req.session.userId = results.insertId;
req.flash('success', 'Signup successful! Check your email for verification.');
res.redirect('/otp.html');
}
});
}
});
});
});
app.post('/verify-otp', (req, res) => {
const otp = req.body.otp;
const userId = req.session.userId;
if (!userId || !otp) {
req.flash('error', 'Invalid request.');
res.redirect('/page-register.html');
return;
}
const sql = 'SELECT * FROM signup WHERE id = ? AND verification_code = ?';
db.query(sql, [userId, otp], (err, results) => {
if (err) {
console.error('Error verifying OTP:', err);
req.flash('error', 'Error verifying OTP. Please try again.');
res.redirect('/otp.html');
} else if (results.length === 1) {
const updateSql = 'UPDATE signup SET is_verified = true WHERE id = ?';
db.query(updateSql, [userId], (updateErr) => {
if (updateErr) {
console.error('Error updating user verification status:', updateErr);
req.flash('error', 'Error verifying OTP. Please try again.');
res.redirect('/otp.html');
} else {
const { username, email } = results[0];
res.redirect(`/app-profile.html?username=${username}&email=${email}`);
}
});
} else {
req.flash('error', 'Invalid OTP. Please try again.');
res.redirect('/otp.html');
}
});
});
app.post('/login', (req, res) => {
const email = req.body.email;
const password = req.body.Password;
if (!email || !password) {
req.flash('error', 'Please enter both email and password.');
return res.redirect('/page-login.html');
}
const loginSql = 'SELECT * FROM signup WHERE email = ? AND is_verified = true';
db.query(loginSql, [email], (loginError, loginResults) => {
if (loginError) {
console.error('Error during login:', loginError);
req.flash('error', 'Error during login. Please try again.');
return res.redirect('/page-login.html');
}
if (loginResults.length === 1) {
const storedHashedPassword = loginResults[0].password;
bcrypt.compare(password, storedHashedPassword, (bcryptError, passwordMatch) => {
if (bcryptError || !passwordMatch) {
req.flash('error', 'Invalid email or password. Please try again.');
return res.redirect('/page-login.html');
}
const { id, username, email } = loginResults[0];
req.session.userId = id;
return res.redirect(`/app-profile.html?username=${username}&email=${email}`);
});
} else {
req.flash('error', 'Invalid email or password. Please try again.');
return res.redirect('/page-login.html');
}
});
});
app.post('/add-courses', (req, res) => {
const {
courseName,
courseCode,
courseDetails,
courseDuration,
coursePrice,
professorName,
maxStudents,
contactNumber
} = req.body;
if (!courseCode) {
console.error('Error inserting course data into the database: Course Code cannot be null');
req.flash('error', 'Course Code cannot be empty.');
res.redirect('/add-courses.html');
return;
}
const sql = 'INSERT INTO course (course_name, course_code, course_details, course_duration, course_price, professor_name, max_students, contact_number) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
db.query(sql, [courseName, courseCode, courseDetails, courseDuration, coursePrice, professorName, maxStudents, contactNumber], (err) => {
if (err) {
console.error('Error inserting course data into the database:', err);
req.flash('error', 'Error adding course. Please try again.');
res.redirect('/add-courses.html');
} else {
console.log('Course data inserted successfully');
req.flash('success', 'Course added successfully!');
res.redirect(`/courses.html?courseName=${courseName}&courseCode=${courseCode}&courseDetails=${courseDetails}&courseDuration=${courseDuration}&coursePrice=${coursePrice}&professorName=${professorName}&maxStudents=${maxStudents}&contactNumber=${contactNumber}`);
}
});
});
app.get('/get-courses', (req, res) => {
const sql = 'SELECT * FROM course';
db.query(sql, (err, results) => {
if (err) {
console.error('Error fetching courses from the database:', err);
res.status(500).json({ error: 'Internal Server Error' });
} else {
res.json(results);
}
});
});
app.post('/add-student', (req, res) => {
const {
firstName,
lastName,
email,
rollNo,
className,
gender,
mobileNumber,
parentsName,
parentsMobileNumber,
bloodGroup,
address
} = req.body;
if (!firstName || !lastName || !email || !rollNo || !className || !gender || !mobileNumber) {
req.flash('error', 'Please fill in all the mandatory fields.');
res.redirect('/add-student.html');
return;
}
const sql = 'INSERT INTO student (first_name, last_name, email, roll_no, class, gender, mobile_number, parents_name, parents_mobile_number, blood_group, address) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
db.query(sql, [firstName, lastName, email, rollNo, className, gender, mobileNumber, parentsName, parentsMobileNumber, bloodGroup, address], (err) => {
if (err) {
console.error('Error inserting student data into the database:', err);
req.flash('error', 'Error adding student. Please try again.');
res.redirect('/add-student.html');
} else {
console.log('Student data inserted successfully');
req.flash('success', 'Student added successfully!');
res.redirect(`/students.html?firstName=${firstName}&lastName=${lastName}&email=${email}&rollNo=${rollNo}&className=${className}&gender=${gender}&mobileNumber=${mobileNumber}&parentsName=${parentsName}&parentsMobileNumber=${parentsMobileNumber}&bloodGroup=${bloodGroup}&address=${address}`);
}
});
});
app.get('/get-students', (req, res) => {
const sql = 'SELECT * FROM student';
db.query(sql, (err, results) => {
if (err) {
console.error('Error fetching students from the database:', err);
res.status(500).json({ error: 'Internal Server Error' });
} else {
res.json(results);
}
});
});
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
app.post('/add-professor', upload.single('file'), (req, res) => {
const {
firstname,
lastname,
email,
password,
confirmpassword,
mobilenumber,
gender,
designation,
department,
education
} = req.body;
const fileData = req.file ? req.file.buffer : null;
if (!firstname || !lastname || !email || !password || !confirmpassword || !mobilenumber || !gender || !designation || !department || !education) {
req.flash('error', 'Please fill in all the mandatory fields.');
res.redirect('/add-professor.html');
return;
}
const sql = 'INSERT INTO professor (firstname, lastname, email, password, confirmpassword, mobilenumber, gender, designation, department, education, file) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
db.query(sql, [firstname, lastname, email, password, confirmpassword, mobilenumber, gender, designation, department, education, fileData], (err) => {
if (err) {
console.error('Error inserting professor data into the database:', err);
req.flash('error', 'Error adding professor. Please try again.');
res.redirect('/add-professor.html');
} else {
console.log('Professor data inserted successfully');
res.redirect(`/professors.html?firstname=${firstname}&lastname=${lastname}&email=${email}&mobilenumber=${mobilenumber}&gender=${gender}&designation=${designation}&department=${department}&education=${education}`);
}
});
});
app.get('/professors.html', (req, res) => {
const sql = 'SELECT professor_id, firstname, lastname, email, mobilenumber, gender, designation, department, education FROM professor';
db.query(sql, (err, results) => {
if (err) {
console.error('Error fetching professors from the database:', err);
res.status(500).json({ error: 'Internal Server Error' });
} else {
res.render('professors.html', { professors: results });
}
});
});
app.post('/add-assessment', (req, res) => {
const { question, option1, option2, option3, option4 } = req.body;
const sql = `INSERT INTO assessment_questions (question, option1, option2, option3, option4)
VALUES (?, ?, ?, ?, ?)`;
db.query(sql, [question, option1, option2, option3, option4], (err, result) => {
if (err) {
console.error('Error inserting assessment:', err);
res.status(500).send('Error inserting assessment');
} else {
console.log('Assessment added successfully');
// Redirect to the "nextpage" after successful assessment addition
res.redirect(`/assessment-details?question=${encodeURIComponent(question)}&option1=${encodeURIComponent(option1)}&option2=${encodeURIComponent(option2)}&option3=${encodeURIComponent(option3)}&option4=${encodeURIComponent(option4)}`);
}
});
});
app.get('/assessment-details', (req, res) => {
// Get the query parameters
const { question, option1, option2, option3, option4 } = req.query;
// Select rows from the assessment_questions table based on the provided query parameters
const sql = `SELECT * FROM assessment_questions WHERE question = ? AND option1 = ? AND option2 = ? AND option3 = ? AND option4 = ?`;
// Execute the query with the provided parameters
db.query(sql, [question, option1, option2, option3, option4], (err, result) => {
if (err) {
console.error('Error fetching assessment details:', err);
res.status(500).send('Error fetching assessment details');
} else {
// Send the result as JSON response
res.status(200).json(result);
}
});
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});