Skip to content

Commit 9fa39ba

Browse files
authored
Merge pull request #39 from prgrms-web-devcourse-final-project/refactor/comment-read
특정 다이어리 댓글 전체 조회 API 반환 데이터 수정
2 parents f6e9fd1 + 2bbcd8e commit 9fa39ba

File tree

6 files changed

+69
-26
lines changed

6 files changed

+69
-26
lines changed
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
package com.example.log4u.domain.comment.dto.response;
22

3+
import java.time.LocalDateTime;
4+
35
import com.example.log4u.domain.comment.entity.Comment;
6+
import com.example.log4u.domain.user.entity.User;
47

58
public record CommentResponseDto(
69
Long commentId,
7-
String content
10+
Long userId,
11+
String userName,
12+
String userProfileImage,
13+
String content,
14+
LocalDateTime createdAt
815
) {
9-
public static CommentResponseDto of(Comment comment) {
16+
public static CommentResponseDto of(Comment comment, User user) {
1017
return new CommentResponseDto(
1118
comment.getCommentId(),
12-
comment.getContent()
19+
user.getUserId(),
20+
user.getName(),
21+
user.getProfileImage(),
22+
comment.getContent(),
23+
comment.getCreatedAt()
1324
);
1425
}
1526
}
27+

src/main/java/com/example/log4u/domain/comment/repository/CommentRepositoryCustom.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import org.springframework.data.domain.Pageable;
44
import org.springframework.data.domain.Slice;
55

6+
import com.example.log4u.domain.comment.dto.response.CommentResponseDto;
67
import com.example.log4u.domain.comment.entity.Comment;
78

89
public interface CommentRepositoryCustom {
910

10-
Slice<Comment> findByDiaryIdWithCursor(Long diaryId, Long cursorCommentId, Pageable pageable);
11+
Slice<CommentResponseDto> findWithUserByDiaryId(Long diaryId, Long cursorCommentId, Pageable pageable);
1112
}

src/main/java/com/example/log4u/domain/comment/repository/CommentRepositoryImpl.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
import org.springframework.data.domain.SliceImpl;
1010
import org.springframework.stereotype.Repository;
1111

12+
import com.example.log4u.domain.comment.dto.response.CommentResponseDto;
1213
import com.example.log4u.domain.comment.entity.Comment;
14+
import com.example.log4u.domain.comment.entity.QComment;
15+
import com.example.log4u.domain.user.entity.QUser;
16+
import com.example.log4u.domain.user.entity.User;
17+
import com.querydsl.core.Tuple;
1318
import com.querydsl.jpa.impl.JPAQueryFactory;
1419

1520
import lombok.RequiredArgsConstructor;
@@ -21,19 +26,31 @@ public class CommentRepositoryImpl implements CommentRepositoryCustom {
2126
private final JPAQueryFactory queryFactory;
2227

2328
@Override
24-
public Slice<Comment> findByDiaryIdWithCursor(Long diaryId, Long cursorCommentId, Pageable pageable) {
25-
List<Comment> result = queryFactory
26-
.selectFrom(comment)
29+
public Slice<CommentResponseDto> findWithUserByDiaryId(Long diaryId, Long cursorCommentId, Pageable pageable) {
30+
QComment comment = QComment.comment;
31+
QUser user = QUser.user;
32+
33+
List<Tuple> tuples = queryFactory
34+
.select(comment, user)
35+
.from(comment)
36+
.join(user).on(comment.userId.eq(user.userId))
2737
.where(
2838
comment.diaryId.eq(diaryId),
2939
cursorCommentId != null ? comment.commentId.lt(cursorCommentId) : null
3040
)
3141
.orderBy(comment.commentId.desc())
32-
.limit(pageable.getPageSize() + 1) // 커서 기반 페이징
42+
.limit(pageable.getPageSize() + 1)
3343
.fetch();
3444

35-
boolean hasNext = result.size() > pageable.getPageSize();
36-
List<Comment> content = hasNext ? result.subList(0, pageable.getPageSize()) : result;
45+
boolean hasNext = tuples.size() > pageable.getPageSize();
46+
List<CommentResponseDto> content = tuples.stream()
47+
.limit(pageable.getPageSize())
48+
.map(tuple -> {
49+
Comment c = tuple.get(comment);
50+
User u = tuple.get(user);
51+
return CommentResponseDto.of(c, u);
52+
})
53+
.toList();
3754

3855
return new SliceImpl<>(content, pageable, hasNext);
3956
}

src/main/java/com/example/log4u/domain/comment/service/CommentService.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,10 @@ private Comment getComment(Long commentId) {
6262
public PageResponse<CommentResponseDto> getCommentListByDiary(Long diaryId, Long cursorCommentId, int size) {
6363
checkDiaryExists(diaryId);
6464
Pageable pageable = PageRequest.of(0, size);
65-
Slice<Comment> slice = commentRepository.findByDiaryIdWithCursor(diaryId, cursorCommentId, pageable);
65+
Slice<CommentResponseDto> slice = commentRepository.findWithUserByDiaryId(diaryId, cursorCommentId, pageable);
6666

67-
List<CommentResponseDto> dtoList = slice.getContent().stream()
68-
.map(CommentResponseDto::of)
69-
.toList();
70-
71-
Long nextCursor = slice.hasNext() ? dtoList.getLast().commentId() : null;
72-
return PageResponse.of(new SliceImpl<>(dtoList, pageable, slice.hasNext()), nextCursor);
67+
Long nextCursor = slice.hasNext() ? slice.getContent().getLast().commentId() : null;
68+
return PageResponse.of(slice, nextCursor);
7369
}
70+
7471
}

src/test/java/com/example/log4u/domain/comment/service/CommonServiceTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.List;
88
import java.util.Optional;
9+
import java.util.stream.IntStream;
910

1011
import org.junit.jupiter.api.DisplayName;
1112
import org.junit.jupiter.api.Test;
@@ -149,26 +150,26 @@ void getCommentsList_In_DiaryDetail_Success() {
149150
int size = 5;
150151
Long cursorCommentId = null;
151152

152-
List<Comment> commentList = CommentFixture.createCommentsListFixture(size + 1); // hasNext 판별 위해 +1
153+
List<CommentResponseDto> dtoList = CommentFixture.createCommentDtos(size + 1);
154+
153155
Pageable pageable = PageRequest.of(0, size);
154-
boolean hasNext = commentList.size() > size;
156+
boolean hasNext = dtoList.size() > size;
157+
List<CommentResponseDto> sliced = hasNext ? dtoList.subList(0, size) : dtoList;
155158

156-
List<Comment> sliced = hasNext ? commentList.subList(0, size) : commentList;
159+
Slice<CommentResponseDto> slice = new SliceImpl<>(sliced, pageable, hasNext);
157160

158-
Slice<Comment> slice = new SliceImpl<>(sliced, pageable, hasNext);
159-
given(commentRepository.findByDiaryIdWithCursor(diaryId, cursorCommentId, pageable))
161+
given(commentRepository.findWithUserByDiaryId(diaryId, cursorCommentId, pageable))
160162
.willReturn(slice);
161163

162164
// when
163-
PageResponse<CommentResponseDto> response = commentService.getCommentListByDiary(diaryId, cursorCommentId,
164-
size);
165+
PageResponse<CommentResponseDto> response = commentService.getCommentListByDiary(diaryId, cursorCommentId, size);
165166

166167
// then
167168
assertThat(response.list()).hasSize(sliced.size());
168169
assertThat(response.pageInfo().hasNext()).isEqualTo(hasNext);
169-
assertThat(response.pageInfo().nextCursor()).isEqualTo(hasNext ? sliced.getLast().getCommentId() : null);
170+
assertThat(response.pageInfo().nextCursor()).isEqualTo(hasNext ? sliced.getLast().commentId() : null);
170171

171-
verify(commentRepository).findByDiaryIdWithCursor(diaryId, cursorCommentId, pageable);
172+
verify(commentRepository).findWithUserByDiaryId(diaryId, cursorCommentId, pageable);
172173
}
173174

174175
}

src/test/java/com/example/log4u/fixture/CommentFixture.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.example.log4u.fixture;
22

3+
import java.time.LocalDateTime;
34
import java.util.ArrayList;
45
import java.util.List;
6+
import java.util.stream.IntStream;
57

8+
import com.example.log4u.domain.comment.dto.response.CommentResponseDto;
69
import com.example.log4u.domain.comment.entity.Comment;
710

811
public class CommentFixture {
@@ -34,4 +37,16 @@ public static List<Comment> createCommentsListFixture(int count) {
3437
}
3538
return comments;
3639
}
40+
41+
public static List<CommentResponseDto> createCommentDtos(int size) {
42+
return IntStream.rangeClosed(1, size)
43+
.mapToObj(i -> new CommentResponseDto(
44+
(long) i,
45+
(long) i,
46+
"사용자" + i,
47+
"https://cdn.example.com/user" + i + ".png",
48+
"댓글 " + i,
49+
LocalDateTime.now().minusMinutes(i) // createdAt
50+
)).toList();
51+
}
3752
}

0 commit comments

Comments
 (0)