-
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.
Merge pull request #1798 from ShivanshPlays/main
Added backend for forum
- Loading branch information
Showing
5 changed files
with
126 additions
and
0 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,80 @@ | ||
import QuestionModel from "../models/question.js"; | ||
import Answer from "../models/answers.js"; | ||
export const createQuestion = async (req, res) => { | ||
try { | ||
const { content } = req.body; | ||
|
||
// Check if the question already exists | ||
const existingQuestion = await QuestionModel.findOne({ content }); | ||
if (existingQuestion) { | ||
return res.status(400).json({ message: "Question already exists" }); | ||
} | ||
|
||
// Create the new question | ||
const newQuestion = new QuestionModel({ content }); | ||
await newQuestion.save(); | ||
|
||
res.status(201).json(newQuestion); | ||
} catch (error) { | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
// Route to fetch questions based on 'answered' or 'unanswered' query | ||
export const getQuestions = async (req, res) => { | ||
try { | ||
const question = req.query.question; | ||
|
||
let query; | ||
if (question === "answered") { | ||
// Fetch questions with at least one answer, and include the answers | ||
query = QuestionModel.find({ answered: true }).populate("answers"); | ||
|
||
} else if (question === "unanswered") { | ||
// Fetch questions with no answers | ||
query = QuestionModel.find({ answered: false }).populate("answers"); | ||
} else { | ||
// If no specific filter is provided, fetch all questions | ||
query = QuestionModel.find(); | ||
} | ||
|
||
const questions = await query.exec(); | ||
if(!question){ | ||
res.status(200).json({message:"No response"}) | ||
} | ||
res.status(200).json(questions); | ||
} catch (error) { | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
// Example route to create a new question with an optional answer | ||
|
||
export const answerQuestion = async (req, res) => { | ||
try { | ||
const { questionId } = req.body; // The ID of the question to answer | ||
const { content } = req.body; | ||
|
||
// Check if the question exists | ||
const existingQuestion = await QuestionModel.findById(questionId); | ||
if (!existingQuestion) { | ||
return res.status(404).json({ message: "Question not found" }); | ||
} | ||
|
||
// Create a new answer linked to the question | ||
const newAnswer = new Answer({ | ||
content, | ||
question: questionId, | ||
}); | ||
await newAnswer.save(); | ||
|
||
// Add the answer ID to the question's answers array | ||
existingQuestion.answers.push(newAnswer._id); | ||
existingQuestion.answered=true; | ||
await existingQuestion.save(); | ||
|
||
res.status(201).json({ message: "Answer added successfully", answer: newAnswer }); | ||
} catch (error) { | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; |
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 mongoose from "mongoose"; | ||
import QuestionModel from './question.js'; | ||
|
||
const answerSchema = new mongoose.Schema({ | ||
content:{ | ||
type:String | ||
}, | ||
|
||
}); | ||
|
||
const answer = mongoose.model("Answer", answerSchema); | ||
export default answer; |
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, { Mongoose } from "mongoose"; | ||
import answer from "./answers.js" | ||
// import answer from "./answers.js"; | ||
const questionSchema = new mongoose.Schema({ | ||
content:{ | ||
type:String | ||
}, | ||
answers:[ | ||
{ | ||
type:mongoose.Schema.Types.ObjectId, | ||
ref:answer | ||
} | ||
], | ||
answered:{ | ||
type:Boolean, | ||
default: false | ||
}, | ||
|
||
}); | ||
export default mongoose.models.Question || mongoose.model('Question', questionSchema); | ||
|
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,10 @@ | ||
import express from "express"; | ||
const router = express.Router(); | ||
// import { getContact, saveContact } from "../controllers/contactController.js"; | ||
import {createQuestion, getQuestions,answerQuestion} from "../controllers/faqController.js" | ||
|
||
router.post("/createquestion", createQuestion); | ||
router.get("/getquestions",getQuestions); | ||
router.post("/answerquestion",answerQuestion); | ||
|
||
export default router; |
f799e19
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
word-wise – ./
word-wise-pi.vercel.app
word-wise-anshikas-projects-45924011.vercel.app
word-wise-git-main-anshikas-projects-45924011.vercel.app