-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
78 lines (65 loc) · 1.76 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
const express = require('express');
const QuizAPI = require('./resources/QuizAPI');
const { isAnswerCorrect, evaluateUserPerformance } = require('./resources/QuizController');
const app = express();
require('dotenv').config();
app.use(express.json());
app.post('/user', async (req, res) => {
const {username, startingDifficulty} = req.body;
if(!username) {
return res.status(400).json({
status: 'error',
message: 'Bad request'
});
}
let userObj = null;
if(!userObj) {
userObj = {
id: Date.now(),
username,
currentDifficulty: startingDifficulty
}
}
res.status(201).json({
userObj
})
});
app.post('/check-answer', async(req, res, next) => {
const {
questionBlock,
userAnswer
} = req.body;
const isCorrect = isAnswerCorrect(questionBlock, userAnswer);
res.status(200).json({
result: isCorrect
})
});
app.post('/check-performance', async(req, res, next) => {
const {
currentDifficulty,
currentAttemptedQuestions
} = req.body;
const calculatedDifficulty = evaluateUserPerformance(currentAttemptedQuestions, currentDifficulty);
res.status(200).json({
difficulty: calculatedDifficulty
})
});
app.get('/quiz', async(req, res, next) => {
const {limit, difficulty} = req.query;
let quizQuestion = null;
try {
const data = await QuizAPI.getQuiz(limit, difficulty);
quizQuestion = data ?? null;
} catch (error) {
}
res.status(200).json({
userMeta: null,
questionInfo: quizQuestion
})
});
app.get('/', async(req, res) => {
res.status(200).json({
status: 'HEALTHY...'
})
});
module.exports = app;