-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz.js
More file actions
277 lines (223 loc) · 6.76 KB
/
Copy pathquiz.js
File metadata and controls
277 lines (223 loc) · 6.76 KB
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
const express = require('express')
const app = express()
app.use(express.json())
const jwt = require('jsonwebtoken')
const { authMiddleWare, SECRET_KEY, adminOnly } = require('./middleware')
const { userModel, QuizModel } = require('./models')
const bcrypt = require('bcrypt')
const {
signupSchema,
loginSchema,
quizSchema,
questionSchema,
validate
} = require("./zod")
app.post('/api/auth/signup', validate(signupSchema), async (req, res) => {
const { name, email, role, password } = req.body;
if (!name || !email || !role || !password) {
return res.status(400).json({
success: false,
error: "All fields are required"
})
}
const existing = await userModel.findOne({ name })
if (existing) {
return res.status(400).json({ message: "User already exists" })
}
const hashPass = await bcrypt.hash(password, 12);
const newUser = await userModel.create({
name,
email,
role,
password: hashPass
})
const token = jwt.sign(
{
id: newUser._id,
name: newUser.name,
email: newUser.email,
role: newUser.role
},
SECRET_KEY
)
res.status(201).json({
success: true,
token
})
})
app.post('/api/auth/login', validate(loginSchema), async (req, res) => {
const { name, password } = req.body
if (!name || !password) {
return res.status(400).json({ message: "Both fields are required" })
}
const user = await userModel.findOne({ name })
if (!user) {
return res.status(400).json({ message: "User not found" })
}
const isValid = await bcrypt.compare(password, user.password)
if (!isValid) {
return res.status(400).json({ message: "Wrong password" })
}
const token = jwt.sign(
{
id: user._id,
name: user.name,
email: user.email,
role: user.role
},
SECRET_KEY
)
res.json({
success: true,
token
})
})
app.get('/api/auth/me', authMiddleWare, async (req, res) => {
const user = await userModel.findById(req.userId)
res.json({ success: true, user })
})
// create new quiz
app.post('/api/quiz', adminOnly, validate(quizSchema), async(req, res) => {
const {title, questions} = req.body
if(!title || !Array.isArray(questions) || questions.length == 0){
return res.status(400).json({
success: false,
error: "Invalid request schema"
})
}
for(const q of questions){
if(!q.text || !Array.isArray(q.options) || typeof q.correctOptionIndex !== Number || q.options.length == 0){
return res.status(400).json({
success: false,
error: "Invalid request schema"
})
}
}
const newData = await QuizModel.create({
title,
questions
})
res.status(201).json({
success: true,
data: {_id: newData.id, title: newData.title}
})
})
// create new questions
app.post('/api/quiz/:quizId', adminOnly, validate(questionSchema), async(req, res) => {
const quizId = req.params.quizId
const {text, options, correctOptionIndex} = req.body
if(!text || typeof correctOptionIndex !== Number || !Array.isArray(options) || options.length == 0){
return res.status(400).json({
success: false,
error: "Invalid request schema"
})
}
const Quiz = await QuizModel.findById(quizId)
if(!Quiz){
return res.status(404).json({
success: false,
error: "Quiz not found"
})
}
const newQuestion = {
text,
options,
correctOptionIndex
}
Quiz.questions.push(newQuestion)
await Quiz.save()
res.status(201).json({
success: true,
data: {quizId, newQuestion}
})
})
app.get('api/quiz/:quizId', adminOnly, async(req, res) => {
const quizId = req.params.quizId
const Quiz = await QuizModel.findById(quizId)
if(!Quiz){
return res.status(404).json({
success: false,
error: "Quiz not found"
})
}
res.status(200).json({
success: true,
data: Quiz.questions
})
})
app.listen(3000)
// const express = require('express')
// const app = express()
// app.use(express.json())
// const {userModel, quizModel} = require('./models')
// const {middleware, SECRET_RANDOM} = require('./middleware')
// const jwt = require('jsonwebtoken')
// const {signupSchema, loginSchema, validate} = require('./zod')
// const bcrypt = require('bcrypt')
// app.post('/signup', validate(signupSchema), async(req, res) => {
// const {name, password, email, role} = req.body
// if(!name || !password || !email || !role){
// return res.status(400).json({
// success: false,
// message: "All fields are required"
// })
// }
// const existing = await userModel.fineOne(name)
// if(existing){
// return res.status(400).json({
// success: false,
// message: "user already exists, please log in"
// })
// }
// const hashPass = await bcrypt.hash(password, 13)
// const newUser = userModel.create({
// name,
// email,
// role,
// password: hashPass
// })
// const token = jwt.sign({id: newUser._id, email: newUser.email, role: newUser.role, password: newUser.password}, SECRET_RANDOM)
// res.status(201).json({
// success: true,
// token
// })
// })
// app.post('/login', validate(loginSchema), async(req, res) => {
// const {name, password} = req.body
// if(!name || !password){
// return res.status(400).json({
// success: false,
// message: "All fields are required"
// })
// }
// const user = await userModel.findOne(name)
// if(!user){
// return res.status(400).json({
// success: false,
// message: "user not found"
// })
// }
// const isValid = bcrypt.compare(password, user.password)
// if(!isValid){
// return res.status(400).json({
// success: false,
// message: "password is not correct"
// })
// }
// const token = jwt.sign({
// id: user._id,
// email: user.email,
// password: user.password,
// role: user.role
// }, SECRET_RANDOM)
// res.status(200).json({
// success: true,
// data: token
// })
// })
// app.get('/api/auth/me', middleware, async(req, res) => {
// const user = await userModel.findOne(req.user.id)
// res.status(200).json({success: true, data: user})
// })
// app.post('api/quiz', adminOnly, async(req, res) => {
// })