forked from codenim34/QuizMasterApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
286 lines (261 loc) · 8.75 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
const http = require("http");
const url = require("url");
const querystring = require("querystring");
const UserController = require("./Controller/userController");
const UserView = require("./View/userView");
const AdminController = require("./Controller/adminController");
const AdminView = require("./View/adminView");
const { addQuiz, addSubQuiz, takeQuiz, loadLeaderboard, getMistakenQuestions, getUserQuizHistory, getRandomQuizzes,getRandomSubQuizzes } = require("./Model/quizModel");
const QuizView = require("./View/quizView");
const server = http.createServer((req, res) => {
const userController = new UserController();
const userView = new UserView();
const adminController = new AdminController();
const adminView = new AdminView();
const quizView = new QuizView();
const { pathname } = url.parse(req.url);
//user register
if (req.method === "POST" && pathname === "/register") {
let data = "";
req.on("data", (chunk) => {
data += chunk;
});
req.on("end", () => {
try {
const registeredUser = userController.registerUser(data);
delete registeredUser.accessToken;
userView.sendSuccessResponse(
res,
"User registered successfully",
registeredUser
);
} catch (error) {
userView.sendErrorResponse(res, 400, error.message);
}
});
}
//user login
else if (req.method === "POST" && pathname === "/login") {
let data = "";
req.on("data", (chunk) => {
data += chunk;
});
req.on("end", () => {
try {
const result = userController.loginUser(data);
if (result) {
const { user, accessToken } = result;
delete user.password;
delete user.accessToken;
userView.sendLogInSuccessResponse(res, "Login successful", {
user,
access_token: accessToken,
});
} else {
userView.sendErrorResponse(res, 401, "Authentication failed");
}
} catch (error) {
userView.sendErrorResponse(res, 400, error.message);
}
});
}
// admin register
else if (req.method === "POST" && pathname === "/admin/register") {
let data = "";
req.on("data", (chunk) => {
data += chunk;
});
req.on("end", () => {
try {
const registeredAdmin = adminController.registerAdmin(data);
delete registeredAdmin.accessToken;
adminView.sendSuccessResponse(
res,
"Admin registered successfully",
registeredAdmin
);
} catch (error) {
adminView.sendErrorResponse(res, 400, error.message);
}
});
}
//admin login
else if (req.method === "POST" && pathname === "/admin/login") {
let data = "";
req.on("data", (chunk) => {
data += chunk;
});
req.on("end", () => {
try {
const loginResult = adminController.loginAdmin(data);
if (loginResult) {
const { admin, accessToken } = loginResult;
delete admin.password;
adminView.sendLogInSuccessResponse(res, "Login successful", {
admin,
access_token: accessToken,
});
} else {
adminView.sendErrorResponse(res, 401, "Authentication failed");
}
} catch (error) {
adminView.sendErrorResponse(res, 400, error.message);
}
});
}
// physics subject wise quiz adding
else if (req.method === "POST" && pathname.startsWith("/admin/addQuiz/")) {
const subject = pathname.split('/').pop();
adminController.authenticateAdmin(req, res, () => {
let data = "";
req.on("data", (chunk) => {
data += chunk;
});
req.on("end", () => {
const { question, options, correctAnswers } = JSON.parse(
data
);
try {
const addedQuiz = addSubQuiz(
question,
options,
correctAnswers,
subject
);
const quizResponse = {
question: addedQuiz.question,
options: addedQuiz.options,
questionID: addedQuiz.questionID,
};
quizView.sendSuccessResponse(
res,
"Quiz added successfully",
quizResponse
);
} catch (error) {
console.error(error);
quizView.sendErrorResponse(res, 400, error.message);
}
});
});
}
//get quiz by id
else if (req.method === "GET" && req.url === "/user/takeQuiz") {
try {
const randomQuizzes = getRandomQuizzes();
quizView.sendSuccessResponse(res, "10 random quizzes fetched successfully", randomQuizzes);
} catch (error) {
quizView.sendErrorResponse(res, 500, "Internal Server Error");
}
}
//get physics subject wise quiz by id
else if (req.method === "GET" && pathname.startsWith("/user/takeQuiz/")) {
const subject = pathname.split('/').pop();
try {
const randomQuizzes = getRandomSubQuizzes(subject);
quizView.sendSuccessResponse(res, `Random quizzes fetched from ${subject} successfully` , randomQuizzes);
} catch (error) {
quizView.sendErrorResponse(res, 500, "Internal Server Error");
}
}
//submit quiz by user
else if (req.method === "POST" && req.url === "/user/submitQuiz") {
userController.authenticateUser(req, res, () => {
let body = "";
req.on("data", (chunk) => {
body += chunk.toString();
});
console.log(body);
req.on("end", () => {
try {
let userResponses = JSON.parse(body);
userResponses.username = req.user.username;
const result = takeQuiz(userResponses);
const response = {
Name: req.user.name,
Score: result.score,
Incorrect: result.noIncorrectQuestions,
"Total Questions": result.TotalQuestions,
"Success Rate": result.successRate + "%"
};
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(response));
} catch (error) {
res.writeHead(400, { "Content-Type": "application/json" });
res.end(
JSON.stringify({ error: "Invalid JSON in request body" })
);
}
});
});
}
//get mistaken questions by user
else if (req.method === "GET" && pathname.startsWith("/user/mistakes")) {
userController.authenticateUser(req, res, () => {
const username = req.user.username; // Assuming the username is stored in req.user
try {
const mistakenQuestions = getMistakenQuestions(username);
quizView.sendSuccessResponse(
res,
"Random mistaken quizzes fetched successfully",
mistakenQuestions
);
} catch (error) {
quizView.sendErrorResponse(res, 500, "Internal Server Error");
}
});
}
//get quiz history by user
else if (req.method === "GET" && pathname === "/user/quizhistory") {
userController.authenticateUser(req, res, () => {
const username = req.user.username; // Assuming the username is stored in req.user
try {
const quizHistory = getUserQuizHistory(username);
if (quizHistory) {
quizView.sendSuccessResponse(res, "Here is your quiz history:", quizHistory);
} else {
quizView.sendErrorResponse(res, 404, "User not found or no quiz history available");
}
} catch (error) {
quizView.sendErrorResponse(res, 500, "Internal Server Error");
}
});
}
//get leaderboard
else if (req.method === "GET" && req.url === "/leaderboard") {
try {
const leaderboard = loadLeaderboard();
// Calculate the total score for each user and sort the leaderboard
const sortedLeaderboard = leaderboard.map(entry => ({
Username: entry.username,
Score: (
entry.percentages.reduce((acc, cur) => acc + cur, 0) /
entry.percentages.length
).toFixed(2),
"Total Marks": entry.marks.reduce((acc, cur) => acc + cur, 0),
})).sort((a, b) => b.Score - a.Score);
let rank = 1;
for (let i = 0; i < sortedLeaderboard.length; i++) {
if (i > 0 && sortedLeaderboard[i].Score < sortedLeaderboard[i - 1].Score) {
rank = i + 1;
}
sortedLeaderboard[i].rank = rank;
sortedLeaderboard[i].Score = `${sortedLeaderboard[i].Score}%`;
}
const response = {
data: sortedLeaderboard
};
quizView.sendSuccessResponse(
res,
"Leaderboard fetched successfully",
response
);
} catch (error) {
quizView.sendErrorResponse(res, 500, "Internal Server Error");
}
}
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});