|
| 1 | +package com.back.domain.board.repository; |
| 2 | + |
| 3 | +import com.back.domain.board.dto.CommentListResponse; |
| 4 | +import com.back.domain.board.dto.QAuthorResponse; |
| 5 | +import com.back.domain.board.dto.QCommentListResponse; |
| 6 | +import com.back.domain.board.entity.Comment; |
| 7 | +import com.back.domain.board.entity.QComment; |
| 8 | +import com.back.domain.board.entity.QCommentLike; |
| 9 | +import com.back.domain.user.entity.QUser; |
| 10 | +import com.back.domain.user.entity.QUserProfile; |
| 11 | +import com.querydsl.core.types.Order; |
| 12 | +import com.querydsl.core.types.OrderSpecifier; |
| 13 | +import com.querydsl.core.types.dsl.BooleanExpression; |
| 14 | +import com.querydsl.core.types.dsl.Expressions; |
| 15 | +import com.querydsl.core.types.dsl.PathBuilder; |
| 16 | +import com.querydsl.jpa.impl.JPAQueryFactory; |
| 17 | +import lombok.RequiredArgsConstructor; |
| 18 | +import org.springframework.data.domain.Page; |
| 19 | +import org.springframework.data.domain.PageImpl; |
| 20 | +import org.springframework.data.domain.Pageable; |
| 21 | +import org.springframework.data.domain.Sort; |
| 22 | + |
| 23 | +import java.util.*; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +@RequiredArgsConstructor |
| 27 | +public class CommentRepositoryImpl implements CommentRepositoryCustom { |
| 28 | + private final JPAQueryFactory queryFactory; |
| 29 | + |
| 30 | + @Override |
| 31 | + public Page<CommentListResponse> getCommentsByPostId(Long postId, Pageable pageable) { |
| 32 | + QComment comment = QComment.comment; |
| 33 | + QCommentLike commentLike = QCommentLike.commentLike; |
| 34 | + |
| 35 | + // 정렬 조건 |
| 36 | + List<OrderSpecifier<?>> orders = buildOrderSpecifiers(pageable, comment, commentLike); |
| 37 | + |
| 38 | + // 부모 댓글 조회 |
| 39 | + List<CommentListResponse> parents = fetchComments( |
| 40 | + comment.post.id.eq(postId).and(comment.parent.isNull()), |
| 41 | + orders, |
| 42 | + pageable.getOffset(), |
| 43 | + pageable.getPageSize() |
| 44 | + ); |
| 45 | + |
| 46 | + if (parents.isEmpty()) { |
| 47 | + return new PageImpl<>(parents, pageable, 0); |
| 48 | + } |
| 49 | + |
| 50 | + // 부모 id 수집 |
| 51 | + List<Long> parentIds = parents.stream() |
| 52 | + .map(CommentListResponse::getCommentId) |
| 53 | + .toList(); |
| 54 | + |
| 55 | + // 자식 댓글 조회 |
| 56 | + List<CommentListResponse> children = fetchComments( |
| 57 | + comment.parent.id.in(parentIds), |
| 58 | + List.of(comment.createdAt.asc()), |
| 59 | + null, |
| 60 | + null |
| 61 | + ); |
| 62 | + |
| 63 | + // 부모 + 자식 id 합쳐서 likeCount 한 번에 조회 |
| 64 | + List<Long> allIds = new ArrayList<>(parentIds); |
| 65 | + allIds.addAll(children.stream().map(CommentListResponse::getCommentId).toList()); |
| 66 | + |
| 67 | + Map<Long, Long> likeCountMap = queryFactory |
| 68 | + .select(commentLike.comment.id, commentLike.count()) |
| 69 | + .from(commentLike) |
| 70 | + .where(commentLike.comment.id.in(allIds)) |
| 71 | + .groupBy(commentLike.comment.id) |
| 72 | + .fetch() |
| 73 | + .stream() |
| 74 | + .collect(Collectors.toMap( |
| 75 | + tuple -> tuple.get(commentLike.comment.id), |
| 76 | + tuple -> tuple.get(commentLike.count()) |
| 77 | + )); |
| 78 | + |
| 79 | + // likeCount 세팅 |
| 80 | + parents.forEach(p -> p.setLikeCount(likeCountMap.getOrDefault(p.getCommentId(), 0L))); |
| 81 | + children.forEach(c -> c.setLikeCount(likeCountMap.getOrDefault(c.getCommentId(), 0L))); |
| 82 | + |
| 83 | + // parentId → children 매핑 |
| 84 | + Map<Long, List<CommentListResponse>> childMap = children.stream() |
| 85 | + .collect(Collectors.groupingBy(CommentListResponse::getParentId)); |
| 86 | + |
| 87 | + parents.forEach(p -> |
| 88 | + p.setChildren(childMap.getOrDefault(p.getCommentId(), List.of())) |
| 89 | + ); |
| 90 | + |
| 91 | + // 총 개수 (부모 댓글만 카운트) |
| 92 | + Long total = queryFactory |
| 93 | + .select(comment.count()) |
| 94 | + .from(comment) |
| 95 | + .where(comment.post.id.eq(postId).and(comment.parent.isNull())) |
| 96 | + .fetchOne(); |
| 97 | + |
| 98 | + return new PageImpl<>(parents, pageable, total != null ? total : 0L); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * 공통 댓글 조회 메서드 (부모/자식 공통) |
| 103 | + */ |
| 104 | + private List<CommentListResponse> fetchComments( |
| 105 | + BooleanExpression condition, |
| 106 | + List<OrderSpecifier<?>> orders, |
| 107 | + Long offset, |
| 108 | + Integer limit |
| 109 | + ) { |
| 110 | + QComment comment = QComment.comment; |
| 111 | + QUser user = QUser.user; |
| 112 | + QUserProfile profile = QUserProfile.userProfile; |
| 113 | + |
| 114 | + var query = queryFactory |
| 115 | + .select(new QCommentListResponse( |
| 116 | + comment.id, |
| 117 | + comment.post.id, |
| 118 | + comment.parent.id, |
| 119 | + new QAuthorResponse(user.id, profile.nickname), |
| 120 | + comment.content, |
| 121 | + Expressions.constant(0L), // likeCount placeholder |
| 122 | + comment.createdAt, |
| 123 | + comment.updatedAt, |
| 124 | + Expressions.constant(Collections.emptyList()) |
| 125 | + )) |
| 126 | + .from(comment) |
| 127 | + .leftJoin(comment.user, user) |
| 128 | + .leftJoin(user.userProfile, profile) |
| 129 | + .where(condition) |
| 130 | + .orderBy(orders.toArray(new OrderSpecifier[0])); |
| 131 | + |
| 132 | + if (offset != null && limit != null) { |
| 133 | + query.offset(offset).limit(limit); |
| 134 | + } |
| 135 | + |
| 136 | + return query.fetch(); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * 정렬 조건 처리 |
| 141 | + */ |
| 142 | + private List<OrderSpecifier<?>> buildOrderSpecifiers(Pageable pageable, QComment comment, QCommentLike commentLike) { |
| 143 | + PathBuilder<Comment> entityPath = new PathBuilder<>(Comment.class, comment.getMetadata()); |
| 144 | + List<OrderSpecifier<?>> orders = new ArrayList<>(); |
| 145 | + |
| 146 | + for (Sort.Order order : pageable.getSort()) { |
| 147 | + Order direction = order.isAscending() ? Order.ASC : Order.DESC; |
| 148 | + String prop = order.getProperty(); |
| 149 | + |
| 150 | + switch (prop) { |
| 151 | + case "likeCount" -> orders.add(new OrderSpecifier<>(direction, commentLike.id.countDistinct())); |
| 152 | + default -> orders.add(new OrderSpecifier<>(direction, |
| 153 | + entityPath.getComparable(prop, Comparable.class))); |
| 154 | + } |
| 155 | + } |
| 156 | + return orders; |
| 157 | + } |
| 158 | +} |
0 commit comments