From 076808772c5325c103edff588da2038fd1b9bef2 Mon Sep 17 00:00:00 2001 From: Malakyasser8 Date: Sat, 4 May 2024 22:02:45 +0300 Subject: [PATCH] Fixed upvote users in post and comment --- src/controller/comments.js | 2 ++ src/controller/posts.js | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/controller/comments.js b/src/controller/comments.js index f2393f9..2f3d76d 100644 --- a/src/controller/comments.js +++ b/src/controller/comments.js @@ -121,6 +121,7 @@ export async function newComment(request) { spoiler_flag: post.spoiler_flag, }); + comment.upvote_users.push(user._id); await comment.save(); console.log(post); @@ -216,6 +217,7 @@ export async function replyToComment(request) { comment.replies_comments_ids.push(reply._id); await comment.save(); + reply.upvote_users.push(user._id); await reply.save(); post.comments_count++; await post.save(); diff --git a/src/controller/posts.js b/src/controller/posts.js index c22d2c2..57229d4 100644 --- a/src/controller/posts.js +++ b/src/controller/posts.js @@ -129,15 +129,17 @@ export async function createPost(request) { post.user_id = user._id; post.username = user.username; post.created_at = Date.now(); - post.upvotes_count++; - user.upvotes_posts_ids.push(post._id); - + if (post.upvotes_count + post.downvotes_count != 0) { post.user_details.upvote_rate = (post.upvotes_count / (post.upvotes_count + post.downvotes_count)) * 100; } - await post.save(); + const savedPost = await post.save(); + savedPost.upvotes_count++; + user.upvotes_posts_ids.push(savedPost._id); + await savedPost.save(); + await user.save(); console.log("HIIIIIIIIII", post._id); return { @@ -269,7 +271,12 @@ export async function sharePost(request) { shared_post.community_id = community._id; } - await shared_post.save(); + const savedSharedPost = await shared_post.save(); + savedSharedPost.upvotes_count++; + user.upvotes_posts_ids.push(savedSharedPost._id); + await user.save(); + await savedSharedPost.save(); + const postObj = await Post.findById(post._id); postObj.shares_count++; postObj.user_details.total_shares++;