Skip to content

Commit

Permalink
๐Ÿ› fix: ์ข‹์•„์š”์ˆœ ์ •๋ ฌ ์ปค๋ฎค๋‹ˆํ‹ฐ ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก GET API ์ˆ˜์ • (#84)
Browse files Browse the repository at this point in the history
๐Ÿ› fix: ์ข‹์•„์š”์ˆœ ์ •๋ ฌ ์ปค๋ฎค๋‹ˆํ‹ฐ ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก GET API ์ˆ˜์ • (#84)
  • Loading branch information
jinho7 authored Aug 23, 2024
2 parents 1ddd30f + 3b98404 commit 58071df
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,39 @@

@Repository
public interface BoardRepository extends JpaRepository<Board, Long> {
@Query("SELECT b FROM Board b WHERE b.id < :cursor ORDER BY b.likeNum DESC, b.id DESC")
List<Board> findAllOrderByLikesWithCursor(@Param("cursor") Long cursor, Pageable pageable);
// ์ข‹์•„์š” ์ˆœ ์ •๋ ฌ - ์ „์ฒด ๊ฒŒ์‹œ๋ฌผ (์ฒซ ํŽ˜์ด์ง€)
@Query("SELECT b FROM Board b ORDER BY b.likeNum DESC, b.id DESC")
List<Board> 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<Board> 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<Board> 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<Board> 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<Board> 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<Board> 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<Board> findByCategoryOrderByLatestWithCursor(@Param("category") Category category, @Param("cursor") Long cursor, Pageable pageable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand Down Expand Up @@ -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<Board> 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); // ์ดํ›„ ํŽ˜์ด์ง€ ์กฐํšŒ
}
}
}
Expand Down

0 comments on commit 58071df

Please sign in to comment.