-
Notifications
You must be signed in to change notification settings - Fork 117
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 #791 from haseebzaki-07/new_branch_5
Add discussion APIs
- Loading branch information
Showing
5 changed files
with
139 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
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" }); | ||
} | ||
}; |
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,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); |
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,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); |
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,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; |