feat: implement listing posts in a hierarchical structure

This commit is contained in:
sBubshait 2025-07-27 10:46:59 +03:00
parent 304ba5c731
commit 1544b37ed3
3 changed files with 40 additions and 6 deletions

View File

@ -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<ApiResponse<List<CommentResponseDTO>>> getCommentsByPostId(
@RequestParam Long postId,
Authentication authentication) {

View File

@ -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<CommentResponseDTO> 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<CommentResponseDTO> getReplies() {
return replies;
}
public void setReplies(List<CommentResponseDTO> replies) {
this.replies = replies;
}
}

View File

@ -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<Long, User> 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<Long, CommentResponseDTO> 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<CommentResponseDTO> 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;
}
}