-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz.js
104 lines (95 loc) · 2.83 KB
/
quiz.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
let QuizSchema = require('../models/Quiz.js').model
let QuizQuestionSchema = require('../models/QuizQuestion.js').model
const quiz_get_byType = async (req, res) => {
let type = req.query.type
try {
const quiz = await QuizSchema.find({ "language": type })
res.json({ quiz })
}
catch (error) {
res.statusCode = 500
res.json({ error: error.toString() })
}
}
const quiz_get_all = async (req, res) => {
try {
const quiz = await QuizSchema.find()
res.json({ quiz })
}
catch (error) {
res.statusCode = 500
res.json({ error: error.toString() })
}
}
const quiz_post_create = async () => {
const { name, questions, time } = req.body
try {
await QuizSchema.create({ name, questions, time })
res.statusCode = 200
res.json({ success: true, message: "Created new quiz" })
} catch (error) {
res.statusCode = 500
res.json({ error: error.toString() })
}
}
const quiz_patch_update = async (req, res) => {
const { id, name, questions, time } = req.body
try {
await QuizSchema.update({ _id: id }, { name, questions, time })
} catch (error) {
res.statusCode = 500
res.json({ error: error.toString() })
}
}
const quiz_delete_Quiz = async (req, res) => {
const { id } = req.body
try {
await QuizSchema.remove({ _id: id })
res.statusCode = 200
res.json({ success: true, message: "Deleted quiz" })
} catch (error) {
res.statusCode = 500
res.json({ error: error.toString() })
}
}
const quiz_post_Question = async (req, res) => {
const { question, answers, correctAnswer, quiz } = req.body
try {
await QuizQuestionSchema.create({ question, answers, correctAnswer, quiz })
res.statusCode = 200
res.json({ success: true, message: "Created new question" })
} catch (error) {
res.statusCode = 500
res.json({ error: error.toString() })
}
}
const quiz_patch_Question = async (req, res) => {
const { id, question, answers, correctAnswer, quiz } = req.body
try {
await QuizQuestionSchema.update({ _id: id }, { question, answers, correctAnswer, quiz })
} catch (error) {
res.statusCode = 500
res.json({ error: error.toString() })
}
}
const quiz_delete_Question = async (req, res) => {
const { id } = req.body
try {
await QuizQuestionSchema.remove({ _id: id })
res.statusCode = 200
res.json({ success: true, message: "Deleted question" })
} catch (error) {
res.statusCode = 500
res.json({ error: error.toString() })
}
}
module.exports = {
quiz_get_byType,
quiz_get_all,
quiz_post_create,
quiz_patch_update,
quiz_delete_Quiz,
quiz_post_Question,
quiz_patch_Question,
quiz_delete_Question
}