Skip to content

Commit

Permalink
Fix: 사용자 댓글 조회 시 게시글을 찾을 수 없는 오류 해결
Browse files Browse the repository at this point in the history
Fix: 사용자 댓글 조회 시 게시글을 찾을 수 없는 오류 해결
  • Loading branch information
sansan20535 authored Jan 21, 2025
2 parents 9b3522f + f475a04 commit c0e91e6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,9 @@ public CommentsAndSubCommentsResponse getPostComments(final Long postId, final L
}

public MyAllCommentsResponse getMemberComments(final String nickname, final Long memberId) {
final Long selectedMemberId = (nickname != null ) ? findMemberByNickname(nickname): memberId;
final List<Comment> comments = commentRepository.findByMemberIdOrderByCreatedAtAsc(selectedMemberId);
final List<Long> commentIds = comments.stream()
.map(Comment::getId)
.toList();
final List<SubComment> subComments = subCommentRepository.findByCommentIdInOrderByCreatedAtAsc(commentIds);
final Long selectedMemberId = findMemberByNickname(nickname, memberId);

final List<Comment> comments = commentRepository.findAllByMemberIdOrderByCreatedAtDesc(selectedMemberId);
final List<MyCommentResponse> myComments = comments.stream()
.map(comment -> {
final String postTitle = postRepository.findById(comment.getPostId()).orElseThrow(() -> new CocosException(FailMessage.NOT_FOUND_POST)).getTitle();
Expand All @@ -175,12 +172,14 @@ public MyAllCommentsResponse getMemberComments(final String nickname, final Long
);
}).toList();

final List<SubComment> subComments = subCommentRepository.findAllByMemberIdOrderByCreatedAtDesc(selectedMemberId);
final List<MySubCommentResponse> mySubComments = subComments.stream()
.map(subComment -> {
final Comment comment = commentRepository.findById(subComment.getCommentId()).orElseThrow(
() -> new CocosException(FailMessage.NOT_FOUND_COMMENT)
);
final Post post = postRepository.findById(comment.getId()).orElseThrow(

final Post post = postRepository.findById(comment.getPostId()).orElseThrow(
() -> new CocosException(FailMessage.NOT_FOUND_POST)
);

Expand All @@ -196,7 +195,7 @@ public MyAllCommentsResponse getMemberComments(final String nickname, final Long
mentionedMember.getNickname()
);
})
.collect(Collectors.toList());
.toList();

return MyAllCommentsResponse.of(
myComments,
Expand Down Expand Up @@ -249,15 +248,15 @@ private int getOrDefaultPetAge(Long memberId, Map<Long, List<Pet>> petMap) {
.orElseThrow(() -> new CocosException(FailMessage.INTERNAL_SERVER_ERROR_PET_AGE));
}

private Long findMemberByNickname(String nickname) {
private Long findMemberByNickname(final String nickname, final Long memberId) {
if (nickname != null) {
final Member member = memberRepository.findByNickname(nickname);
if (member == null) {
throw new CocosException((FailMessage.NOT_FOUND_MEMBER));
}
return member.getId();
} else {
return null;
return memberId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {

List<Comment> findByPostIdOrderByCreatedAtAsc(@Param("postId") Long postId);

List<Comment> findByMemberIdOrderByCreatedAtAsc(final Long memberId);
List<Comment> findAllByMemberIdOrderByCreatedAtDesc(final Long memberId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ public interface SubCommentRepository extends JpaRepository<SubComment, Long> {

List<SubComment> findByCommentIdInOrderByCreatedAtAsc(@Param("commentId") List<Long> commentId);

List<SubComment> findByMemberIdOrderByCreatedAtAsc(final Long memberId);
List<SubComment> findAllByMemberIdOrderByCreatedAtDesc(final Long memberId);
}

0 comments on commit c0e91e6

Please sign in to comment.