Skip to content

Commit 7d386a0

Browse files
committed
refactor: CustomDiaryRepositoryImpl 리팩토링
1 parent f1bf3a3 commit 7d386a0

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/main/java/com/example/log4u/domain/diary/repository/CustomDiaryRepositoryImpl.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ public Page<Diary> searchDiaries(
4242
.where(condition);
4343

4444
// 전체 카운트 조회
45-
long total = query.fetchCount();
45+
Long total = queryFactory
46+
.select(diary.count())
47+
.from(diary)
48+
.where(condition)
49+
.fetchOne();
4650

4751
// 데이터 조회
4852
List<Diary> content = query
@@ -51,7 +55,7 @@ public Page<Diary> searchDiaries(
5155
.limit(pageable.getPageSize())
5256
.fetch();
5357

54-
return new PageImpl<>(content, pageable, total);
58+
return new PageImpl<>(content, pageable, total != null ? total : 0);
5559
}
5660

5761
@Override
@@ -74,6 +78,7 @@ public Slice<Diary> findByUserIdAndVisibilityInAndCursorId(
7478
.limit(pageable.getPageSize() + 1)
7579
.fetch();
7680

81+
// 다음 페이지 여부를 계산하여 반환
7782
return checkAndCreateSlice(content, pageable);
7883
}
7984

@@ -86,11 +91,13 @@ private BooleanExpression createCondition(
8691
) {
8792
BooleanExpression condition = diary.visibility.in(visibilities);
8893

89-
if (keyword != null && StringUtils.hasText(keyword)) {
94+
// keyword가 있을 경우
95+
if (StringUtils.hasText(keyword)) {
9096
condition = condition.and(diary.title.containsIgnoreCase(keyword)
9197
.or(diary.content.containsIgnoreCase(keyword)));
9298
}
9399

100+
// userId가 있을 경우
94101
if (userId != null) {
95102
condition = condition.and(diary.userId.eq(userId));
96103
}
@@ -116,7 +123,7 @@ private Slice<Diary> checkAndCreateSlice(List<Diary> content, Pageable pageable)
116123

117124
// 다음 페이지가 있으면 마지막 항목 제거
118125
if (hasNext) {
119-
content.removeLast();
126+
content.remove(content.size() - 1); // removeLast() 대신 인덱스로 처리
120127
}
121128

122129
return new SliceImpl<>(content, pageable, hasNext);

0 commit comments

Comments
 (0)