From cb0d828f044f6fccc4fce50de0d4de88a6902efd Mon Sep 17 00:00:00 2001 From: Shivansh Date: Sat, 9 Nov 2024 01:32:18 +0530 Subject: [PATCH] Added backend for forum --- backend/app.js | 3 ++ backend/controllers/faqController.js | 80 ++++++++++++++++++++++++++++ backend/models/answers.js | 12 +++++ backend/models/question.js | 21 ++++++++ backend/routes/faqRoutes.js | 10 ++++ 5 files changed, 126 insertions(+) create mode 100644 backend/controllers/faqController.js create mode 100644 backend/models/answers.js create mode 100644 backend/models/question.js create mode 100644 backend/routes/faqRoutes.js diff --git a/backend/app.js b/backend/app.js index 4f42f81..1b30621 100644 --- a/backend/app.js +++ b/backend/app.js @@ -10,6 +10,8 @@ import getInTouch from "./routes/getInTouchRoutes.js"; import addBlog from "./routes/addBlogRoutes.js"; import subscribe from "./routes/subscribeRoutes.js"; import discussion from "./routes/discussionRoutes.js"; +import faqRoutes from "./routes/faqRoutes.js"; + import cors from "cors"; import path from "path"; // Import path module import { fileURLToPath } from "url"; // Import fileURLToPath @@ -39,6 +41,7 @@ app.use("/api/getInTouch", getInTouch); app.use("/api/addBlog", addBlog); app.use("/api/newsletter", subscribe); app.use("/api/discussion", discussion); +app.use("/api/faq", faqRoutes); const PORT = process.env.PORT || 5000; app.listen(PORT, () => { diff --git a/backend/controllers/faqController.js b/backend/controllers/faqController.js new file mode 100644 index 0000000..5dbacd8 --- /dev/null +++ b/backend/controllers/faqController.js @@ -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 }); + } + }; \ No newline at end of file diff --git a/backend/models/answers.js b/backend/models/answers.js new file mode 100644 index 0000000..c4806d7 --- /dev/null +++ b/backend/models/answers.js @@ -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; \ No newline at end of file diff --git a/backend/models/question.js b/backend/models/question.js new file mode 100644 index 0000000..59b08b8 --- /dev/null +++ b/backend/models/question.js @@ -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); + diff --git a/backend/routes/faqRoutes.js b/backend/routes/faqRoutes.js new file mode 100644 index 0000000..68e81d3 --- /dev/null +++ b/backend/routes/faqRoutes.js @@ -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; \ No newline at end of file