diff --git a/server/models/Post.js b/server/models/Post.js index 8605a88..3eecc24 100644 --- a/server/models/Post.js +++ b/server/models/Post.js @@ -17,7 +17,15 @@ const PostSchema = new Schema({ updatedAt: { type: Date, default: Date.now - } + }, + likes: { // Counter for the number of likes + type: Number, + default: 0 + }, + likedBy: [{ // Array of user IDs who liked the post, to prevent duplicate likes + type: mongoose.Schema.Types.ObjectId, + ref: 'User' + }] }); -module.exports = mongoose.model('Post', PostSchema); \ No newline at end of file +module.exports = mongoose.model('Post', PostSchema); diff --git a/server/routes/admin.js b/server/routes/admin.js index eeccf33..1fe576e 100644 --- a/server/routes/admin.js +++ b/server/routes/admin.js @@ -191,6 +191,20 @@ router.delete('/delete-post/:id', authMiddleware, async (req, res) => { } }); +// POST: Add a like to a post +app.post('/posts/:id/like', async (req, res) => { + const postId = req.params.id; + const userId = req.user.id; // Assume user is authenticated + + const post = await Post.findById(postId); + if (!post.likedBy.includes(userId)) { + post.likes += 1; + post.likedBy.push(userId); + await post.save(); + } + res.status(200).json(post); +}); + /** * POST /register * Admin Registration Route diff --git a/server/routes/main.js b/server/routes/main.js index bc0cea3..cb7c8a2 100644 --- a/server/routes/main.js +++ b/server/routes/main.js @@ -184,6 +184,18 @@ router.post('/send-message', async (req, res) => { } }); +const handleLike = async () => { + if (!liked) { + await axios.post(`/posts/${postId}/like`); + } else { + await axios.delete(`/posts/${postId}/like`); + } + setLiked(!liked); // Update UI state + fetchPostData(); // Re-fetch or update post data +}; +{post.likes} Reactions + + // function insertPostData() { // Post.insertMany([ // { diff --git a/views/admin/delete-post.ejs b/views/admin/delete-post.ejs new file mode 100644 index 0000000..7ac964c --- /dev/null +++ b/views/admin/delete-post.ejs @@ -0,0 +1,59 @@ + + +
+ + +This action cannot be undone.
+ +