Skip to content

Commit

Permalink
Fixed upvote users in post and comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Malakyasser8 committed May 4, 2024
1 parent 414166e commit 0768087
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/controller/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
17 changes: 12 additions & 5 deletions src/controller/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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++;
Expand Down

0 comments on commit 0768087

Please sign in to comment.