|
| 1 | +const questionsRouter = require('express').Router() |
| 2 | +const QuestionModel = require('../models/Questions') |
| 3 | + |
| 4 | +// Read all questions |
| 5 | +questionsRouter.get("/", async (req, res) => { |
| 6 | + try { |
| 7 | + const questions = await QuestionModel.find(); |
| 8 | + res.status(200).json(questions); |
| 9 | + } catch (error) { |
| 10 | + res.status(500).json({error: error.message}); |
| 11 | + } |
| 12 | +}) |
| 13 | + |
| 14 | +// Create a new question |
| 15 | +questionsRouter.post("/", async (req, res) => { |
| 16 | + console.log('Incoming Data:', req.body) |
| 17 | + try { |
| 18 | + const maxQuestion = await QuestionModel.findOne().sort({ id: -1 }).exec() |
| 19 | + const maxId = maxQuestion ? maxQuestion.id : 0 |
| 20 | + |
| 21 | + const newQuestion = new QuestionModel({ |
| 22 | + category: req.body.category, |
| 23 | + complexity: req.body.complexity, |
| 24 | + description: req.body.description, |
| 25 | + id: maxId + 1, |
| 26 | + title: req.body.title |
| 27 | + }) |
| 28 | + |
| 29 | + // Save the new question |
| 30 | + await newQuestion.save() |
| 31 | + res.status(201).json(newQuestion) |
| 32 | + } catch (error) { |
| 33 | + if (error.code === 11000) { |
| 34 | + // Check for duplicate key in MongoDB |
| 35 | + return res.status(400).json({ error: "This title is already in use." }) |
| 36 | + } else if (error.name === "ValidationError" || error.name === "CastError") { |
| 37 | + const errors = Object.values(error.errors).map(err => err.message) |
| 38 | + return res.status(400).json({ error: errors }) |
| 39 | + } |
| 40 | + res.status(500).json({ error: error.message }) |
| 41 | + } |
| 42 | +}) |
| 43 | + |
| 44 | +// Read a specific question |
| 45 | +questionsRouter.get("/:id", async (req, res) => { |
| 46 | + |
| 47 | + const id = req.params.id; |
| 48 | + |
| 49 | + console.log("Fetching Question with ID:", id); |
| 50 | + |
| 51 | + try { |
| 52 | + const question = await QuestionModel.findById(id); |
| 53 | + |
| 54 | + if (!question) { |
| 55 | + return res.status(404).json({error: 'Question not found'}); |
| 56 | + } |
| 57 | + console.log("Data found: ", question); |
| 58 | + res.status(200).json(question); |
| 59 | + } catch (error) { |
| 60 | + res.status(500).json({error: error.message}); |
| 61 | + } |
| 62 | +}) |
| 63 | + |
| 64 | +questionsRouter.put("/:id", async (req, res) => { |
| 65 | + try { |
| 66 | + const question = await QuestionModel.findByIdAndUpdate(req.params.id, req.body, { new: true, runValidators: true }); |
| 67 | + if (!question) return res.status(404).json({ error: "Question not found" }); |
| 68 | + res.status(200).json(question); |
| 69 | + } catch (error) { |
| 70 | + |
| 71 | + if (error.code === 11000) { |
| 72 | + // Check for duplicate key in MongoDB |
| 73 | + return res.status(400).json({ error: "This title is already in use. " }); |
| 74 | + } else if (error.name === "ValidationError" || error.name === "CastError" ) { |
| 75 | + const errors = Object.values(error.errors).map(err => err.message); |
| 76 | + return res.status(400).json({ error: errors }); |
| 77 | + } |
| 78 | + res.status(500).json({ error: error.message }); |
| 79 | + } |
| 80 | +}); |
| 81 | + |
| 82 | + |
| 83 | +// Delete a question |
| 84 | +questionsRouter.delete("/:id", async (req, res) => { |
| 85 | + const id = req.params.id; |
| 86 | + |
| 87 | + try { |
| 88 | + const question = await QuestionModel.findByIdAndDelete({_id: id}); |
| 89 | + if (!question) { |
| 90 | + return res.status(404).json({error: 'Question not found '}); |
| 91 | + } |
| 92 | + res.status(200).json({message: 'Question deleted'}); |
| 93 | + } catch (error) { |
| 94 | + res.status(500).json({ error: error.message }); |
| 95 | + } |
| 96 | +}) |
| 97 | + |
| 98 | +module.exports = questionsRouter |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
0 commit comments