Skip to content
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 @@ -10,4 +10,5 @@ public record CommentListResponse(
String profileImageUrl,
@Schema(description = "댓글 내용", example = "이 옷 정보 알려주세요!") String content,
@Schema(description = "대댓글 존재 여부", example = "false") boolean replied,
@Schema(description = "총 대댓글 개수", example = "0") long replyCount,
@Schema(description = "내가 작성한 댓글인가?", example = "false") boolean isMine) {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import static org.clokey.history.entity.QHistoryImage.historyImage;
import static org.clokey.member.entity.QMember.member;

import com.querydsl.core.Tuple;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.NumberExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -52,6 +54,7 @@ public Slice<CommentListResponse> findAllParentCommentByHistoryId(
member.profileImageUrl,
comment.content,
Expressions.constant(false),
Expressions.constant(0L),
member.id.eq(currentMemberId)))
.from(comment)
.join(comment.member, member)
Expand All @@ -71,9 +74,10 @@ public Slice<CommentListResponse> findAllParentCommentByHistoryId(
results = results.subList(0, size);
}

List<Long> parentIdsWithReplies =
NumberExpression<Long> replyCountExpression = comment.id.count();
List<Tuple> replyCounts =
queryFactory
.select(comment.comment.id)
.select(comment.comment.id, replyCountExpression)
.from(comment)
.where(
comment.comment.id.in(
Expand All @@ -83,9 +87,13 @@ public Slice<CommentListResponse> findAllParentCommentByHistoryId(
.groupBy(comment.comment.id)
.fetch();

// 각 부모 댓글이 대댓글을 가지고 있는지 여부 조회
Map<Long, Boolean> repliedMap =
parentIdsWithReplies.stream().collect(Collectors.toMap(id -> id, id -> true));
// 각 부모 댓글별 대댓글 개수 조회
Map<Long, Long> replyCountMap =
replyCounts.stream()
.collect(
Collectors.toMap(
tuple -> tuple.get(comment.comment.id),
tuple -> tuple.get(replyCountExpression)));

List<CommentListResponse> finalResults =
results.stream()
Expand All @@ -97,7 +105,8 @@ public Slice<CommentListResponse> findAllParentCommentByHistoryId(
c.nickname(),
c.profileImageUrl(),
c.content(),
repliedMap.getOrDefault(c.commentId(), false),
replyCountMap.getOrDefault(c.commentId(), 0L) > 0,
replyCountMap.getOrDefault(c.commentId(), 0L),
c.isMine()))
.toList();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.clokey.domain.comment.controller;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willDoNothing;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
Expand Down Expand Up @@ -292,6 +291,7 @@ class 기록의_댓글_목록_조회_요청_시 {
"testProfile",
"testContent1",
false,
0L,
true),
new CommentListResponse(
2L,
Expand All @@ -300,6 +300,7 @@ class 기록의_댓글_목록_조회_요청_시 {
"testProfile",
"testContent2",
false,
0L,
true));

given(commentService.getHistoryComments(1L, null, 2, SortDirection.ASC))
Expand Down Expand Up @@ -333,6 +334,7 @@ class 기록의_댓글_목록_조회_요청_시 {
"testProfile",
"testContent2",
false,
0L,
true),
new CommentListResponse(
1L,
Expand All @@ -341,6 +343,7 @@ class 기록의_댓글_목록_조회_요청_시 {
"testProfile",
"testContent1",
false,
0L,
true));

given(commentService.getHistoryComments(1L, null, 2, SortDirection.DESC))
Expand Down Expand Up @@ -374,6 +377,7 @@ class 기록의_댓글_목록_조회_요청_시 {
"testProfile",
"testContent2",
false,
0L,
true));

given(commentService.getHistoryComments(1L, null, 1, SortDirection.ASC))
Expand Down Expand Up @@ -406,6 +410,7 @@ class 기록의_댓글_목록_조회_요청_시 {
"testProfile",
"testContent1",
false,
0L,
true),
new CommentListResponse(
2L,
Expand All @@ -414,6 +419,7 @@ class 기록의_댓글_목록_조회_요청_시 {
"testProfile",
"testContent2",
false,
0L,
true));

given(commentService.getHistoryComments(1L, null, 1, SortDirection.ASC))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ void setUp() {

// then
assertThat(response.content()).extracting("commentId").containsExactly(1L, 2L, 3L);
assertThat(response.content())
.extracting("commentId", "replied", "replyCount")
.containsExactly(
tuple(1L, true, 1L), tuple(2L, false, 0L), tuple(3L, false, 0L));
}

@Test
Expand Down