-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 미션 최종 순위 조회 API 구현 * feat: 동일한 인증 횟수인 경우 동일 순위로 지정
- Loading branch information
Showing
8 changed files
with
184 additions
and
4 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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/nexters/goalpanzi/application/mission/dto/response/MemberRankResponse.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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.nexters.goalpanzi.application.mission.dto.response; | ||
|
||
import com.nexters.goalpanzi.domain.mission.MemberRank; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
||
public record MemberRankResponse( | ||
@Schema(description = "미션 최종 순위") | ||
Integer rank | ||
) { | ||
|
||
public static MemberRankResponse from(final MemberRank memberRank) { | ||
return new MemberRankResponse( | ||
memberRank.rank() | ||
); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/nexters/goalpanzi/domain/mission/MemberRank.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.nexters.goalpanzi.domain.mission; | ||
|
||
import com.nexters.goalpanzi.domain.member.Member; | ||
|
||
public record MemberRank( | ||
Member member, | ||
Integer rank | ||
) { | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/com/nexters/goalpanzi/domain/mission/MemberRanks.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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.nexters.goalpanzi.domain.mission; | ||
|
||
import com.nexters.goalpanzi.domain.member.Member; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.Objects; | ||
|
||
@RequiredArgsConstructor | ||
public class MemberRanks { | ||
|
||
private final List<MemberRank> memberRanks; | ||
|
||
public static MemberRanks from(final List<MissionMember> missionMembers) { | ||
List<MissionMember> sortedMissionMembers = sortedMembersByVerificationCountDesc(missionMembers); | ||
|
||
List<MemberRank> memberRanks = new ArrayList<>(); | ||
int rank = 1; | ||
int previousVerificationCount = sortedMissionMembers.getFirst().getVerificationCount(); | ||
|
||
for (int i = 0; i < sortedMissionMembers.size(); i++) { | ||
MissionMember missionMember = sortedMissionMembers.get(i); | ||
if (missionMember.getVerificationCount() < previousVerificationCount) { | ||
rank = i + 1; | ||
} | ||
memberRanks.add(new MemberRank(missionMember.getMember(), rank)); | ||
previousVerificationCount = missionMember.getVerificationCount(); | ||
} | ||
|
||
return new MemberRanks(memberRanks); | ||
} | ||
|
||
private static List<MissionMember> sortedMembersByVerificationCountDesc(final List<MissionMember> missionMembers) { | ||
return missionMembers | ||
.stream() | ||
.sorted((m1, m2) -> m2.getVerificationCount() - m1.getVerificationCount()) | ||
.toList(); | ||
} | ||
|
||
public MemberRank getRankByMember(final Member member) { | ||
return memberRanks.stream() | ||
.filter(memberRank -> memberRank.member().equals(member)) | ||
.findFirst() | ||
.orElseThrow(() -> new NoSuchElementException("No rank found for member " + member.getId())); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
MemberRanks memberRanks1 = (MemberRanks) o; | ||
return Objects.equals(memberRanks, memberRanks1.memberRanks); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(memberRanks); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Ranks{" + | ||
"ranks=" + memberRanks + | ||
'}'; | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
src/test/java/com/nexters/goalpanzi/domain/mission/MemberRanksTest.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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.nexters.goalpanzi.domain.mission; | ||
|
||
import com.nexters.goalpanzi.domain.member.Member; | ||
import com.nexters.goalpanzi.domain.member.SocialType; | ||
import com.nexters.goalpanzi.fixture.MissionFixture; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static com.nexters.goalpanzi.fixture.MemberFixture.*; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
||
class MemberRanksTest { | ||
|
||
@Test | ||
void 미션_최종_등수를_확인할_수_있다() { | ||
// given | ||
Member memberHost = Member.socialLogin(SOCIAL_ID, EMAIL_HOST, SocialType.APPLE); | ||
Member memberA = Member.socialLogin(SOCIAL_ID, EMAIL_MEMBER_A, SocialType.APPLE); | ||
Member memberB = Member.socialLogin(SOCIAL_ID, EMAIL_MEMBER_B, SocialType.GOOGLE); | ||
|
||
List<MissionMember> missionMembers = List.of( | ||
new MissionMember(memberHost, MissionFixture.create(), 10), | ||
new MissionMember(memberA, MissionFixture.create(), 12), | ||
new MissionMember(memberB, MissionFixture.create(), 14) | ||
); | ||
|
||
// when, then | ||
MemberRanks actual = MemberRanks.from(missionMembers); | ||
assertAll( | ||
() -> assertThat(actual.getRankByMember(memberB).rank()).isEqualTo(1), | ||
() -> assertThat(actual.getRankByMember(memberA).rank()).isEqualTo(2), | ||
() -> assertThat(actual.getRankByMember(memberHost).rank()).isEqualTo(3) | ||
); | ||
} | ||
} |
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