diff --git a/backend/controllers/discussionController.js b/backend/controllers/discussionController.js new file mode 100644 index 00000000..9cebfe5e --- /dev/null +++ b/backend/controllers/discussionController.js @@ -0,0 +1,90 @@ +// controllers/discussionController.js + +const Post = require("../model/post"); +const Comment = require("../model/comment"); +const User = require("../model/user"); + +// Controller to create a new post +exports.createPost = async (req, res) => { + try { + const { content, images, author } = req.body; + // const author = req.user._id; // Assuming user is authenticated + + const post = new Post({ + content, + images, + author, + }); + + await post.save(); + res.status(201).json({ message: "Post created successfully", post }); + } catch (error) { + res.status(500).json({ error: "Failed to create post" }); + } +}; + +// Controller to add a comment to a post +exports.addComment = async (req, res) => { + try { + const { content, author } = req.body; + // const author = req.user._id; + const { postId } = req.params; + + const comment = new Comment({ + content, + author, + post: postId, + }); + + await comment.save(); + + // Add comment to the post's comments array + await Post.findByIdAndUpdate(postId, { $push: { comments: comment._id } }); + + res.status(201).json({ message: "Comment added successfully", comment }); + } catch (error) { + res.status(500).json({ error: "Failed to add comment" }); + } +}; + +// Controller to retrieve all posts with their comments +exports.getPostsWithComments = async (req, res) => { + try { + const posts = await Post.find() + .populate("author", "firstName lastName") + .populate({ + path: "comments", + populate: { path: "author", select: "firstName lastName" }, + }) + .exec(); + + res.status(200).json(posts); + } catch (error) { + res.status(500).json({ error: "Failed to retrieve posts" }); + } +}; + +exports.getSinglePostWithComments = async (req, res) => { + try { + const { postId } = req.params; + + const post = await Post.findById(postId) + .populate("author", "firstName lastName") + .populate({ + path: "comments", + populate: { + path: "author", + select: "firstName lastName", + }, + }); + + if (!post) { + return res.status(404).json({ error: "Post not found" }); + } + + res.status(200).json(post); + } catch (error) { + console.error("Error fetching post with comments:", error); + res.status(500).json({ error: "Server error" }); + } +}; diff --git a/backend/index.js b/backend/index.js index 66361510..05e8d1c6 100644 --- a/backend/index.js +++ b/backend/index.js @@ -8,7 +8,11 @@ const contactRoutes = require('./routes/Contactroute'); const shopRoutes = require('./routes/shop') const googleauth = require('./routes/googleauth') const agriProductRoutes = require('./routes/agriProductRoutes'); + +const discussionRoutes = require('./routes/discussionRoutes'); + const rentProductRoutes = require('./routes/rent/rentProductRoutes'); + const { sendEmail } = require('./services/emailService'); const session = require('express-session'); const passport = require('passport'); @@ -50,6 +54,7 @@ app.use('/api', contactRoutes); app.use('/api', shopRoutes); app.use('/api', rentProductRoutes); app.use('/api', userRoutes); +app.use('/api/discussions', discussionRoutes); app.use('/api/products', agriProductRoutes); diff --git a/backend/model/comment.js b/backend/model/comment.js new file mode 100644 index 00000000..ff10e5b4 --- /dev/null +++ b/backend/model/comment.js @@ -0,0 +1,12 @@ +// models/Comment.js + +const mongoose = require('mongoose'); + +const commentSchema = new mongoose.Schema({ + content: { type: String, required: true }, + author: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }, // Reference to User (farmer) + post: { type: mongoose.Schema.Types.ObjectId, ref: 'Post', required: true }, // Reference to Post + createdAt: { type: Date, default: Date.now }, +}); + +module.exports = mongoose.model('Comment', commentSchema); diff --git a/backend/model/post.js b/backend/model/post.js new file mode 100644 index 00000000..dc51607b --- /dev/null +++ b/backend/model/post.js @@ -0,0 +1,13 @@ +// models/Post.js + +const mongoose = require('mongoose'); + +const postSchema = new mongoose.Schema({ + content: { type: String, required: true }, + images: [{ type: String }], // Array to store URLs for uploaded images + author: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }, // Reference to User (farmer) + comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }], // Array of references to Comment model + createdAt: { type: Date, default: Date.now }, +}); + +module.exports = mongoose.model('Post', postSchema); diff --git a/backend/routes/discussionRoutes.js b/backend/routes/discussionRoutes.js new file mode 100644 index 00000000..5a50e2d9 --- /dev/null +++ b/backend/routes/discussionRoutes.js @@ -0,0 +1,19 @@ +// routes/discussionRoutes.js + +const express = require('express'); +const { createPost, addComment, getPostsWithComments,getSinglePostWithComments } = require('../controllers/discussionController'); +const router = express.Router(); + +// Route to create a new post +router.post('/posts', createPost); + +// Route to add a comment to a post +router.post('/posts/:postId/comments', addComment); + +// Route to retrieve all posts with their comments +router.get('/posts', getPostsWithComments); + +router.get('/post/:postId', getSinglePostWithComments); + + +module.exports = router;