Skip to content

Commit

Permalink
Merge pull request #48 from StudyFlexUMC5th/yeeun2
Browse files Browse the repository at this point in the history
Fix: ranking dto
  • Loading branch information
Yeeun411 authored Jan 14, 2024
2 parents f4257ff + 91541b9 commit d5596ff
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ public BaseResponse<?> getOpenStudies() {
}
@GetMapping("/ranking")
public BaseResponse<?> getStudyRanking() {
try{
List<StudyMainPageResponseDto> rankedStudies = studyService.getRankedStudies();
try {
List<RankResponseDto> rankedStudies = studyService.getRankedStudies();
return new BaseResponse<>(BaseResponseStatus.SUCCESS, rankedStudies);
}catch (BaseException e){
} catch (BaseException e) {

return new BaseResponse<>(e.getStatus());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
25 changes: 16 additions & 9 deletions src/main/java/com/umc/StudyFlexBE/service/StudyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -199,23 +201,28 @@ public StudyRes createStudy(StudyReq studyReq, String email){
}


public List<StudyMainPageResponseDto> getRankedStudies() {
public List<RankResponseDto> getRankedStudies() {
List<Study> 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
Expand Down

0 comments on commit d5596ff

Please sign in to comment.