diff --git a/src/main/java/com/umc/StudyFlexBE/controller/StudyController.java b/src/main/java/com/umc/StudyFlexBE/controller/StudyController.java index 97ca796..ceb1ce0 100644 --- a/src/main/java/com/umc/StudyFlexBE/controller/StudyController.java +++ b/src/main/java/com/umc/StudyFlexBE/controller/StudyController.java @@ -88,10 +88,11 @@ public BaseResponse getOpenStudies() { } @GetMapping("/ranking") public BaseResponse getStudyRanking() { - try{ - List rankedStudies = studyService.getRankedStudies(); + try { + List rankedStudies = studyService.getRankedStudies(); return new BaseResponse<>(BaseResponseStatus.SUCCESS, rankedStudies); - }catch (BaseException e){ + } catch (BaseException e) { + return new BaseResponse<>(e.getStatus()); } } diff --git a/src/main/java/com/umc/StudyFlexBE/dto/response/RankResponseDto.java b/src/main/java/com/umc/StudyFlexBE/dto/response/RankResponseDto.java new file mode 100644 index 0000000..bf9f90e --- /dev/null +++ b/src/main/java/com/umc/StudyFlexBE/dto/response/RankResponseDto.java @@ -0,0 +1,16 @@ +package com.umc.StudyFlexBE.dto.response; + +import lombok.*; + +@Setter +@Getter +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class RankResponseDto { + private int rank; + private String teamName; + private double progressRate; + private int members; + private String category; +} diff --git a/src/main/java/com/umc/StudyFlexBE/service/StudyService.java b/src/main/java/com/umc/StudyFlexBE/service/StudyService.java index 5beed1b..5d99de4 100644 --- a/src/main/java/com/umc/StudyFlexBE/service/StudyService.java +++ b/src/main/java/com/umc/StudyFlexBE/service/StudyService.java @@ -16,6 +16,8 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; +import java.util.concurrent.atomic.AtomicInteger; + @Service @Slf4j @@ -199,23 +201,28 @@ public StudyRes createStudy(StudyReq studyReq, String email){ } - public List getRankedStudies() { + public List getRankedStudies() { List studies = studyRepository.findAll(); studies.forEach(this::calculateRankScore); + + AtomicInteger rankCounter = new AtomicInteger(1); + return studies.stream() .sorted((s1, s2) -> Double.compare(s2.getRankScore(), s1.getRankScore())) .limit(3) - .map(study -> StudyMainPageResponseDto.builder() - .studyId(study.getId().intValue()) // Long을 int로 변환 (필요에 따라 조정) - .studyName(study.getName()) - .thumbnailUrl(study.getThumbnailUrl()) - .studyStatus(study.getStatus().toString()) - .maxMembers(study.getMaxMembers()) - .currentMembers(study.getCurrentMembers()) - .build()) + .map(study -> { + RankResponseDto dto = new RankResponseDto(); + dto.setRank(rankCounter.getAndIncrement()); + dto.setTeamName(study.getName()); + dto.setProgressRate(study.getTotalProgressRate()); + dto.setMembers(study.getCurrentMembers()); + dto.setCategory(study.getCategory().getName()); + return dto; + }) .collect(Collectors.toList()); } + private void calculateRankScore(Study study) { int noticeCount = studyNoticeRepository.countByStudy(study); double rankScore = noticeCount