Commit 55f78aee authored by Yoon, Daeki's avatar Yoon, Daeki 😅
Browse files

quiz

parent c1d16d72
...@@ -48,8 +48,11 @@ const requireSignin = expressJwt({ ...@@ -48,8 +48,11 @@ const requireSignin = expressJwt({
}) })
const hasAuthorization = (req, res, next) => { const hasAuthorization = (req, res, next) => {
const authorized = req.profile && req.auth && req.profile._id === req.auth._id // console.log('profile:', req.profile, 'auth:', req.auth);
// console.log('id is equal?:', req.profile._id === req.auth._id);
const authorized = req.profile && req.auth && req.profile._id == req.auth._id
if (!authorized) { if (!authorized) {
// console.log('authorized?', authorized);
return res.status(403).json({ return res.status(403).json({
error: 'User is not authorized' error: 'User is not authorized'
}) })
......
import express from 'express' import express from 'express'
import authCtrl from './auth.controller' import authCtrl from './auth.controller.js'
const router = express.Router() const router = express.Router()
......
import express from 'express' import express from 'express'
import bodyParser from 'body-parser' import bodyParser from 'body-parser'
import userRoutes from './user/user.routes.js' import userRoutes from './user/user.routes.js'
import authRoutes from './auth/auth.routes.js'
import quizRoutes from './quiz/quiz.routes.js'
const app = express() const app = express()
app.use(bodyParser.json()) app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true })) app.use(bodyParser.urlencoded({ extended: true }))
app.use('/', authRoutes)
app.use('/', userRoutes) app.use('/', userRoutes)
app.use('/', quizRoutes)
app.use((err, req, res, next) => { app.use((err, req, res, next) => {
if (err.name === 'UnauthorizedError') { if (err.name === 'UnauthorizedError') {
......
import mongoose from 'mongoose'
const AnswerSchema = new mongoose.Schema({
questionId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Question',
},
type: String, // single/multiple choice?
score: Number, // 맞힌 점수
points: Number, // Question.score와 자동 연결 필요
content: String, // 기록한 답
})
export default mongoose.model('Answer', AnswerSchema)
\ No newline at end of file
import mongoose from 'mongoose'
const QuestionSchema = new mongoose.Schema({
type: String, // 객관식, 주관식 single/multiple choice
created: {
type: Date,
default: Date.now
},
updated: Date,
level: String,
category: [String],
score: Number, //문제당 할당 점수
content: String, //질문
choices: [String], // 선택형 항목
correct: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Answer'
}, // 정답; Answer Model 객체
})
export default mongoose.model('Question', QuestionSchema)
\ No newline at end of file
import formidable from 'formidable'
import fs from 'fs'
import quizModel from './quiz.model.js'
const create = async (req, res) => {
const form = new formidable.IncomingForm()
form.keepExtensions = true
form.parse(req, async (err, fields, files) => {
if (err) {
return res.status(400).json({
error: 'Image could not be uploaded'
})
}
const quiz = new quizModel(fields)
quiz.author = req.profile
if (files.image) {
quiz.image.data = fs.readFileSync(files.image.path)
quiz.image.contentType = files.image.type
}
try {
const result = await quiz.save()
res.json(result)
} catch (error) {
return res.status(400).json({
error: 'Quiz save db error'
})
}
})
}
export default {
create,
}
\ No newline at end of file
import mongoose from 'mongoose'
const QuizSchema = new mongoose.Schema({
author: {
type: mongoose.SchemaTypes.ObjectId,
ref: 'User'
},
title: String,
score: Number,
published: Boolean,
created: {
type: Date,
default: Date.now
},
updated: Date,
publishedAt: Date,
startAt: Date,
endAt: Date,
questions: [], // Question Schemas
image: {
type: Buffer,
contentType: String,
}
})
export default mongoose.model('Quiz', QuizSchema)
\ No newline at end of file
import express from 'express'
import authCtrl from '../auth/auth.controller.js'
import userCtrl from '../user/user.controller.js'
import quizCtrl from './quiz.controller.js'
const router = express.Router()
router.route('/api/quiz/:userId')
.post(authCtrl.requireSignin, authCtrl.hasAuthorization, userCtrl.isInstructor, quizCtrl.create)
router.param('userId', userCtrl.userById)
export default router
\ No newline at end of file
import User from './user.model.js' import User from './user.model.js'
import formidable from 'formidable' import formidable from 'formidable'
import extend from 'lodash/extend' import extend from 'lodash/extend.js'
import fs from 'fs' import fs from 'fs'
const create = async (req, res) => { const create = async (req, res) => {
...@@ -34,7 +34,7 @@ const read = (req, res) => { ...@@ -34,7 +34,7 @@ const read = (req, res) => {
return res.json(req.profile) return res.json(req.profile)
} }
const update = async (req, res) => { const update = (req, res) => {
let form = new formidable.IncomingForm() let form = new formidable.IncomingForm()
form.keepExtensions = true form.keepExtensions = true
form.parse(req, async (err, fields, files) => { form.parse(req, async (err, fields, files) => {
...@@ -78,6 +78,16 @@ const remove = async (req, res) => { ...@@ -78,6 +78,16 @@ const remove = async (req, res) => {
} }
} }
const isInstructor = (req, res, next) => {
const instructor = req.profile && req.profile.instructor
if (!instructor) {
return res.status(403).json({
error: 'User is not an instructor'
})
}
next()
}
const userById = async (req, res, next, id) => { const userById = async (req, res, next, id) => {
try { try {
let user = await User.findById(id) let user = await User.findById(id)
...@@ -102,5 +112,6 @@ export default { ...@@ -102,5 +112,6 @@ export default {
read, read,
update, update,
remove, remove,
isInstructor,
userById, userById,
} }
\ No newline at end of file
...@@ -28,6 +28,10 @@ const UserSchema = new mongoose.Schema({ ...@@ -28,6 +28,10 @@ const UserSchema = new mongoose.Schema({
data: Buffer, data: Buffer,
contentType: String, contentType: String,
}, },
instructor: {
type: Boolean,
default: false,
},
}) })
UserSchema.virtual('password') UserSchema.virtual('password')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment