From 47ba8ca3c11c20e1c472e1bf4842fcf3c671e080 Mon Sep 17 00:00:00 2001 From: George <gm00442@surrey.ac.uk> Date: Tue, 13 Apr 2021 15:38:42 +0100 Subject: [PATCH] Created the addComment and removeComment functions for adding and deleting commentID's from a post --- posts-service/controllers/posts.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/posts-service/controllers/posts.js b/posts-service/controllers/posts.js index 8e0bb30..66e90ac 100644 --- a/posts-service/controllers/posts.js +++ b/posts-service/controllers/posts.js @@ -56,9 +56,21 @@ export const likePost = async (req, res) => { } export const addComment = async (req, res) => { - + const { id, commentId } = req.params; + const post = await Post.findById(id); + post.comments.push(commentId); + await post.save(); + + return res.status(200).json(post); } export const removeComment = async (req, res) => { - + const { id, commentId } = req.params; + + const post = await Post.findById(id); + const commentIndex = post.comments.findIndex((c) => c === commentId); + post.comments.splice(commentIndex, 1); + await post.save(); + + return res.status(200).json(post); } \ No newline at end of file -- GitLab