From f747b5c229638331236f2694f1e4f572c66c2124 Mon Sep 17 00:00:00 2001 From: jjunh33 Date: Thu, 22 Oct 2026 18:46:51 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EA=B3=B5=EC=A7=80=20=EC=BB=A4?= =?UTF-8?q?=EC=84=9C=20=EB=AA=A9=EB=A1=9D=20=EC=BF=BC=EB=A6=AC=EB=A5=BC=20?= =?UTF-8?q?=ED=95=98=EB=82=98=EB=A1=9C=20=ED=86=B5=ED=95=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stream/db/welfare/NoticeJpaRepository.java | 17 ++++++----------- .../stream/db/welfare/NoticeRepositoryImpl.java | 10 +++++++--- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/infrastructure/db/src/main/java/kr/ac/kookmin/stream/db/welfare/NoticeJpaRepository.java b/infrastructure/db/src/main/java/kr/ac/kookmin/stream/db/welfare/NoticeJpaRepository.java index 9ee415e8..89eff42b 100644 --- a/infrastructure/db/src/main/java/kr/ac/kookmin/stream/db/welfare/NoticeJpaRepository.java +++ b/infrastructure/db/src/main/java/kr/ac/kookmin/stream/db/welfare/NoticeJpaRepository.java @@ -13,28 +13,23 @@ public interface NoticeJpaRepository extends JpaRepository findByIdAndIsDeletedFalse(Long id); - @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 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 findNextSlice( + List findSlice( @Param("category") NoticeCategory category, - @Param("cursorPinned") boolean cursorPinned, + @Param("cursorPinned") Boolean cursorPinned, @Param("cursorCreatedAt") LocalDateTime cursorCreatedAt, @Param("cursorId") Long cursorId, Pageable pageable diff --git a/infrastructure/db/src/main/java/kr/ac/kookmin/stream/db/welfare/NoticeRepositoryImpl.java b/infrastructure/db/src/main/java/kr/ac/kookmin/stream/db/welfare/NoticeRepositoryImpl.java index aba0e249..1d75fe1d 100644 --- a/infrastructure/db/src/main/java/kr/ac/kookmin/stream/db/welfare/NoticeRepositoryImpl.java +++ b/infrastructure/db/src/main/java/kr/ac/kookmin/stream/db/welfare/NoticeRepositoryImpl.java @@ -20,9 +20,13 @@ public class NoticeRepositoryImpl implements NoticeRepository { @Override public CursorSliceResult findAll(NoticeCategory category, NoticeCursor cursor, int size) { Pageable pageable = Pageable.ofSize(size + 1); - List entities = cursor == null - ? noticeJpaRepository.findFirstSlice(category, pageable) - : noticeJpaRepository.findNextSlice(category, cursor.pinned(), cursor.createdAt(), cursor.id(), pageable); + List 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()