Skip to content

Commit 97e547c

Browse files
authored
�bug: community 페이징 수정 (#158)
* fix(community): repository 리스트 조회 offset, limit 추가 * test(community): community 게시글 페이징 테스트 추가 * refactor(community): QueryDSL 조회 수정 * refactor(community): isNotDeleted(), isWriter() 적용
1 parent f874aee commit 97e547c

File tree

4 files changed

+59
-17
lines changed

4 files changed

+59
-17
lines changed

src/main/java/com/somemore/community/repository/board/CommunityBoardRepositoryImpl.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public Optional<CommunityBoard> findById(Long id) {
4545
@Override
4646
public Page<CommunityBoardView> findCommunityBoards(Pageable pageable) {
4747
List<CommunityBoardView> content = getCommunityBoardsQuery()
48-
.where(QCommunityBoard.communityBoard.deleted.eq(false))
48+
.where(isNotDeleted())
49+
.offset(pageable.getOffset())
50+
.limit(pageable.getPageSize())
4951
.fetch();
5052

5153
JPAQuery<Long> countQuery = queryFactory
@@ -59,14 +61,17 @@ public Page<CommunityBoardView> findCommunityBoards(Pageable pageable) {
5961
@Override
6062
public Page<CommunityBoardView> findByWriterId(UUID writerId, Pageable pageable) {
6163
List<CommunityBoardView> content = getCommunityBoardsQuery()
62-
.where(QCommunityBoard.communityBoard.writerId.eq(writerId)
63-
.and(QCommunityBoard.communityBoard.deleted.eq(false)))
64+
.where(isWriter(writerId)
65+
.and(isNotDeleted()))
66+
.offset(pageable.getOffset())
67+
.limit(pageable.getPageSize())
6468
.fetch();
6569

6670
JPAQuery<Long> countQuery = queryFactory
6771
.select(communityBoard.count())
6872
.from(communityBoard)
69-
.where(isNotDeleted());
73+
.where(isWriter(writerId)
74+
.and(isNotDeleted()));
7075

7176
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
7277
}
@@ -95,4 +100,6 @@ public void deleteAllInBatch() {
95100
private BooleanExpression isNotDeleted() {
96101
return communityBoard.deleted.eq(false);
97102
}
103+
104+
private BooleanExpression isWriter(UUID writerId) {return communityBoard.writerId.eq(writerId); }
98105
}

src/main/java/com/somemore/community/repository/comment/CommunityCommentRepositoryImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public Page<CommunityCommentView> findCommentsByBoardId(Long boardId, Pageable p
5353

5454
JPAQuery<Long> countQuery = queryFactory
5555
.select(communityComment.count())
56-
.from(communityComment);
56+
.from(communityComment)
57+
.where(communityComment.communityBoardId.eq(boardId));
5758

5859
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
5960
}

src/test/java/com/somemore/community/service/board/CommunityBoardQueryServiceTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ void setUp() {
6565
communityBoardRepository.save(createCommunityBoard(writerId2));
6666

6767
communityId1 = communityBoard1.getId();
68+
69+
for (int i = 1; i <= 11; i++) {
70+
String title = "제목" + i;
71+
CommunityBoard communityBoard = createCommunityBoard(title, writerId1);
72+
communityBoardRepository.save(communityBoard);
73+
}
74+
75+
for (int i = 1; i <= 20; i++) {
76+
String title = "제목" + i;
77+
CommunityBoard communityBoard = createCommunityBoard(title, writerId2);
78+
communityBoardRepository.save(communityBoard);
79+
}
6880
}
6981

7082
@AfterEach
@@ -82,8 +94,9 @@ void getAllCommunityBoards() {
8294

8395
//then
8496
assertThat(dtos).isNotNull();
85-
assertThat(dtos.getTotalElements()).isEqualTo(2);
97+
assertThat(dtos.getTotalElements()).isEqualTo(33);
8698
assertThat(dtos.getSize()).isEqualTo(10);
99+
assertThat(dtos.getTotalPages()).isEqualTo(4);
87100
assertThat(dtos.getNumber()).isZero();
88101
}
89102

@@ -97,8 +110,9 @@ void getCommunityBoardsByWriter() {
97110

98111
//then
99112
assertThat(dtos).isNotNull();
100-
assertThat(dtos.getTotalElements()).isEqualTo(1);
113+
assertThat(dtos.getTotalElements()).isEqualTo(12);
101114
assertThat(dtos.getSize()).isEqualTo(10);
115+
assertThat(dtos.getTotalPages()).isEqualTo(2);
102116
assertThat(dtos.getNumber()).isZero();
103117
}
104118

src/test/java/com/somemore/community/service/comment/CommunityCommentQueryServiceTest.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class CommunityCommentQueryServiceTest extends IntegrationTestSupport {
3535
@Autowired
3636
private DeleteCommunityCommentUseCase deleteCommunityCommentUseCase;
3737

38-
private Long boardId, commentId, replyId;
38+
private Long boardId, commentId, replyId, commentId2;
3939
UUID writerId;
4040

4141
@BeforeEach
@@ -53,7 +53,11 @@ void setUp() {
5353
communityCommentRepository.save(communityComment1);
5454
commentId = communityComment1.getId();
5555

56-
for (int i = 1; i <= 8; i++) {
56+
CommunityComment communityComment2 = createCommunityComment(boardId, writerId);
57+
communityCommentRepository.save(communityComment2);
58+
commentId2 = communityComment2.getId();
59+
60+
for (int i = 1; i <= 12; i++) {
5761
String content = "제목" + i;
5862
CommunityComment communityComment = createCommunityComment(content, boardId, writerId);
5963
communityCommentRepository.save(communityComment);
@@ -79,14 +83,15 @@ void getCommentsByCommunityBoardId() {
7983

8084
//then
8185
assertThat(comments).isNotNull();
82-
assertThat(comments.getTotalElements()).isEqualTo(10);
86+
assertThat(comments.getTotalElements()).isEqualTo(15);
8387
assertThat(comments.getSize()).isEqualTo(4);
84-
assertThat(comments.getNumber()).isZero();
88+
assertThat(comments.getTotalPages()).isEqualTo(4);
8589
}
8690

87-
@DisplayName("삭제된 댓글의 경우 조회할 수 없다.")
91+
@DisplayName("삭제된 대댓글의 경우 조회할 수 없다.")
8892
@Test
89-
void doesNotFind() {
93+
void doesNotFindReply() {
94+
9095
//given
9196
deleteCommunityCommentUseCase.deleteCommunityComment(writerId, replyId, boardId);
9297

@@ -95,9 +100,24 @@ void doesNotFind() {
95100

96101
//then
97102
assertThat(comments).isNotNull();
98-
assertThat(comments.getTotalElements()).isEqualTo(9);
103+
assertThat(comments.getTotalElements()).isEqualTo(14);
104+
assertThat(comments.getSize()).isEqualTo(4);
105+
}
106+
107+
@DisplayName("대댓글이 없는 삭제된 댓글의 경우 조회할 수 없다.")
108+
@Test
109+
void doesNotFindCommennt() {
110+
111+
//given
112+
deleteCommunityCommentUseCase.deleteCommunityComment(writerId, commentId2, boardId);
113+
114+
//when
115+
Page<CommunityCommentResponseDto> comments = communityCommentQueryService.getCommunityCommentsByBoardId(boardId, 0);
116+
117+
//then
118+
assertThat(comments).isNotNull();
119+
assertThat(comments.getTotalElements()).isEqualTo(14);
99120
assertThat(comments.getSize()).isEqualTo(4);
100-
assertThat(comments.getNumber()).isZero();
101121
}
102122

103123
@DisplayName("대댓글이 있는 댓글의 경우 삭제된 댓글로 조회할 수 있다.")
@@ -106,13 +126,13 @@ void getCommentsByCommunityBoardIdWithDeletedComment() {
106126

107127
//given
108128
deleteCommunityCommentUseCase.deleteCommunityComment(writerId, commentId, boardId);
129+
109130
//when
110131
Page<CommunityCommentResponseDto> comments = communityCommentQueryService.getCommunityCommentsByBoardId(boardId, 0);
111132

112133
//then
113134
assertThat(comments).isNotNull();
114-
assertThat(comments.getTotalElements()).isEqualTo(10);
135+
assertThat(comments.getTotalElements()).isEqualTo(15);
115136
assertThat(comments.getSize()).isEqualTo(4);
116-
assertThat(comments.getNumber()).isZero();
117137
}
118138
}

0 commit comments

Comments
 (0)