From c5e4d55099a65f7abff0109723c4b209637cab54 Mon Sep 17 00:00:00 2001 From: Sin Ye Rin <91180366+nyeroni@users.noreply.github.com> Date: Mon, 14 Oct 2024 15:59:46 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EA=B2=BD=EC=9F=81=EB=A5=A0=20=EB=B0=98?= =?UTF-8?q?=ED=99=98=20=ED=98=95=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...java => CompetitionRateApiController.java} | 14 ++++++-- .../service/CompetitionRateService.java | 34 ++++++++++++++++--- 2 files changed, 41 insertions(+), 7 deletions(-) rename src/main/java/yerong/wedle/competitionRate/controller/{CompetitionRateController.java => CompetitionRateApiController.java} (76%) diff --git a/src/main/java/yerong/wedle/competitionRate/controller/CompetitionRateController.java b/src/main/java/yerong/wedle/competitionRate/controller/CompetitionRateApiController.java similarity index 76% rename from src/main/java/yerong/wedle/competitionRate/controller/CompetitionRateController.java rename to src/main/java/yerong/wedle/competitionRate/controller/CompetitionRateApiController.java index bf79db3..01edcc1 100644 --- a/src/main/java/yerong/wedle/competitionRate/controller/CompetitionRateController.java +++ b/src/main/java/yerong/wedle/competitionRate/controller/CompetitionRateApiController.java @@ -17,14 +17,14 @@ @RequiredArgsConstructor @RestController @RequestMapping("/api/competition-rate") -public class CompetitionRateController { +public class CompetitionRateApiController { private final CompetitionRateService competitionRateService; @Operation( summary = "대학교 경쟁률 조회", description = "대학교 ID를 이용해 해당 대학교의 경쟁률 삼년치를 조회합니다." ) @GetMapping() - public ResponseEntity getAllCompetitionRates(@RequestParam Long universityId) { + public ResponseEntity getAllCompetitionRatesByUniversity(@RequestParam Long universityId) { UniversityCompetitionRateResponse universityCompetitionRates = competitionRateService.getUniversityCompetitionRates(universityId); return ResponseEntity.ok(universityCompetitionRates); } @@ -38,4 +38,14 @@ public ResponseEntity> getTop5Universiti List top5Rates = competitionRateService.getTop5UniversitiesCompetitionRates(); return ResponseEntity.ok(top5Rates); } + + @Operation( + summary = "모든 대학교 경쟁률 조회", + description = "모든 대학교의 경쟁률을 조회합니다." + ) + @GetMapping("/all") + public ResponseEntity> getAllUniversitiesCompetitionRates() { + List rates = competitionRateService.getAllUniversitiesCompetitionRates(); + return ResponseEntity.ok(rates); + } } diff --git a/src/main/java/yerong/wedle/competitionRate/service/CompetitionRateService.java b/src/main/java/yerong/wedle/competitionRate/service/CompetitionRateService.java index 433af55..a8d9731 100644 --- a/src/main/java/yerong/wedle/competitionRate/service/CompetitionRateService.java +++ b/src/main/java/yerong/wedle/competitionRate/service/CompetitionRateService.java @@ -13,6 +13,7 @@ import yerong.wedle.university.repository.UniversityRepository; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; @Service @@ -45,11 +46,34 @@ public UniversityCompetitionRateResponse getUniversityCompetitionRates(Long univ public List getTop5UniversitiesCompetitionRates() { List top5Rates = competitionRateRepository.findTop5ByOrderByUniversityNameAsc(); - return top5Rates.stream() - .map(rate -> new UniversityCompetitionRateResponse( - rate.getUniversity().getName(), - rate.getUniversity().getLogo(), - List.of(convertToDto(rate)) + Map> universityRatesMap = top5Rates.stream() + .collect(Collectors.groupingBy( + CompetitionRate::getUniversity, + Collectors.mapping(this::convertToDto, Collectors.toList()) + )); + return universityRatesMap.entrySet().stream() + .map(entry -> new UniversityCompetitionRateResponse( + entry.getKey().getName(), + entry.getKey().getLogo(), + entry.getValue() + )) + .collect(Collectors.toList()); + } + + @Transactional(readOnly = true) + public List getAllUniversitiesCompetitionRates() { + + List rates = competitionRateRepository.findAll(); + Map> universityRatesMap = rates.stream() + .collect(Collectors.groupingBy( + CompetitionRate::getUniversity, + Collectors.mapping(this::convertToDto, Collectors.toList()) + )); + return universityRatesMap.entrySet().stream() + .map(entry -> new UniversityCompetitionRateResponse( + entry.getKey().getName(), + entry.getKey().getLogo(), + entry.getValue() )) .collect(Collectors.toList()); }