Skip to content

Instantly share code, notes, and snippets.

@raymag
Created August 8, 2020 20:58
Show Gist options
  • Save raymag/a0f451694cecaaad1ac465c2e3a778d1 to your computer and use it in GitHub Desktop.
Save raymag/a0f451694cecaaad1ac465c2e3a778d1 to your computer and use it in GitHub Desktop.
API Quiz create and update questions
// create one quiz question
router.post('/questions', async (req, res) => {
try {
const { description } = req.body
const { image } = req.body
const { alternatives } = req.body
const question = await Question.create({
description,
alternatives,
image
})
return res.status(201).json(question)
} catch (error) {
return res.status(500).json({"error":error})
}
})
// update one quiz question
router.put('/questions/:id', async (req, res) => {
try {
const _id = req.params.id
const { description, image, alternatives } = req.body
let question = await Question.findOne({_id})
if(!question){
question = await Question.create({
description,
alternatives
})
return res.status(201).json(question)
}else{
question.description = description
question.alternatives = alternatives
question.image = image
await question.save()
return res.status(200).json(question)
}
} catch (error) {
return res.status(500).json({"error":error})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment