-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion.js
50 lines (46 loc) · 1.41 KB
/
question.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
// 导入模型
const Question = require('../models/question')
module.exports = {
async getAllQuestions(ctx, next) {
try {
const data = await Question.getAllQuestions()
ctx.body = { msg: 1001, data }
} catch (err) {
ctx.body = { code: -1, msg: 1000, }
}
await next();
},
async createQuestion(ctx, next) {
const req = ctx.request.body
if (req.topic && req.option && req.answer) {
try {
const data = await Question.createQuestion(req)
ctx.body = { msg: 1003, data }
} catch (err) {
ctx.body = { code: -1, msg: 1000 }
}
} else {
ctx.body = { code: -1, msg: 1002 }
}
await next();
},
async isCorrect(ctx, next) {
const req = ctx.request.body
if (req.answers) {
try {
await Promise.all(
req.answers.map(async answer => await Question.isCorrect(answer))
).then(results => {
console.log(results.length)
const data = results.length === 10 && results.every(result => result === true)
ctx.body = { msg: data ? 1007 : 1008, data }
})
} catch (err) {
ctx.body = { code: -1, msg: 1000 }
}
} else {
ctx.body = { code: -1, msg: 1002 }
}
await next();
}
}