quiz.controller.js 4.33 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
      p.quiz = quiz._id
Yoon, Daeki's avatar
Yoon, Daeki committed
20
21
      await p.save()
      quiz.problems.push(p._id)
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
22
23
    }

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

Yoon, Daeki's avatar
Yoon, Daeki committed
27
28
29
30
31
32
33
34
35
36
37
38
    // 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
    })
  }
}

Yoon, Daeki's avatar
Yoon, Daeki committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const createProblem = async (req, res) => {
  try {
    const problem = new Problem(req.body)
    problem.author = req.profile._id

    await problem.save()

    // push the problem to the quiz
    if (problem.quiz) {
      const quiz = await Quiz.findById(problem.quiz)
      quiz.problems.push(problem)
      await quiz.save()
    }
    res.json(problem)
  } catch (error) {
    return res.status(400).json({
      error: dbErrorHandler.getErrorMessage(error)
    })
  }
}

Yoon, Daeki's avatar
Yoon, Daeki committed
60
61
62
63
64
65
66
67
68
69
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
70
71
72
73
74
75
76
77
78
79
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
80
81
82
83
84
const read = async (req, res) => {
  let quiz = req.quiz
  res.json(quiz)
}

Yoon, Daeki's avatar
Yoon, Daeki committed
85
86
87
88
89
const readProblem = async (req, res) => {
  let problem = req.problem
  res.json(problem)
}

Yoon, Daeki's avatar
Yoon, Daeki committed
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
const updateProblem = async (req, res) => {
  try {
    const problem = req.problem
    console.log('req.body in updateProblem in quiz.controller', req.body);
    problem.question = req.body.question
    problem.answers = req.body.answers
    problem.updated = new Date()
    console.log('updated problem in updateProblem in quiz.controller', problem);
    await problem.save()
    res.json(problem)
  } catch (error) {
    return res.status(400).json({
      error: dbErrorHandler.getErrorMessage(error)
    })
  }
}

const removeProblem = async (req, res) => {
  try {
    const problem = req.problem
    await Quiz.findByIdAndUpdate(problem.quiz, {
      $pull: {problems: problem._id}
    })
    const deletedProblem = await problem.remove()
    res.json(deletedProblem)      
  } catch (error) {
    return res.status(400).json({
      error: dbErrorHandler.getErrorMessage(error)
    })
  }
}

Yoon, Daeki's avatar
Yoon, Daeki committed
122
123
124
125
126
127
128
129
130
131
132
133
134
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
135
136
137
138
139
140
141
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
142
      return res.status(400).json({
Yoon, Daeki's avatar
Yoon, Daeki committed
143
        error: 'Quiz not found'
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
144
145
      })
    }
Yoon, Daeki's avatar
Yoon, Daeki committed
146
147
148
149
150
151
152
    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
153
154
}

Yoon, Daeki's avatar
Yoon, Daeki committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
174
175
export default {
  create,
Yoon, Daeki's avatar
Yoon, Daeki committed
176
  createProblem,
Yoon, Daeki's avatar
Yoon, Daeki committed
177
  read,
Yoon, Daeki's avatar
Yoon, Daeki committed
178
  readProblem,
Yoon, Daeki's avatar
Yoon, Daeki committed
179
180
  updateProblem,
  removeProblem,
Yoon, Daeki's avatar
Yoon, Daeki committed
181
  isAuthor,
Yoon, Daeki's avatar
Yoon, Daeki committed
182
183
  isProblemAuthor,
  listByUserId,
Yoon, Daeki's avatar
Yoon, Daeki committed
184
  quizById,
Yoon, Daeki's avatar
Yoon, Daeki committed
185
  problemById,
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
186
}