Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

인기순 게시글 조회 API 구현 #762

Merged
merged 12 commits into from
Nov 30, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,18 @@ public List<GameSetResponse> findLatestGames(final String tagName, final Pageabl
}

@Transactional(readOnly = true)
public List<GameSetResponse> findBestGames(final String tagName, final Pageable pageable,
public List<GameSetResponse> findPopularGamesWithTag(final String tagName, final Pageable pageable,
final GuestOrApiMember guestOrApiMember) {
List<GameSet> gameSets = gameSetRepository.findGamesByViews(tagName, pageable);
return gameSetResponses(guestOrApiMember, gameSets);
}

@Transactional(readOnly = true)
public List<GameSetResponse> findPopularGamesWithoutTag(final Pageable pageable, final GuestOrApiMember guestOrApiMember) {
List<GameSet> popularGames = gameSetRepository.findPopularGames(pageable);
return gameSetResponses(guestOrApiMember, popularGames);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

페이지네이션 제한 및 문서화가 필요합니다

메서드 구현은 적절하나, PR 요구사항에 맞춰 다음 개선사항들을 제안드립니다:

  1. PR 요구사항에 명시된 "상위 16개 게시물" 제한을 보장하기 위한 검증 로직 추가
  2. 메서드 동작 방식에 대한 문서화
+/**
+ * 전체 게시물 중 인기 게시물을 조회수 기준으로 조회합니다.
+ * 최대 16개의 게시물을 반환합니다.
+ *
+ * @param pageable 페이지 정보 (size는 16 이하여야 함)
+ * @param guestOrApiMember 요청한 사용자 정보
+ * @return 조회수 내림차순으로 정렬된 게시물 목록
+ * @throws IllegalArgumentException 페이지 크기가 16을 초과하는 경우
+ */
 @Transactional(readOnly = true)
 public List<GameSetResponse> findPopularGamesWithoutTag(final Pageable pageable,
         final GuestOrApiMember guestOrApiMember) {
+    if (pageable.getPageSize() > 16) {
+        throw new IllegalArgumentException("페이지 크기는 16을 초과할 수 없습니다.");
+    }
     List<GameSet> popularGames = gameSetRepository.findPopularGames(pageable);
     return gameSetResponses(guestOrApiMember, popularGames);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@Transactional(readOnly = true)
public List<GameSetResponse> findPopularGamesWithoutTag(final Pageable pageable, final GuestOrApiMember guestOrApiMember) {
List<GameSet> popularGames = gameSetRepository.findPopularGames(pageable);
return gameSetResponses(guestOrApiMember, popularGames);
}
/**
* 전체 게시물 인기 게시물을 조회수 기준으로 조회합니다.
* 최대 16개의 게시물을 반환합니다.
*
* @param pageable 페이지 정보 (size는 16 이하여야 )
* @param guestOrApiMember 요청한 사용자 정보
* @return 조회수 내림차순으로 정렬된 게시물 목록
* @throws IllegalArgumentException 페이지 크기가 16 초과하는 경우
*/
@Transactional(readOnly = true)
public List<GameSetResponse> findPopularGamesWithoutTag(final Pageable pageable, final GuestOrApiMember guestOrApiMember) {
if (pageable.getPageSize() > 16) {
throw new IllegalArgumentException("페이지 크기는 16을 초과할 수 없습니다.");
}
List<GameSet> popularGames = gameSetRepository.findPopularGames(pageable);
return gameSetResponses(guestOrApiMember, popularGames);
}


private List<GameSetResponse> gameSetResponses(GuestOrApiMember guestOrApiMember, List<GameSet> gameSets) {
if (guestOrApiMember.isGuest()) {
return gameSets.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public interface GameSetRepository extends JpaRepository<GameSet, Long> {
"WHERE g.mainTag.name = :name " +
"ORDER BY g.views DESC, g.createdAt DESC")
List<GameSet> findGamesByViews(@Param("name") String mainTag, Pageable pageable);

@Query("SELECT g FROM GameSet g "
+ "ORDER BY g.views DESC, "
+ "g.createdAt DESC")
List<GameSet> findPopularGames(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,19 @@ public List<GameSetResponse> findLatestGames(
return gameService.findLatestGames(tagName, pageable, guestOrApiMember);
}

@GetMapping("/best")
@Operation(summary = "조회수 순으로 밸런스 게임 조회", description = "조회수 순으로 정렬된 16개의 게임 목록을 리턴합니다.")
public List<GameSetResponse> findBestGames(
@RequestParam String tagName,
@GetMapping("/popular")
@Operation(summary = "인기순으로 밸런스 게임 조회",
description = "메인 태그가 주어지면 해당 태그의 게임을 인기순으로 조회, 없으면 밸런스 게임 전체를 인기순으로 조회합니다.")
public List<GameSetResponse> findPopularGamesWithTag(
@RequestParam(required = false) String tagName,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "16") int size,
@Parameter(hidden = true) @AuthPrincipal final GuestOrApiMember guestOrApiMember
) {
Pageable pageable = PageRequest.of(page, size);
return gameService.findBestGames(tagName, pageable, guestOrApiMember);
if (tagName != null) {
return gameService.findPopularGamesWithTag(tagName, pageable, guestOrApiMember);
}
return gameService.findPopularGamesWithoutTag(pageable, guestOrApiMember);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

메인태그 유무에 따른 분기처리를 서비스 계층에서 수행하면 findPopularGames 메서드 하나로 코드를 통합할 수 있지 않을까요??

}
}