Skip to content

Commit

Permalink
Merge pull request #791 from haseebzaki-07/new_branch_5
Browse files Browse the repository at this point in the history
Add discussion APIs
  • Loading branch information
manikumarreddyu authored Nov 5, 2024
2 parents 4af6c78 + 0ba3c61 commit a6f33eb
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
90 changes: 90 additions & 0 deletions backend/controllers/discussionController.js
Original file line number Diff line number Diff line change
@@ -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" });
}
};
5 changes: 5 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);


Expand Down
12 changes: 12 additions & 0 deletions backend/model/comment.js
Original file line number Diff line number Diff line change
@@ -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);
13 changes: 13 additions & 0 deletions backend/model/post.js
Original file line number Diff line number Diff line change
@@ -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);
19 changes: 19 additions & 0 deletions backend/routes/discussionRoutes.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit a6f33eb

Please sign in to comment.