-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
259 lines (213 loc) · 7.46 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
const express = require('express');
const bodyParser = require('body-parser');
const ejsMate = require('ejs-mate')
const path = require('path')
const _ = require('lodash');
const mongoose = require('mongoose');
const session = require('express-session')
const { forEach } = require('lodash');
const flash = require('connect-flash')
const MongoStore = require('connect-mongo');
const cookieParser = require('cookie-parser');
const userController = require('./controllers/userController');
const { MongoClient } = require('mongodb');
const questionController = require('./controllers/questionController');
const questionModel = require('../question-paper-generator/models/question');
const { auth, isLogedIn } = require('./middlewares/auth');
const dbUrl = process.env.MONGODB_URL || 'mongodb://localhost:27017/reeloDB'
const secret = process.env.SECRET_KEY || "EDI@50";
require('dotenv').config();
const app = express();
const store = MongoStore.create({
mongoUrl: dbUrl,
secret,
touchAfter: 24 * 60 * 60
})
store.on('error', e => {
console.log('Session Error!!!', e)
})
app.engine('ejs', ejsMate);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.set('views', './views');
app.set('view engine', 'ejs');
const sessionConfig = {
store,
name: 'QuestForge',
secret,
resave: false,
saveUninitialized: true,
cookie: {
httpOnly: true, //just a extra layer of security to avoid client site req / cross-plaform req
// secure: true, //how extra security (sends no cookies for local host as well)
expires: Date.now() + (1000 * 60 * 60 * 24 * 7), //expires after 1 week of creation
maxAge: (1000 * 60 * 60 * 24 * 7)
}
}
app.use(session(sessionConfig))
app.use(flash())
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static("public"));
app.use(express.json());
app.use(cookieParser());
app.use((req, res, next) => {
next();
});
mongoose.connect(dbUrl, { useNewUrlParser: true })
.then(data => console.log("Database connected"))
.catch(err => console.log("Database connection failed"));
const client = new MongoClient(dbUrl, { useNewUrlParser: true, useUnifiedTopology: true });
//middleware for flashing all msg
app.use((req, res, next) => {
res.locals.currentUser = isLogedIn(req)
res.locals.success = req.flash('success') //this will send success to all the rendering requests
res.locals.warning = req.flash('warning')
res.locals.error = req.flash('error')
next()
})
app.get("/", function (req, res) {
res.render("home");
});
app.get("/login", function (req, res) {
res.render("login");
});
app.post("/signup", userController.signup);
app.get("/signup", function (req, res) {
res.render("signup");
});
app.post("/login", userController.login);
app.get("/logout", userController.logout);
app.get("/question", auth, function (req, res) {
res.render("topic");
});
app.get('/subject', auth, async (req, res) => {
res.render(`subject`);
});
async function topics(subject) {
try {
await client.connect();
const database = client.db('test');
const topicResult = await database.collection('questionBank').aggregate([
{ $match: { "subject": { $regex: new RegExp("^" + subject, "i") } } },
{ $group: { _id: "$topic" } }
]).toArray();
return topicResult;
} finally {
await client.close();
}
}
app.get("/topic", auth, async function (req, res) {
try {
let subject = req.query.subject;
subject = subject[0].toUpperCase() + subject.slice(1);
let topicResults = await topics(subject);
let distinctTopics = topicResults.map(item => item._id);
res.render("topic", { distinctTopics, subject });
} catch (error) {
console.error('Error fetching topics:', error);
res.status(500).send('Internal Server Error');
}
});
async function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
async function generateQuestionPaper(totalMarks, difficultyDistribution, questions) {
// Filter questions based on marks
const easyQuestions = questions.filter(question => question.marks === 1);
const mediumQuestions = questions.filter(question => question.marks === 4);
const hardQuestions = questions.filter(question => question.marks === 6);
// Calculate the number of questions for each difficulty level
const easyCount = Math.round((totalMarks * (difficultyDistribution.easy / 100)) / 3);
const mediumCount = Math.round((totalMarks * (difficultyDistribution.medium / 100)) / 5);
const hardCount = Math.round((totalMarks * (difficultyDistribution.hard / 100)) / 7);
// Shuffle the filtered questions array to randomize question selection
const shuffledEasyQuestions = await shuffleArray(easyQuestions);
const shuffledMediumQuestions = await shuffleArray(mediumQuestions);
const shuffledHardQuestions = await shuffleArray(hardQuestions);
// Select questions based on the calculated counts for each difficulty level
const questionPaper = shuffledEasyQuestions.slice(0, easyCount)
.concat(shuffledMediumQuestions.slice(0, mediumCount))
.concat(shuffledHardQuestions.slice(0, hardCount));
return questionPaper;
}
async function runQuery(topicsArray, subject, difficulty, totalMarks) {
try {
await client.connect();
const database = client.db('test');
const aggregationResult = await database.collection('questionBank').aggregate([
{ $match: { "subject": subject, "topic": { $in: topicsArray } } }
]).toArray();
const difficultyDistribution = {
easy: difficulty.easy,
medium: difficulty.medium,
hard: difficulty.hard
};
const questionPaper = await generateQuestionPaper(totalMarks, difficultyDistribution, aggregationResult);
return questionPaper;
} finally {
await client.close();
}
}
app.get('/paper', auth, async (req, res) => {
try {
let topicsArray = toArray(req.query.topics);
let subject = req.query.subject;
let totalMarks = req.query.totalMarks;
let difficulty = {
easy: req.query.easy,
medium: req.query.medium,
hard: req.query.hard
}
let paperName = req.query.paperName;
let userId = req.userId;
const questionArray = await runQuery(topicsArray, subject, difficulty, totalMarks);
res.render('paper', { questionArray, topicsArray, difficulty, totalMarks, paperName, userId });
} catch (error) {
console.error('Error in /paper route:', error);
res.status(500).send('Internal Server Error');
}
});
app.post('/save', async (req, res) => {
try {
await questionController.createQuestion(req, res);
} catch (error) {
console.error('Error in /save route:', error);
res.status(500).send('Internal Server Error');
}
});
async function getQuestionsByUserId(userId) {
try {
const questions = await questionModel.find({ createdBy: userId });
return questions;
} catch (err) {
console.error(err);
throw err;
}
}
app.get('/getlist', auth, async (req, res) => {
let userId = req.userId;
try {
const details = await getQuestionsByUserId(userId);
res.render('list', { papers: details });
} catch (err) {
console.error(err);
res.status(500).send('Internal Server Error');
}
});
function toArray(value) {
if (Array.isArray(value)) {
return value;
} else if (value) {
return [value];
} else {
return [];
}
}
app.listen(process.env.PORT || 3000, function () {
console.log("Server listening on Port 3000!");
});