-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #433 from Sawan-Kushwah/add/backend
✨Implement Backend for discussion forum with CURD operation | Clean and clear schema 🚀 #433
- Loading branch information
Showing
5 changed files
with
166 additions
and
39 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,56 @@ | ||
const Question = require('../Models/discussion.js'); | ||
|
||
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 | ||
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' }); | ||
} | ||
}; | ||
|
||
const addAnswerToQuestion = async (req, res) => { | ||
try { | ||
const questionId = req.params.id; | ||
const { answer } = req.body; | ||
console.log(questionId) | ||
console.log(answer) | ||
|
||
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' }); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
getQuestions, | ||
saveQuestion, | ||
addAnswerToQuestion | ||
}; |
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 @@ | ||
const mongoose = require('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); | ||
|
||
module.exports = 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
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 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
|
||
const { getQuestions, saveQuestion, addAnswerToQuestion } = require('../Controllers/discussionController'); | ||
|
||
router.get('/getQuestion', getQuestions); | ||
|
||
router.post('/postQuestion', saveQuestion); | ||
|
||
router.put('/:id/answer', addAnswerToQuestion); | ||
|
||
module.exports = router; |