forked from Aniket762/playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestionController.js
51 lines (44 loc) · 1.71 KB
/
questionController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const express = require('express')
var router = express.Router()
var ObjectId = require('mongoose').Types.ObjectId
var { question } = require('../models/question')
// get all questions
router.get('/', (req, res, next) => {
question.find((err, response) => {
if (!err) res.send(response)
else console.log('Error while retrieving all questions: ' + JSON.stringify(err, undefined, 2))
})
})
// post questions
router.post('/', (req, res) => {
var newQuestion = new question({
title: req.body.title,
message: req.body.message,
})
newQuestion.save((err, response) => {
if (!err) res.send(response)
else console.log('Error while creating new question: ' + JSON.stringify(err, undefined, 2))
})
})
// update question
router.put('/moderator/:id', (req, res) => {
if (!ObjectId.isValid(req.params.id)) return res.status(400).send('No question with given Id: ' + res.params.id)
var updateQuestion = {
title: req.body.title,
message: req.body.message,
isApproved: req.body.isApproved
}
question.findByIdAndUpdate(req.params.id, { $set: updateQuestion }, (err, response) => {
if (!err) res.send(response)
else console.log('Error while updating a question: ' + JSON.stringify(err, undefined, 2))
})
})
// delete questions
router.delete('/moderator/:id', (req, res) => {
if (!ObjectId.isValid(req.params.id)) return res.status(400).send('No question with given Id: ' + res.params.id)
question.findByIdAndRemove(req.params.id, (err, response) => {
if (!err) res.send(response)
else console.log('Error while deleting a question: ' + JSON.stringify(err, undefined, 2))
})
})
module.exports = router