Skip to content

Commit

Permalink
Merge pull request #130 from UnivApp/develop
Browse files Browse the repository at this point in the history
feat: ranking구현
  • Loading branch information
nyeroni authored Oct 7, 2024
2 parents 41c6e3e + 4a29b3c commit ddd86ea
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class RankingApiController {
summary = "대학교 랭킹 정보 조회", description = "대학교 ID를 이용해 해당 대학교의 랭킹 목록을 조회합니다."
)
@GetMapping
public ResponseEntity<List<RankingResponse>> getRankingsByUniversityId() {
List<RankingResponse> rankings = rankingService.getRankings();
public ResponseEntity<List<RankingResponse>> getRankings() {
List<RankingResponse> rankings = rankingService.getAllRankings();
return ResponseEntity.ok(rankings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@Getter
@NoArgsConstructor(access = PROTECTED)
@Entity
@Table(name = "ranking")
public class Ranking {

@Id
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class RankingResponse {
private String displayName;
private String fullName;
private String description;
private int year;
private String category;
List<UniversityRankingResponse> universityRankingResponses;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,62 +17,30 @@ public class RankingService {

private final RankingRepository rankingRepository;

@Transactional
public List<RankingResponse> getRankings() {
@Transactional(readOnly = true)
public List<RankingResponse> getAllRankings() {
List<Ranking> rankings = rankingRepository.findAll();

List<RankingResponse> rankingResponses = new ArrayList<>();

List<Ranking> 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<UniversityRankingResponse> 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(),
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(),
List.of(new UniversityRankingResponse(
ranking.getUniversityName(),
ranking.getLogo(),
ranking.getRankingCategory().getValue(),
ranking.getRankNum()
))
);
}

private UniversityRankingResponse convertToUniversityRankingResponse(Ranking ranking) {
return new UniversityRankingResponse(
ranking.getUniversityName(),
ranking.getLogo(),
ranking.getRankingCategory().getValue(),
ranking.getRankNum()
ranking.getRankingYear(),
ranking.getRankingCategory() != null ? ranking.getRankingCategory().name() : null,
List.of(universityRankingResponse)
);
}
}

0 comments on commit ddd86ea

Please sign in to comment.