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 @@ -34,28 +34,23 @@ Page<NoticeJpaEntity> search(
Pageable pageable
);

@Query("""
SELECT n FROM NoticeJpaEntity n
WHERE n.isDeleted = false
AND (:category IS NULL OR n.category = :category)
ORDER BY n.pinned DESC, n.createdAt DESC, n.id DESC
""")
List<NoticeJpaEntity> findFirstSlice(@Param("category") NoticeCategory category, Pageable pageable);

// 커서가 없으면(cursorId가 null) 첫 페이지, 있으면 그 다음부터 조회한다. 커서의 세 값(pinned, createdAt, id)은 함께 넘긴다.
// 정렬이 pinned DESC, createdAt DESC, id DESC라 "다음"은 정렬상 뒤쪽이다. boolean은 0/1로 바꿔 비교한다
@Query("""
SELECT n FROM NoticeJpaEntity n
WHERE n.isDeleted = false
AND (:category IS NULL OR n.category = :category)
AND (
(CASE WHEN n.pinned = true THEN 1 ELSE 0 END) < (CASE WHEN :cursorPinned = true THEN 1 ELSE 0 END)
:cursorId IS NULL
OR (CASE WHEN n.pinned = true THEN 1 ELSE 0 END) < (CASE WHEN :cursorPinned = true THEN 1 ELSE 0 END)
OR (n.pinned = :cursorPinned AND n.createdAt < :cursorCreatedAt)
OR (n.pinned = :cursorPinned AND n.createdAt = :cursorCreatedAt AND n.id < :cursorId)
)
ORDER BY n.pinned DESC, n.createdAt DESC, n.id DESC
""")
List<NoticeJpaEntity> findNextSlice(
List<NoticeJpaEntity> findSlice(
@Param("category") NoticeCategory category,
@Param("cursorPinned") boolean cursorPinned,
@Param("cursorPinned") Boolean cursorPinned,
@Param("cursorCreatedAt") LocalDateTime cursorCreatedAt,
@Param("cursorId") Long cursorId,
Pageable pageable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ public class NoticeRepositoryImpl implements NoticeRepository {
@Override
public CursorSliceResult<Notice> findAll(NoticeCategory category, NoticeCursor cursor, int size) {
Pageable pageable = Pageable.ofSize(size + 1);
List<NoticeJpaEntity> entities = cursor == null
? noticeJpaRepository.findFirstSlice(category, pageable)
: noticeJpaRepository.findNextSlice(category, cursor.pinned(), cursor.createdAt(), cursor.id(), pageable);
List<NoticeJpaEntity> entities = noticeJpaRepository.findSlice(
category,
cursor == null ? null : cursor.pinned(),
cursor == null ? null : cursor.createdAt(),
cursor == null ? null : cursor.id(),
pageable
);

return CursorSliceResult.ofSlice(
entities, size, NoticeJpaEntity::toDomain, notice -> NoticeCursor.of(notice, category).format()
Expand Down