diff --git a/src/main/java/com/example/template/domain/board/entity/Board.java b/src/main/java/com/example/template/domain/board/entity/Board.java index 496a76c..206e9f2 100644 --- a/src/main/java/com/example/template/domain/board/entity/Board.java +++ b/src/main/java/com/example/template/domain/board/entity/Board.java @@ -30,7 +30,7 @@ public class Board extends BaseEntity { private String content; // 내용 @Enumerated(EnumType.STRING) - @Column(nullable = false) + @Column(name = "category", nullable = false) private Category category; // 카테고리 @Enumerated(EnumType.STRING) diff --git a/src/main/java/com/example/template/domain/board/repository/BoardRepository.java b/src/main/java/com/example/template/domain/board/repository/BoardRepository.java index 891564a..bc2971d 100644 --- a/src/main/java/com/example/template/domain/board/repository/BoardRepository.java +++ b/src/main/java/com/example/template/domain/board/repository/BoardRepository.java @@ -14,15 +14,39 @@ @Repository public interface BoardRepository extends JpaRepository { - @Query("SELECT b FROM Board b WHERE b.id < :cursor ORDER BY b.likeNum DESC, b.id DESC") - List findAllOrderByLikesWithCursor(@Param("cursor") Long cursor, Pageable pageable); + // 좋아요 순 정렬 - 전체 게시물 (첫 페이지) + @Query("SELECT b FROM Board b ORDER BY b.likeNum DESC, b.id DESC") + List findAllOrderByLikesFirstPage(Pageable pageable); + + // 좋아요 순 정렬 - 전체 게시물 (이후 페이지) + @Query(value = "SELECT b1.* FROM board b1 " + + "JOIN (SELECT b2.board_id, CONCAT(LPAD(CAST(b2.like_num AS CHAR(10)), 10, '0'), LPAD(CAST(b2.board_id AS CHAR(10)), 10, '0')) as cursorValue " + + " FROM board b2) as cursorTable ON cursorTable.board_id = b1.board_id " + + "WHERE cursorValue < (SELECT CONCAT(LPAD(CAST(b3.like_num AS CHAR(10)), 10, '0'), LPAD(CAST(b3.board_id AS CHAR(10)), 10, '0')) " + + " FROM board b3 WHERE b3.board_id = :cursor) " + + "ORDER BY b1.like_num DESC, b1.board_id DESC " + + "LIMIT :limit", + nativeQuery = true) + List findAllOrderByLikesWithCursor(@Param("cursor") Long cursor, @Param("limit") int limit); + + // 좋아요 순 정렬 - 카테고리별 (첫 페이지) + @Query("SELECT b FROM Board b WHERE b.category = :category ORDER BY b.likeNum DESC, b.id DESC") + List findByCategoryOrderByLikesFirstPage(@Param("category") Category category, Pageable pageable); + + // 좋아요 순 정렬 - 카테고리별 (이후 페이지) + @Query(value = "SELECT b1.* FROM board b1 " + + "JOIN (SELECT b2.board_id, CONCAT(LPAD(CAST(b2.like_num AS CHAR(10)), 10, '0'), LPAD(CAST(b2.board_id AS CHAR(10)), 10, '0')) as cursorValue " + + " FROM board b2 ) as cursorTable ON cursorTable.board_id = b1.board_id " + + "WHERE cursorValue < (SELECT CONCAT(LPAD(CAST(b3.like_num AS CHAR(10)), 10, '0'), LPAD(CAST(b3.board_id AS CHAR(10)), 10, '0')) " + + " FROM board b3 WHERE b3.board_id = :cursor AND b3.category = :#{#category.name()}) " + + "ORDER BY b1.like_num DESC, b1.board_id DESC " + + "LIMIT :limit", + nativeQuery = true) + List findByCategoryOrderByLikesWithCursor(@Param("category") Category category, @Param("cursor") Long cursor, @Param("limit") int limit); @Query("SELECT b FROM Board b WHERE b.id < :cursor ORDER BY b.id DESC") List findAllOrderByLatestWithCursor(@Param("cursor") Long cursor, Pageable pageable); - @Query("SELECT b FROM Board b WHERE b.category = :category AND b.id < :cursor ORDER BY b.likeNum DESC, b.id DESC") - List findByCategoryOrderByLikesWithCursor(@Param("category") Category category, @Param("cursor") Long cursor, Pageable pageable); - @Query("SELECT b FROM Board b WHERE b.category = :category AND b.id < :cursor ORDER BY b.id DESC") List findByCategoryOrderByLatestWithCursor(@Param("category") Category category, @Param("cursor") Long cursor, Pageable pageable); diff --git a/src/main/java/com/example/template/domain/board/service/queryService/BoardQueryServiceImpl.java b/src/main/java/com/example/template/domain/board/service/queryService/BoardQueryServiceImpl.java index 9dca891..06882fd 100644 --- a/src/main/java/com/example/template/domain/board/service/queryService/BoardQueryServiceImpl.java +++ b/src/main/java/com/example/template/domain/board/service/queryService/BoardQueryServiceImpl.java @@ -11,6 +11,7 @@ import com.example.template.domain.member.entity.Member; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -37,6 +38,10 @@ public BoardResponseDTO.BoardListDTO getBoardList(Category category, return createBoardListDTO(boards, limit, member); } + private Long initializeCursor(Long cursor) { + return (cursor == null || cursor == 0) ? Long.MAX_VALUE : cursor; + } + @Override public BoardResponseDTO.BoardDTO getBoardDetail(Long boardId, Member member) { Board board = boardRepository.findById(boardId) @@ -67,23 +72,25 @@ public BoardResponseDTO.BoardListDTO getMyLikedPosts(Long cursor, Integer limit, return createBoardListDTO(boards, limit, member); } - private Long initializeCursor(Long cursor) { - return (cursor == 0) ? Long.MAX_VALUE : cursor; - } - private List fetchBoards(Category category, Long cursor, Integer limit, SortType sortType) { - PageRequest pageRequest = PageRequest.of(0, limit + 1); + Pageable pageable = PageRequest.of(0, limit + 1); // 커서 페이지네이션을 사용하는 대신 기본적으로 `limit + 1`로 설정 + if (category == null) { if (sortType == SortType.LIKES) { - return boardRepository.findAllOrderByLikesWithCursor(cursor, pageRequest); + return (cursor == Long.MAX_VALUE) + ? boardRepository.findAllOrderByLikesFirstPage(pageable) // 첫 페이지 조회 + : boardRepository.findAllOrderByLikesWithCursor(cursor, limit + 1); // 이후 페이지 조회 } else { - return boardRepository.findAllOrderByLatestWithCursor(cursor, pageRequest); + return boardRepository.findAllOrderByLatestWithCursor(cursor, pageable); } } else { if (sortType == SortType.LIKES) { - return boardRepository.findByCategoryOrderByLikesWithCursor(category, cursor, pageRequest); + return (cursor == Long.MAX_VALUE) + ? boardRepository.findByCategoryOrderByLikesFirstPage(category, pageable) // 카테고리별 첫 페이지 조회 + : boardRepository.findByCategoryOrderByLikesWithCursor(category, cursor, limit + 1); // 이후 페이지 조회 } else { - return boardRepository.findByCategoryOrderByLatestWithCursor(category, cursor, pageRequest); + // 카테고리별 최신순 첫 페이지 조회 + return boardRepository.findByCategoryOrderByLatestWithCursor(category, cursor, pageable); // 이후 페이지 조회 } } }