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
6 changes: 6 additions & 0 deletions src/main/java/balancetalk/game/application/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ public List<GameSetResponse> findBestGames(final String tagName, final Pageable
return gameSetResponses(guestOrApiMember, gameSets);
}

@Transactional(readOnly = true)
public List<GameSetResponse> findPopularGames(final Pageable pageable, final GuestOrApiMember guestOrApiMember) {
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 @@ -70,7 +70,7 @@ public List<GameSetResponse> findLatestGames(
}

@GetMapping("/best")
@Operation(summary = "조회수 순으로 밸런스 게임 조회", description = "조회수 순으로 정렬된 16개의 게임 목록을 리턴합니다.")
@Operation(summary = "메인 태그에 해당하는 조회수 순으로 밸런스 게임 조회", description = "조회수 순으로 정렬된 16개의 게임 목록을 리턴합니다.")
public List<GameSetResponse> findBestGames(
@RequestParam String tagName,
@RequestParam(defaultValue = "0") int page,
Expand All @@ -80,4 +80,15 @@ public List<GameSetResponse> findBestGames(
Pageable pageable = PageRequest.of(page, size);
return gameService.findBestGames(tagName, pageable, guestOrApiMember);
}

@GetMapping("/popular-games")
Copy link
Member

Choose a reason for hiding this comment

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

처음 API 명세서를 보는 사람 입장에서, "/best"와 "/popular-games" 이 두 API가 어떤 차이를 보이는지 알기 어려울 것 같아요.

아래처럼 하나의 API 엔드포인트에서 쿼리 파라미터를 통해 두 가지 조회 방식을 처리하는 것은 어떨까요?

  • 메인 태그에 해당하는 게임 세트 목록 인기순 조회 API: "/game-sets/popular?mainTag={mainTag}"
  • 모든 게임 세트 인기순 조회 API: "/game-sets/popular"
    이렇게 하면 코드의 간결함과 유연함이 향상될 것 같아요.

만약 2개의 API로 분리하고 싶다면, 둘을 명확하게 구분할 수 있도록 URI라도 개선하는게 좋아보여요.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

very good idea.

@Operation(summary = "조회수 순으로 밸런스 게임 조회", description = "태그 관계없이 조회수 순으로 정렬된 16개의 게임 목록을 리턴합니다.")
public List<GameSetResponse> findPopularGames(
@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.findPopularGames(pageable, guestOrApiMember);
}
}