From d5d261a8988cee1250d26f794b8d1636199f5f22 Mon Sep 17 00:00:00 2001 From: Sin Ye Rin <91180366+nyeroni@users.noreply.github.com> Date: Mon, 7 Oct 2024 21:32:59 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20ranking=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/RankingApiController.java | 4 +- .../category/ranking/domain/Ranking.java | 5 +- .../category/ranking/dto/RankingResponse.java | 1 + .../ranking/service/RankingService.java | 63 +++++-------------- 4 files changed, 20 insertions(+), 53 deletions(-) diff --git a/src/main/java/yerong/wedle/category/ranking/controller/RankingApiController.java b/src/main/java/yerong/wedle/category/ranking/controller/RankingApiController.java index 6780df5..c058210 100644 --- a/src/main/java/yerong/wedle/category/ranking/controller/RankingApiController.java +++ b/src/main/java/yerong/wedle/category/ranking/controller/RankingApiController.java @@ -26,8 +26,8 @@ public class RankingApiController { summary = "대학교 랭킹 정보 조회", description = "대학교 ID를 이용해 해당 대학교의 랭킹 목록을 조회합니다." ) @GetMapping - public ResponseEntity> getRankingsByUniversityId() { - List rankings = rankingService.getRankings(); + public ResponseEntity> getRankings() { + List rankings = rankingService.getAllRankings(); return ResponseEntity.ok(rankings); } } diff --git a/src/main/java/yerong/wedle/category/ranking/domain/Ranking.java b/src/main/java/yerong/wedle/category/ranking/domain/Ranking.java index 6100f4a..7611e5f 100644 --- a/src/main/java/yerong/wedle/category/ranking/domain/Ranking.java +++ b/src/main/java/yerong/wedle/category/ranking/domain/Ranking.java @@ -14,6 +14,7 @@ @Getter @NoArgsConstructor(access = PROTECTED) @Entity +@Table(name = "ranking") public class Ranking { @Id @@ -26,8 +27,8 @@ public class Ranking { private int rankNum; - @Column(name = "year") - private int year; + @Column(name = "ranking_year") + private int rankingYear; private String universityName; private String logo; diff --git a/src/main/java/yerong/wedle/category/ranking/dto/RankingResponse.java b/src/main/java/yerong/wedle/category/ranking/dto/RankingResponse.java index c5dc80f..895f5ba 100644 --- a/src/main/java/yerong/wedle/category/ranking/dto/RankingResponse.java +++ b/src/main/java/yerong/wedle/category/ranking/dto/RankingResponse.java @@ -17,6 +17,7 @@ public class RankingResponse { private String displayName; private String fullName; private String description; + private int year; private String category; List universityRankingResponses; diff --git a/src/main/java/yerong/wedle/category/ranking/service/RankingService.java b/src/main/java/yerong/wedle/category/ranking/service/RankingService.java index fcc5956..4784cd3 100644 --- a/src/main/java/yerong/wedle/category/ranking/service/RankingService.java +++ b/src/main/java/yerong/wedle/category/ranking/service/RankingService.java @@ -4,13 +4,10 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import yerong.wedle.category.ranking.domain.Ranking; -import yerong.wedle.category.ranking.domain.RankingCategory; -import yerong.wedle.category.ranking.domain.RankingType; import yerong.wedle.category.ranking.dto.RankingResponse; import yerong.wedle.category.ranking.dto.UniversityRankingResponse; import yerong.wedle.category.ranking.repository.RankingRepository; -import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -20,62 +17,30 @@ public class RankingService { private final RankingRepository rankingRepository; - @Transactional - public List getRankings() { + @Transactional(readOnly = true) + public List getAllRankings() { List rankings = rankingRepository.findAll(); - List rankingResponses = new ArrayList<>(); - - List cwtsRankings = rankings.stream() - .filter(ranking -> ranking.getRankingType() == RankingType.CWTS) - .collect(Collectors.toList()); - - var cwtsGroupedByCategory = cwtsRankings.stream() - .collect(Collectors.groupingBy(Ranking::getRankingCategory)); - - rankingResponses.addAll(rankings.stream() - .filter(ranking -> ranking.getRankingType() != RankingType.CWTS) // CWTS 제외 + return rankings.stream() .map(this::convertToRankingResponse) - .collect(Collectors.toList())); - - for (RankingCategory category : RankingCategory.values()) { - List universityRankingResponses = cwtsGroupedByCategory.getOrDefault(category, List.of()).stream() - .map(this::convertToUniversityRankingResponse) - .collect(Collectors.toList()); - - if (!universityRankingResponses.isEmpty()) { - rankingResponses.add(new RankingResponse( - RankingType.CWTS.getDisplayName(), - RankingType.CWTS.getFullName(), - RankingType.CWTS.getDescription(), - category.getValue(), - universityRankingResponses - )); - } - } - - return rankingResponses; + .collect(Collectors.toList()); } private RankingResponse convertToRankingResponse(Ranking ranking) { + // 대학 랭킹 응답 생성 + UniversityRankingResponse universityRankingResponse = new UniversityRankingResponse( + ranking.getUniversityName(), + ranking.getLogo(), + ranking.getRankNum() + ); + return new RankingResponse( ranking.getRankingType().getDisplayName(), ranking.getRankingType().getFullName(), ranking.getRankingType().getDescription(), - null, - List.of(new UniversityRankingResponse( - ranking.getUniversityName(), - ranking.getLogo(), - ranking.getRankNum() - )) - ); - } - - private UniversityRankingResponse convertToUniversityRankingResponse(Ranking ranking) { - return new UniversityRankingResponse( - ranking.getUniversityName(), - ranking.getLogo(), - ranking.getRankNum() + ranking.getRankingYear(), + ranking.getRankingCategory() != null ? ranking.getRankingCategory().name() : null, + List.of(universityRankingResponse) ); } }