Skip to content

대댓글의 순서가 정렬되지 않는 버그 해결 #360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package page.clab.api.domain.comment.application;

import java.util.Comparator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.Hibernate;
Expand Down Expand Up @@ -58,7 +59,10 @@ public Long createComment(Long parentId, Long boardId, CommentRequestDto request
public PagedResponseDto<CommentResponseDto> getAllComments(Long boardId, Pageable pageable) {
Member currentMember = memberService.getCurrentMember();
Page<Comment> comments = getCommentByBoardIdAndParentIsNull(boardId, pageable);
comments.forEach(comment -> Hibernate.initialize(comment.getChildren()));
comments.forEach(comment -> {
Hibernate.initialize(comment.getChildren());
sortChildrenComments(comment);
});
Page<CommentResponseDto> commentDtos = comments.map(comment -> toCommentResponseDto(comment, currentMember));
return new PagedResponseDto<>(commentDtos);
}
Expand Down Expand Up @@ -178,4 +182,8 @@ private CommentMyResponseDto toCommentMyResponseDto(Comment comment, Member curr
return CommentMyResponseDto.toDto(comment, hasLikeByMe);
}

private void sortChildrenComments(Comment comment) {
comment.getChildren().sort(Comparator.comparing(Comment::getCreatedAt));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand Down
Loading