From 1544b37ed3cf00b654c617419c1414af5a199ff4 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Sun, 27 Jul 2025 10:46:59 +0300 Subject: [PATCH] feat: implement listing posts in a hierarchical structure --- .../wesal/controller/CommentController.java | 2 +- .../wesal/wesal/dto/CommentResponseDTO.java | 12 +++++++ .../wesal/wesal/service/CommentService.java | 32 ++++++++++++++++--- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/online/wesal/wesal/controller/CommentController.java b/backend/src/main/java/online/wesal/wesal/controller/CommentController.java index ce80e91..dda0124 100644 --- a/backend/src/main/java/online/wesal/wesal/controller/CommentController.java +++ b/backend/src/main/java/online/wesal/wesal/controller/CommentController.java @@ -69,7 +69,7 @@ public class CommentController { } @GetMapping("/list") - @Operation(summary = "Get comments for post", description = "Get all comments for a specific post ordered by creation date") + @Operation(summary = "Get comments for post", description = "Get all comments for a specific post in hierarchical structure with nested replies") public ResponseEntity>> getCommentsByPostId( @RequestParam Long postId, Authentication authentication) { diff --git a/backend/src/main/java/online/wesal/wesal/dto/CommentResponseDTO.java b/backend/src/main/java/online/wesal/wesal/dto/CommentResponseDTO.java index 3c10c2c..5c8c92a 100644 --- a/backend/src/main/java/online/wesal/wesal/dto/CommentResponseDTO.java +++ b/backend/src/main/java/online/wesal/wesal/dto/CommentResponseDTO.java @@ -3,6 +3,8 @@ package online.wesal.wesal.dto; import online.wesal.wesal.entity.Comment; import online.wesal.wesal.entity.User; import java.time.LocalDateTime; +import java.util.List; +import java.util.ArrayList; public class CommentResponseDTO { @@ -15,6 +17,7 @@ public class CommentResponseDTO { private String displayReplyComment; private CreatorDTO replyUser; private Integer level; + private List replies; public CommentResponseDTO() {} @@ -28,6 +31,7 @@ public class CommentResponseDTO { this.displayReplyComment = comment.getDisplayReplyComment() != null ? String.valueOf(comment.getDisplayReplyComment()) : null; this.replyUser = replyUser != null ? new CreatorDTO(replyUser) : null; this.level = comment.getLevel(); + this.replies = new ArrayList<>(); } public String getId() { @@ -101,4 +105,12 @@ public class CommentResponseDTO { public void setLevel(Integer level) { this.level = level; } + + public List getReplies() { + return replies; + } + + public void setReplies(List replies) { + this.replies = replies; + } } \ No newline at end of file diff --git a/backend/src/main/java/online/wesal/wesal/service/CommentService.java b/backend/src/main/java/online/wesal/wesal/service/CommentService.java index f0c84d0..da084f6 100644 --- a/backend/src/main/java/online/wesal/wesal/service/CommentService.java +++ b/backend/src/main/java/online/wesal/wesal/service/CommentService.java @@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -105,10 +106,31 @@ public class CommentService { Map users = userRepository.findAllById(creatorIds.stream().distinct().collect(Collectors.toList())).stream() .collect(Collectors.toMap(User::getId, user -> user)); - return comments.stream() - .map(comment -> new CommentResponseDTO(comment, - users.get(comment.getCreatorId()), - comment.getReplyUser() != null ? users.get(comment.getReplyUser()) : null)) - .collect(Collectors.toList()); + Map commentMap = comments.stream() + .collect(Collectors.toMap( + Comment::getId, + comment -> new CommentResponseDTO(comment, + users.get(comment.getCreatorId()), + comment.getReplyUser() != null ? users.get(comment.getReplyUser()) : null) + )); + + List rootComments = new ArrayList<>(); + + for (Comment comment : comments) { + CommentResponseDTO dto = commentMap.get(comment.getId()); + + if (comment.getDisplayReplyComment() == null) { + rootComments.add(dto); + } else { + CommentResponseDTO parent = commentMap.get(comment.getDisplayReplyComment()); + if (parent != null) { + parent.getReplies().add(dto); + } else { + rootComments.add(dto); + } + } + } + + return rootComments; } } \ No newline at end of file