-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added backend api for both answered and unanswered question
- Loading branch information
1 parent
c10ec6a
commit 35c231b
Showing
6 changed files
with
208 additions
and
191 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Question from '../models/discussion.js'; | ||
|
||
export const getQuestions = async (req, res) => { | ||
try { | ||
const questions = await Question.find(); | ||
res.json(questions); | ||
} catch (error) { | ||
res.status(500).json({ error: 'Failed to retrieve questions' }); | ||
} | ||
}; | ||
|
||
// Save a new question to MongoDB | ||
export const saveQuestion = async (req, res) => { | ||
try { | ||
const { content } = req.body; | ||
|
||
const newQuestion = new Question({ | ||
content, | ||
}); | ||
|
||
await newQuestion.save(); | ||
|
||
res.status(201).json(newQuestion); | ||
} catch (error) { | ||
res.status(500).json({ error: 'Failed to save question' }); | ||
} | ||
}; | ||
|
||
export const addAnswerToQuestion = async (req, res) => { | ||
try { | ||
const questionId = req.params.id; | ||
const { answer } = req.body; | ||
|
||
|
||
const updatedQuestion = await Question.findByIdAndUpdate( | ||
questionId, | ||
{ answered: true, answer }, | ||
{ new: true } | ||
); | ||
|
||
if (updatedQuestion) { | ||
res.json(updatedQuestion); | ||
} else { | ||
res.status(404).json({ error: 'Question not found' }); | ||
} | ||
} catch (error) { | ||
res.status(500).json({ error: 'Failed to update question' }); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import mongoose from 'mongoose'; | ||
|
||
// Define the schema for a question | ||
const discussionSchema = new mongoose.Schema({ | ||
content: { | ||
type: String, | ||
required: true, | ||
}, | ||
answered: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
answer: { | ||
type: String, | ||
default: '', | ||
}, | ||
}, { timestamps: true }); | ||
|
||
const Question = mongoose.model('Question', discussionSchema); | ||
|
||
export default Question; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import express from 'express'; | ||
const router = express.Router(); | ||
|
||
import { getQuestions, saveQuestion, addAnswerToQuestion } from '../controllers/discussionController.js'; | ||
|
||
router.get('/getQuestion', getQuestions); | ||
|
||
router.post('/postQuestion', saveQuestion); | ||
|
||
router.put('/:id/answer', addAnswerToQuestion); | ||
|
||
export default router; |
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
Oops, something went wrong.