Skip to content

Commit

Permalink
feat: 댓글 조회 시 commentLike의 수 함께 조회하도록 구현 (#133)
Browse files Browse the repository at this point in the history
* feat: cafe 조회 시 comment에 좋아요 정보 함께 반환

* chore: default_batch_fetch_size를 통해 commentLike N+1문제 해결
  • Loading branch information
jung-woo-kim authored Aug 3, 2023
1 parent facfa91 commit ada2ac4
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/main/java/mocacong/server/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class Comment extends BaseTime {
@OneToMany(mappedBy = "comment", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Report> reports = new ArrayList<>();

@OneToMany(mappedBy = "comment", cascade = CascadeType.ALL, orphanRemoval = true)
private List<CommentLike> commentLikes = new ArrayList<>();

@Column(name = "is_masked")
private boolean isMasked;

Expand Down Expand Up @@ -112,4 +115,9 @@ public void addReport(Report report) {
public void updateIsMasked(boolean isMasked) {
this.isMasked= isMasked;
}

public int getLikeCounts() {
return commentLikes.size();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public class CommentResponse {
private String imgUrl;
private String nickname;
private String content;
private int likeCount;
private Boolean isMe;
}
4 changes: 2 additions & 2 deletions src/main/java/mocacong/server/service/CafeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ private List<CommentResponse> findCommentResponses(Cafe cafe, Member member) {
.map(comment -> {
if (comment.isWrittenByMember(member)) {
return new CommentResponse(comment.getId(), member.getImgUrl(), member.getNickname(),
comment.getContent(), true);
comment.getContent(), comment.getLikeCounts(), true);
} else {
return new CommentResponse(comment.getId(), comment.getWriterImgUrl(),
comment.getWriterNickname(), comment.getContent(), false);
comment.getWriterNickname(), comment.getContent(), comment.getLikeCounts(), false);
}
})
.collect(Collectors.toList());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mocacong/server/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ private List<CommentResponse> findCommentResponses(Member member, Slice<Comment>
return comments.stream()
.map(comment -> {
if (comment.isWrittenByMember(member)) {
return new CommentResponse(comment.getId(), member.getImgUrl(), member.getNickname(), comment.getContent(), true);
return new CommentResponse(comment.getId(), member.getImgUrl(), member.getNickname(), comment.getContent(), comment.getLikeCounts(), true);
} else {
return new CommentResponse(comment.getId(), comment.getWriterImgUrl(), comment.getWriterNickname(), comment.getContent(), false);
return new CommentResponse(comment.getId(), comment.getWriterImgUrl(), comment.getWriterNickname(), comment.getContent(), comment.getLikeCounts(), false);
}
})
.collect(Collectors.toList());
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ spring:
create_empty_composites:
enabled: true
format_sql: true
default_batch_fetch_size: 100
servlet:
multipart:
max-file-size: 10MB
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ spring:
create_empty_composites:
enabled: true
format_sql: true
default_batch_fetch_size: 100
task:
execution:
pool:
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/mocacong/server/service/CafeServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class CafeServiceTest {
private FavoriteRepository favoriteRepository;
@Autowired
private CafeImageRepository cafeImageRepository;
@Autowired
private CommentLikeRepository commentLikeRepository;

@MockBean
private AwsS3Uploader awsS3Uploader;
Expand Down Expand Up @@ -167,6 +169,51 @@ void findCafeWithReviewsAndComments() {
);
}

@Test
@DisplayName("코멘트에 좋아요가 있는 카페를 조회한다")
void findCafeWithCommentLike() {
Member member1 = new Member("[email protected]", "encodePassword", "케이");
memberRepository.save(member1);
Member member2 = new Member("[email protected]", "encodePassword", "메리");
memberRepository.save(member2);
Cafe cafe = new Cafe("2143154352323", "케이카페");
cafeRepository.save(cafe);
cafeService.saveCafeReview(member1.getId(), cafe.getMapId(),
new CafeReviewRequest(1, "group", "느려요", "없어요",
"불편해요", "없어요", "북적북적해요", "불편해요"));
cafeService.saveCafeReview(member2.getId(), cafe.getMapId(),
new CafeReviewRequest(2, "both", "느려요", "없어요",
"깨끗해요", "없어요", null, "보통이에요"));
Comment comment1 = new Comment(cafe, member1, "이 카페 조금 아쉬운 점이 많아요 ㅠㅠ");
commentRepository.save(comment1);
Comment comment2 = new Comment(cafe, member2, "와이파이가 왜케 느릴까요...");
commentRepository.save(comment2);
Comment comment3 = new Comment(cafe, member1, "다시 와봐도 똑같네요. 리뷰 수정할까 하다가 그대로 남겨요..");
commentRepository.save(comment3);
CommentLike commentLike1 = new CommentLike(member1, comment2);
commentLikeRepository.save(commentLike1);
CommentLike commentLike2 = new CommentLike(member2, comment1);
commentLikeRepository.save(commentLike2);

FindCafeResponse actual = cafeService.findCafeByMapId(member1.getId(), cafe.getMapId());

assertAll(
() -> assertThat(actual.getFavorite()).isFalse(),
() -> assertThat(actual.getFavoriteId()).isNull(),
() -> assertThat(actual.getScore()).isEqualTo(1.5),
() -> assertThat(actual.getMyScore()).isEqualTo(1),
() -> assertThat(actual.getStudyType()).isEqualTo("group"),
() -> assertThat(actual.getReviewsCount()).isEqualTo(2),
() -> assertThat(actual.getCommentsCount()).isEqualTo(3),
() -> assertThat(actual.getComments())
.extracting("nickname")
.containsExactlyInAnyOrder("케이", "메리", "케이"),
() -> assertThat(actual.getComments())
.extracting("likeCount")
.containsExactlyInAnyOrder(1, 1, 0)
);
}

@Test
@DisplayName("평점, 리뷰가 존재하지 않는 카페 정보를 미리보기한다")
void previewCafe() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import mocacong.server.domain.Member;
import mocacong.server.dto.request.MemberSignUpRequest;
import mocacong.server.exception.badrequest.DuplicateMemberException;
import mocacong.server.exception.badrequest.DuplicateNicknameException;
import mocacong.server.repository.MemberRepository;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -39,6 +40,8 @@ void signUpWithConcurrent() throws InterruptedException {
memberService.signUp(request);
} catch (DuplicateMemberException e) {
exceptions.add(e); // 중복 예외를 리스트에 추가
} catch (DuplicateNicknameException e) {
exceptions.add(e);
}
latch.countDown();
});
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ spring:
enabled: true
show_sql: true
format_sql: true
default_batch_fetch_size: 100
redis:
host: localhost
port: 16379
Expand Down

0 comments on commit ada2ac4

Please sign in to comment.