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
11 changes: 10 additions & 1 deletion src/main/java/balancetalk/game/application/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,21 @@ 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);
}

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 메서드 하나로 코드를 통합할 수 있지 않을까요??

}
}