-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
194 lines (174 loc) · 4.86 KB
/
index.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
const express = require('express');
const app = express();
const quizzes = require('./text.json').quizzes;
const categories = require('./categories.json').categories;
const port = 8000;
//API Design
//To get all the Categories of the Quiz - /categories
//to get a particular category quiz /categories/category
//To create a Quiz of 10 Questions of a particular category /categories/category/create-Quiz
//To create random 10 Question Quizzes /random-Quiz
const endpoints = [
{ method: 'GET', path: '/', description: 'Home endpoint' },
{
method: 'GET',
path: '/categories',
description: 'Get the List of all the Categories Available',
},
{
method: 'GET',
path: '/categories/category',
description: 'Generate A Quiz of 10 Questions of this particular Category',
},
{
method: 'GET',
path: '/generate-random',
description: 'Create a Random Quiz of Random Difficulty',
},
{
method: 'GET',
path: '/generate-random/easy',
description: 'Create a Random Quiz of difficulty level as Easy',
},
{
method: 'GET',
path: '/generate-random/medium',
description: 'Create a Random Quiz of difficulty level as Medium',
},
{
method: 'GET',
path: '/generate-random/hard',
description: 'Create a Random Quiz of difficulty level as Hard',
},
];
app.get('/', (req, res) => {
let html = `
<!DOCTYPE html>
<html>
<head>
<title>API Endpoints</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 10px;
text-align: left;
border: 1px solid #ddd;
}
th {
background-color: #f4f4f4;
}
</style>
</head>
<body>
<h1>Available API Endpoints</h1>
<table>
<thead>
<tr>
<th>Method</th>
<th>Path</th>
<th>Description</th>
</tr>
</thead>
<tbody>
${endpoints
.map(
(endpoint) => `
<tr>
<td>${endpoint.method}</td>
<td>${endpoint.path}</td>
<td>${endpoint.description}</td>
</tr>`
)
.join('')}
</tbody>
</table>
</body>
</html>
`;
res.send(html);
});
app.get('/categories', (req, res) => {
if (categories.length === 0) {
return res.status(404).json({ message: 'No Categories Found' });
}
return res.json(categories);
});
app.get('/categories/:cat', (req, res) => {
const cat = req.params.cat.trim().toLowerCase();
if (cat == '') {
return res.status(404).json({
message: 'Category Not Found Please Enter the Cat ID correctly',
});
}
const filteredQuizzes = quizzes.filter(
(quiz) => quiz.category.toLowerCase() === cat
);
if (filteredQuizzes.length === 0) {
return res
.status(404)
.json({ message: `No quizzes found for the category '${cat}'` });
}
const limit = 10;
const quiz = filteredQuizzes.slice(0, limit);
res.json({
category: cat,
totalQuestions: filteredQuizzes.length,
quiz,
});
});
app.get('/generate-random', (req, res) => {
const randomQuestions = [];
const usedIndices = new Set();
// Ensure 10 unique random questions are selected
while (randomQuestions.length < 10) {
const randomIndex = Math.floor(Math.random() * quizzes.length);
if (!usedIndices.has(randomIndex)) {
randomQuestions.push(quizzes[randomIndex]);
usedIndices.add(randomIndex);
}
}
res.json(randomQuestions);
});
app.get('/generate-random/:diff', (req, res) => {
const diff = req.params.diff.toLowerCase().trim(); // Get difficulty from route parameter
// Filter questions based on the difficulty level
const filteredQuestions = quizzes.filter(
(q) => q.difficulty.toLowerCase() === diff
);
if (filteredQuestions.length === 0) {
return res
.status(404)
.json({
error: 'No questions found for the specified difficulty level.',
});
}
const randomQuestions = [];
const usedIndices = new Set();
// Ensure up to 10 unique random questions are selected from the filtered list
while (
randomQuestions.length < 10 &&
randomQuestions.length < filteredQuestions.length
) {
const randomIndex = Math.floor(Math.random() * filteredQuestions.length);
if (!usedIndices.has(randomIndex)) {
randomQuestions.push(filteredQuestions[randomIndex]);
usedIndices.add(randomIndex);
}
}
res.json(randomQuestions);
});
app.use((req, res) => {
res
.status(404)
.json({ message: 'Trivia Not available Read Journals for now..' });
});
app.listen(port, () =>
console.log('Server Running Successfully on ' + port + ' Port')
);