Created
August 8, 2020 20:58
-
-
Save raymag/a0f451694cecaaad1ac465c2e3a778d1 to your computer and use it in GitHub Desktop.
API Quiz create and update questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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