-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from giwoong01/feature/ranking-refactor
Develop
- Loading branch information
Showing
10 changed files
with
238 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 77 additions & 3 deletions
80
src/main/java/com/itcontest/skhuming/main/application/MainService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,107 @@ | ||
package com.itcontest.skhuming.main.application; | ||
|
||
import com.itcontest.skhuming.global.util.ChangeDepartmentUtil; | ||
import com.itcontest.skhuming.member.api.dto.response.DepartmentRankResDto; | ||
import com.itcontest.skhuming.member.api.dto.response.MemberRankResDto; | ||
import com.itcontest.skhuming.member.domain.Member; | ||
import com.itcontest.skhuming.member.domain.repository.MemberRepository; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
@Service | ||
@Transactional | ||
public class MainService { | ||
private final MemberRepository memberRepository; | ||
|
||
public MainService(MemberRepository memberRepository) { | ||
this.memberRepository = memberRepository; | ||
} | ||
|
||
public List<MemberRankResDto> mainPageRanking() { | ||
List<Member> memberList = memberRepository.findAll(Sort.by(Sort.Direction.DESC, "score")); | ||
/** | ||
* 메인페이지 전체 중 3명 랭킹 | ||
*/ | ||
public List<MemberRankResDto> mainAllRanking() { | ||
List<Member> memberList = memberRepository.findAll( | ||
Sort.by(Sort.Direction.DESC, "score")); | ||
|
||
List<MemberRankResDto> memberRankingList = new ArrayList<>(); | ||
for (int i = 0; i < Math.min(3, memberList.size()); i++) { | ||
Member member = memberList.get(i); | ||
MemberRankResDto memberRankResDto = new MemberRankResDto(member.getMemberId(), member.getTier(), member.getScore(), member.getNickname(), member.getDepartment(), 0); | ||
MemberRankResDto memberRankResDto = new MemberRankResDto( | ||
member.getMemberId(), | ||
member.getTier(), | ||
member.getScore(), | ||
member.getNickname(), | ||
member.getDepartment(), | ||
0); | ||
memberRankingList.add(memberRankResDto); | ||
} | ||
|
||
return memberRankingList; | ||
} | ||
|
||
/** | ||
* 메인페이지 학부별 3명 랭킹 | ||
*/ | ||
public List<MemberRankResDto> mainDepartmentRanking(int departmentNumber) { | ||
List<Member> memberList = memberRepository.findByDepartment( | ||
ChangeDepartmentUtil.departmentNumber(departmentNumber), Sort.by(Sort.Direction.DESC, "score")); | ||
|
||
List<MemberRankResDto> memberRankingList = new ArrayList<>(); | ||
for (int i = 0; i < Math.min(3, memberList.size()); i++) { | ||
Member member = memberList.get(i); | ||
MemberRankResDto memberRankResDto = new MemberRankResDto( | ||
member.getMemberId(), | ||
member.getTier(), | ||
member.getScore(), | ||
member.getNickname(), | ||
member.getDepartment(), | ||
0); | ||
|
||
memberRankingList.add(memberRankResDto); | ||
} | ||
|
||
return memberRankingList; | ||
} | ||
|
||
/** | ||
* 학부별 총합 점수 랭킹 | ||
*/ | ||
public List<DepartmentRankResDto> departmentByRanking() { | ||
HashMap<Integer, Integer> scoreMap = new HashMap<>(); | ||
for (int i = 1; i < 5; i++) { | ||
int sum = 0; | ||
List<Member> memberList = memberRepository.findByDepartment( | ||
ChangeDepartmentUtil.departmentNumber(i), Sort.by(Sort.Direction.DESC, "score")); | ||
for (Member member : memberList) { | ||
sum += member.getScore(); | ||
} | ||
|
||
scoreMap.put(i, sum); | ||
} | ||
|
||
List<Integer> scoreList = new ArrayList<>(scoreMap.keySet()); | ||
scoreList.sort((o1, o2) -> scoreMap.get(o2).compareTo(scoreMap.get(o1))); | ||
|
||
Long idx = 1L; | ||
List<DepartmentRankResDto> departmentRankList = new ArrayList<>(); | ||
for (Integer i : scoreList) { | ||
DepartmentRankResDto departmentRankResDto = new DepartmentRankResDto( | ||
idx, | ||
ChangeDepartmentUtil.departmentNumber(i), | ||
scoreMap.get(i) | ||
); | ||
|
||
idx++; | ||
departmentRankList.add(departmentRankResDto); | ||
} | ||
|
||
return departmentRankList; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.