Skip to content
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

[Refactor] Comment 조회 시 이미지가 없다면 null 반환 하도록 수정 #118

Merged
merged 1 commit into from
Jun 1, 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
Expand Up @@ -62,7 +62,7 @@ private static List<String> extractCommentImages(List<CommentImage> commentImage
return Optional.ofNullable(commentImages)
.orElseGet(ArrayList::new)
.stream()
.map(CommentImage::getImageUrl)
.map(commentImage -> commentImage != null ? commentImage.getImageUrl() : null)
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ public CommentResponse writeComment(CommentCreateServiceRequest request, Long ta
request.getImageUrls().stream()
.map(url -> CommentImage.createImages(comment, url))
.forEach(commentImageRepository::save);
} else {
commentImageRepository.save(CommentImage.createImages(comment, ""));
}

List<String> imageUrls = commentImageRepository.findByCommentIdWithImageUrl(comment.getId());
Expand All @@ -83,7 +81,8 @@ public PageResponse<CommentFindAllResponse> findAllComments(Long talkRoomId) {

Long totalCount = commentRepository.commentTotalCount(talkRoom.getId());

return PageResponse.of(findComment.size(), totalCount, CommentFindAllResponse.toList(findComment, commentImages));
return PageResponse.of(findComment.size(), totalCount,
CommentFindAllResponse.toList(findComment, commentImages));
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void writeComment() {
// then
assertThat(response)
.extracting("content", "userName", "imageUrls")
.contains("의견 남기기", "[email protected]", List.of(""));
.contains("의견 남기기", "[email protected]", List.of());
}

@Test
Expand Down Expand Up @@ -580,6 +580,49 @@ void findAllCommentsWithImage() {
);
}

@Test
@DisplayName("의견을 조회할 때 이미지도 같이 조회 된다.(2)")
void findAllCommentsWithImage2() {
// given
User user = userRepository.save(createUser());
Book book = bookRepository.save(createBook());
TalkRoom talkRoom = talkRoomRepository.save(createTalkRoom(book, user));

createTalkRoomRole(talkRoom);

List<Comment> comments = IntStream.range(1, 6)
.mapToObj(i -> Comment.builder()
.talkRoom(talkRoom)
.user(user)
.content("의견 " + i)
.registeredDateTime(LocalDateTime.now())
.build())
.toList();

for (int i = 0; i < 5; i++) {
commentRepository.save(comments.get(i));
}

CommentImage images = CommentImage.createImages(comments.get(0), "이미지 1");

commentImageRepository.save(images);

// when
PageResponse<CommentFindAllResponse> result = commentService.findAllComments(talkRoom.getId());

// then
assertThat(result.getTotalCount()).isEqualTo(5L);
assertThat(result.getQueryResponse()).hasSize(5)
.extracting("content", "commentImages")
.containsExactly(
tuple("의견 5", List.of()),
tuple("의견 4", List.of()),
tuple("의견 3", List.of()),
tuple("의견 2", List.of()),
tuple("의견 1", List.of("이미지 1"))
);
}

private static Comment createComment(User user, TalkRoom talkRoom) {
return Comment.builder()
.content("의견")
Expand Down
Loading