feat: implement listing posts in a hierarchical structure
This commit is contained in:
parent
304ba5c731
commit
1544b37ed3
@ -69,7 +69,7 @@ public class CommentController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/list")
|
@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(
|
public ResponseEntity<ApiResponse<List<CommentResponseDTO>>> getCommentsByPostId(
|
||||||
@RequestParam Long postId,
|
@RequestParam Long postId,
|
||||||
Authentication authentication) {
|
Authentication authentication) {
|
||||||
|
|||||||
@ -3,6 +3,8 @@ package online.wesal.wesal.dto;
|
|||||||
import online.wesal.wesal.entity.Comment;
|
import online.wesal.wesal.entity.Comment;
|
||||||
import online.wesal.wesal.entity.User;
|
import online.wesal.wesal.entity.User;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class CommentResponseDTO {
|
public class CommentResponseDTO {
|
||||||
|
|
||||||
@ -15,6 +17,7 @@ public class CommentResponseDTO {
|
|||||||
private String displayReplyComment;
|
private String displayReplyComment;
|
||||||
private CreatorDTO replyUser;
|
private CreatorDTO replyUser;
|
||||||
private Integer level;
|
private Integer level;
|
||||||
|
private List<CommentResponseDTO> replies;
|
||||||
|
|
||||||
public CommentResponseDTO() {}
|
public CommentResponseDTO() {}
|
||||||
|
|
||||||
@ -28,6 +31,7 @@ public class CommentResponseDTO {
|
|||||||
this.displayReplyComment = comment.getDisplayReplyComment() != null ? String.valueOf(comment.getDisplayReplyComment()) : null;
|
this.displayReplyComment = comment.getDisplayReplyComment() != null ? String.valueOf(comment.getDisplayReplyComment()) : null;
|
||||||
this.replyUser = replyUser != null ? new CreatorDTO(replyUser) : null;
|
this.replyUser = replyUser != null ? new CreatorDTO(replyUser) : null;
|
||||||
this.level = comment.getLevel();
|
this.level = comment.getLevel();
|
||||||
|
this.replies = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getId() {
|
public String getId() {
|
||||||
@ -101,4 +105,12 @@ public class CommentResponseDTO {
|
|||||||
public void setLevel(Integer level) {
|
public void setLevel(Integer level) {
|
||||||
this.level = level;
|
this.level = level;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<CommentResponseDTO> getReplies() {
|
||||||
|
return replies;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReplies(List<CommentResponseDTO> replies) {
|
||||||
|
this.replies = replies;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
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()
|
Map<Long, User> users = userRepository.findAllById(creatorIds.stream().distinct().collect(Collectors.toList())).stream()
|
||||||
.collect(Collectors.toMap(User::getId, user -> user));
|
.collect(Collectors.toMap(User::getId, user -> user));
|
||||||
|
|
||||||
return comments.stream()
|
Map<Long, CommentResponseDTO> commentMap = comments.stream()
|
||||||
.map(comment -> new CommentResponseDTO(comment,
|
.collect(Collectors.toMap(
|
||||||
users.get(comment.getCreatorId()),
|
Comment::getId,
|
||||||
comment.getReplyUser() != null ? users.get(comment.getReplyUser()) : null))
|
comment -> new CommentResponseDTO(comment,
|
||||||
.collect(Collectors.toList());
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user