Skip to content

Commit

Permalink
Merge pull request #1026 from bounswe/backend/improve-comment-endpoint
Browse files Browse the repository at this point in the history
added user vote choice into comment and replys
  • Loading branch information
AliBasarann authored Dec 17, 2023
2 parents bae0ca4 + 9d4ddf5 commit dfa43c9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.app.gamereview.dto.response.comment;

import com.app.gamereview.enums.VoteChoice;
import com.app.gamereview.model.User;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down Expand Up @@ -32,6 +33,8 @@ public class CommentReplyResponseDto {

private int voteCount; // voteCount = # of upvote + # of downvote

private VoteChoice userVote;

// TODO reports
// TODO annotations
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.app.gamereview.dto.response.comment;

import com.app.gamereview.enums.VoteChoice;
import com.app.gamereview.model.User;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down Expand Up @@ -35,6 +36,8 @@ public class GetPostCommentsResponseDto {

private ArrayList<CommentReplyResponseDto> replies;

private VoteChoice userVote;

// TODO reports
// TODO annotations
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.*;
import java.util.stream.Collectors;

import com.app.gamereview.dto.request.group.CreateGroupRequestDto;
import com.app.gamereview.dto.request.notification.CreateNotificationRequestDto;
import com.app.gamereview.dto.request.home.HomePagePostsFilterRequestDto;
import com.app.gamereview.dto.response.comment.CommentReplyResponseDto;
Expand Down Expand Up @@ -232,7 +231,7 @@ public List<GetPostCommentsResponseDto> getCommentList(String postId, User user)

// First, convert all comments to DTOs and identify top-level comments
for (Comment comment : comments) {
GetPostCommentsResponseDto dto = convertToCommentDto(comment);
GetPostCommentsResponseDto dto = convertToCommentDto(comment, user.getId());
commentMap.put(comment.getId(), dto);

if (comment.getParentComment() == null) {
Expand All @@ -245,34 +244,40 @@ public List<GetPostCommentsResponseDto> getCommentList(String postId, User user)
if (comment.getParentComment() != null) {
GetPostCommentsResponseDto parentDto = commentMap.get(comment.getParentComment());
if (parentDto != null) {
parentDto.getReplies().add(convertToReplyDto(comment));
parentDto.getReplies().add(convertToReplyDto(comment, user.getId()));
}
}
}

return topLevelComments;
}

private GetPostCommentsResponseDto convertToCommentDto(Comment comment) {
private GetPostCommentsResponseDto convertToCommentDto(Comment comment, String loggedInUserId) {
Boolean isEdited = comment.getCreatedAt().isBefore(comment.getLastEditedAt());
String commenterId = comment.getCommenter();
Optional<User> commenter = userRepository.findByIdAndIsDeletedFalse(commenterId);

Optional<Vote> userVote = voteRepository.findByTypeIdAndVotedBy(comment.getId(), loggedInUserId);
VoteChoice userVoteChoice = userVote.map(Vote::getChoice).orElse(null);

User commenterObject = commenter.orElse(null);
return new GetPostCommentsResponseDto(comment.getId(), comment.getCommentContent(), commenterObject,
comment.getPost(), comment.getLastEditedAt(), comment.getCreatedAt(), isEdited, comment.getIsDeleted(), comment.getOverallVote(),
comment.getVoteCount(), new ArrayList<>());
comment.getVoteCount(), new ArrayList<>(), userVoteChoice);
}

private CommentReplyResponseDto convertToReplyDto(Comment comment) {
private CommentReplyResponseDto convertToReplyDto(Comment comment, String loggedInUserId) {
Boolean isEdited = comment.getCreatedAt().isBefore(comment.getLastEditedAt());
String commenterId = comment.getCommenter();
Optional<User> commenter = userRepository.findByIdAndIsDeletedFalse(commenterId);

Optional<Vote> userVote = voteRepository.findByTypeIdAndVotedBy(comment.getId(), loggedInUserId);
VoteChoice userVoteChoice = userVote.map(Vote::getChoice).orElse(null);

User commenterObject = commenter.orElse(null);
return new CommentReplyResponseDto(comment.getId(), comment.getCommentContent(), commenterObject,
comment.getPost(), comment.getLastEditedAt(), comment.getCreatedAt(), isEdited, comment.getIsDeleted(), comment.getOverallVote(),
comment.getVoteCount());
comment.getVoteCount(), userVoteChoice);
}


Expand Down

0 comments on commit dfa43c9

Please sign in to comment.