quiz.controller.js 2.92 KB
Newer Older
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
1
2
import formidable from 'formidable'
import fs from 'fs'
Yoon, Daeki's avatar
Yoon, Daeki committed
3
import dbErrorHandler from '../helpers/dbErrorHandler.js'
Yoon, Daeki's avatar
Yoon, Daeki committed
4
5
import Problem from './problem.model.js'
import Quiz from './quiz.model.js'
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
6
7

const create = async (req, res) => {
Yoon, Daeki's avatar
Yoon, Daeki committed
8
  try {
Yoon, Daeki's avatar
Yoon, Daeki committed
9
    const { title, problems } = req.body
Yoon, Daeki's avatar
Yoon, Daeki committed
10
11
12
13
14
15
16
17

    const quiz = new Quiz()
    // console.log('quiz in quiz.controller:', quiz);

    for await (let problem of problems) {
      // console.log('problem in quiz.controller:', problem);
      const p = new Problem(problem)
      // console.log('problem in quiz.controller:', p);
Yoon, Daeki's avatar
Yoon, Daeki committed
18
      p.author = req.profile
Yoon, Daeki's avatar
Yoon, Daeki committed
19
20
      await p.save()
      quiz.problems.push(p._id)
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
21
22
    }

Yoon, Daeki's avatar
Yoon, Daeki committed
23
    quiz.title = title
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
24
25
    quiz.author = req.profile

Yoon, Daeki's avatar
Yoon, Daeki committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    // console.log('quiz in quiz.controller:', quiz);
    await quiz.save()
    quiz.author.hashedPassword = undefined
    quiz.author.salt = undefined
    res.json(quiz)
  } catch (error) {
    return res.status(400).json({
      error: 'Quiz save DB error' + error
    })
  }
}

const isAuthor = (req, res, next) => {
  const isAuthor = req.auth && req.quiz && req.auth._id == req.quiz.author._id
  if (!isAuthor) {
    return res.status(403).json({
      error: 'User is not an author'
    })
  }
  next()
}

Yoon, Daeki's avatar
Yoon, Daeki committed
48
49
50
51
52
53
54
55
56
57
const isProblemAuthor = (req, res, next) => {
  const isProblemAuthor = req.auth && req.problem && req.auth._id == req.problem.author._id
  if (!isProblemAuthor) {
    return res.status(403).json({
      error: 'User is not an author of the problem'
    })
  }
  next()
}

Yoon, Daeki's avatar
Yoon, Daeki committed
58
59
60
61
62
const read = async (req, res) => {
  let quiz = req.quiz
  res.json(quiz)
}

Yoon, Daeki's avatar
Yoon, Daeki committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const readProblem = async (req, res) => {
  let problem = req.problem
  res.json(problem)
}

const listByUserId = async (req, res) => {
  try {
    const authorId = req.profile._id
    const quizzes = await Quiz.find({author: authorId}).exec()
    // console.log('quizzes in listByUserId:', quizzes);
    res.json(quizzes)      
  } catch (error) {
    return res.status(400).json({
      error: dbErrorHandler.getErrorMessage(error)
    })
  }
}

Yoon, Daeki's avatar
Yoon, Daeki committed
81
82
83
84
85
86
87
const quizById = async (req, res, next, id) => {
  try {
    const quiz = await Quiz.findById(id)
      .populate('author', '_id name')
      .populate('problems')
      .exec()
    if (!quiz) {
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
88
      return res.status(400).json({
Yoon, Daeki's avatar
Yoon, Daeki committed
89
        error: 'Quiz not found'
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
90
91
      })
    }
Yoon, Daeki's avatar
Yoon, Daeki committed
92
93
94
95
96
97
98
    req.quiz = quiz
    next()
  } catch (error) {
    return res.status(400).json({
      error: 'Quiz by id query db error: ' + error
    })
  }
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
99
100
}

Yoon, Daeki's avatar
Yoon, Daeki committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const problemById = async (req, res, next, id) => {
  try {
    const problem = await Problem.findById(id)
      .populate('author', '_id name')
      .exec()
    if (!problem) {
      return res.status(400).json({
        error: 'Problem not found'
      })
    }
    req.problem = problem
    next()
  } catch (error) {
    return res.status(400).json({
      error: dbErrorHandler.getErrorMessage(error)
    })
  }
}

Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
120
121
export default {
  create,
Yoon, Daeki's avatar
Yoon, Daeki committed
122
  read,
Yoon, Daeki's avatar
Yoon, Daeki committed
123
  readProblem,
Yoon, Daeki's avatar
Yoon, Daeki committed
124
  isAuthor,
Yoon, Daeki's avatar
Yoon, Daeki committed
125
126
  isProblemAuthor,
  listByUserId,
Yoon, Daeki's avatar
Yoon, Daeki committed
127
  quizById,
Yoon, Daeki's avatar
Yoon, Daeki committed
128
  problemById,
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
129
}