quiz.controller.js 3.85 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
39
40
41
42
43
44
45
46
47
48
    // 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
49
50
51
52
53
54
55
56
57
58
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
59
60
61
62
63
const read = async (req, res) => {
  let quiz = req.quiz
  res.json(quiz)
}

Yoon, Daeki's avatar
Yoon, Daeki committed
64
65
66
67
68
const readProblem = async (req, res) => {
  let problem = req.problem
  res.json(problem)
}

Yoon, Daeki's avatar
Yoon, Daeki committed
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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
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
114
115
116
117
118
119
120
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
121
      return res.status(400).json({
Yoon, Daeki's avatar
Yoon, Daeki committed
122
        error: 'Quiz not found'
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
123
124
      })
    }
Yoon, Daeki's avatar
Yoon, Daeki committed
125
126
127
128
129
130
131
    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
132
133
}

Yoon, Daeki's avatar
Yoon, Daeki committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
153
154
export default {
  create,
Yoon, Daeki's avatar
Yoon, Daeki committed
155
  read,
Yoon, Daeki's avatar
Yoon, Daeki committed
156
  readProblem,
Yoon, Daeki's avatar
Yoon, Daeki committed
157
158
  updateProblem,
  removeProblem,
Yoon, Daeki's avatar
Yoon, Daeki committed
159
  isAuthor,
Yoon, Daeki's avatar
Yoon, Daeki committed
160
161
  isProblemAuthor,
  listByUserId,
Yoon, Daeki's avatar
Yoon, Daeki committed
162
  quizById,
Yoon, Daeki's avatar
Yoon, Daeki committed
163
  problemById,
Yoon, Daeki's avatar
quiz    
Yoon, Daeki committed
164
}