Skip to content

Commit

Permalink
Merge pull request #1798 from ShivanshPlays/main
Browse files Browse the repository at this point in the history
Added backend for forum
  • Loading branch information
hiteashgupta1 authored Nov 9, 2024
2 parents 872d325 + cb0d828 commit f799e19
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
3 changes: 3 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, () => {
Expand Down
80 changes: 80 additions & 0 deletions backend/controllers/faqController.js
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 });
}
};
12 changes: 12 additions & 0 deletions backend/models/answers.js
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;
21 changes: 21 additions & 0 deletions backend/models/question.js
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);

10 changes: 10 additions & 0 deletions backend/routes/faqRoutes.js
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;

1 comment on commit f799e19

@vercel
Copy link

@vercel vercel bot commented on f799e19 Nov 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.