quiz.controller.js 4.55 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
Yoon, Daeki committed
6
import extend from 'lodash/extend.js'
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
7
8

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

    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
19
      p.author = req.profile
Yoon, Daeki's avatar
Yoon, Daeki committed
20
      p.quiz = quiz._id
Yoon, Daeki's avatar
Yoon, Daeki committed
21
22
      await p.save()
      quiz.problems.push(p._id)
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
23
24
    }

Yoon, Daeki's avatar
Yoon, Daeki committed
25
    quiz.title = title
Yoon, Daeki's avatar
Yoon, Daeki committed
26
27
28
    quiz.startAt = startAt
    quiz.endAt = endAt
    quiz.course = course
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
29
30
    quiz.author = req.profile

Yoon, Daeki's avatar
Yoon, Daeki committed
31
32
33
34
35
36
37
38
39
40
41
42
    // 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
64
65
66
67
68
69
70
71
72
73
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
74
75
76
77
78
79
80
81
82
83
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
84
85
86
87
88
const read = async (req, res) => {
  let quiz = req.quiz
  res.json(quiz)
}

Yoon, Daeki's avatar
Yoon, Daeki committed
89
90
91
92
93
const readProblem = async (req, res) => {
  let problem = req.problem
  res.json(problem)
}

Yoon, Daeki's avatar
Yoon, Daeki committed
94
95
const updateProblem = async (req, res) => {
  try {
Yoon, Daeki's avatar
Yoon, Daeki committed
96
    let problem = req.problem
Yoon, Daeki's avatar
Yoon, Daeki committed
97
    console.log('req.body in updateProblem in quiz.controller', req.body);
Yoon, Daeki's avatar
Yoon, Daeki committed
98
99
100
    problem = extend(problem, req.body)
    // problem.question = req.body.question
    // problem.answers = req.body.answers
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
120
121
122
123
124
125
126
    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
127
128
129
130
131
132
133
134
135
136
137
138
139
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
140
141
142
143
144
const quizById = async (req, res, next, id) => {
  try {
    const quiz = await Quiz.findById(id)
      .populate('author', '_id name')
      .populate('problems')
Yoon, Daeki's avatar
Yoon, Daeki committed
145
      .populate('course', '_id name')
Yoon, Daeki's avatar
Yoon, Daeki committed
146
147
      .exec()
    if (!quiz) {
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
148
      return res.status(400).json({
Yoon, Daeki's avatar
Yoon, Daeki committed
149
        error: 'Quiz not found'
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
150
151
      })
    }
Yoon, Daeki's avatar
Yoon, Daeki committed
152
153
154
155
156
157
158
    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
159
160
}

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