-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into fix/#191-routine-image-upload
- Loading branch information
Showing
28 changed files
with
710 additions
and
38 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
43 changes: 43 additions & 0 deletions
43
src/main/java/com/moabam/api/application/ranking/RankingMapper.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,43 @@ | ||
package com.moabam.api.application.ranking; | ||
|
||
import java.util.List; | ||
|
||
import com.moabam.api.dto.ranking.RankingInfo; | ||
import com.moabam.api.dto.ranking.TopRankingInfo; | ||
import com.moabam.api.dto.ranking.TopRankingResponse; | ||
import com.moabam.api.dto.ranking.UpdateRanking; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class RankingMapper { | ||
|
||
public static TopRankingInfo topRankingResponse(int rank, long score, RankingInfo rankInfo) { | ||
return TopRankingInfo.builder() | ||
.rank(rank) | ||
.score(score) | ||
.nickname(rankInfo.nickname()) | ||
.image(rankInfo.image()) | ||
.memberId(rankInfo.memberId()) | ||
.build(); | ||
} | ||
|
||
public static TopRankingInfo topRankingResponse(int rank, UpdateRanking updateRanking) { | ||
return TopRankingInfo.builder() | ||
.rank(rank) | ||
.score(updateRanking.score()) | ||
.nickname(updateRanking.rankingInfo().nickname()) | ||
.image(updateRanking.rankingInfo().image()) | ||
.memberId(updateRanking.rankingInfo().memberId()) | ||
.build(); | ||
} | ||
|
||
public static TopRankingResponse topRankingResponses(TopRankingInfo myRanking, | ||
List<TopRankingInfo> topRankings) { | ||
return TopRankingResponse.builder() | ||
.topRankings(topRankings) | ||
.myRanking(myRanking) | ||
.build(); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/com/moabam/api/application/ranking/RankingService.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,76 @@ | ||
package com.moabam.api.application.ranking; | ||
|
||
import static java.util.Objects.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.springframework.data.redis.core.ZSetOperations; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.moabam.api.dto.ranking.RankingInfo; | ||
import com.moabam.api.dto.ranking.TopRankingInfo; | ||
import com.moabam.api.dto.ranking.TopRankingResponse; | ||
import com.moabam.api.dto.ranking.UpdateRanking; | ||
import com.moabam.api.infrastructure.redis.ZSetRedisRepository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class RankingService { | ||
|
||
private static final String RANKING = "Ranking"; | ||
private static final int START_INDEX = 0; | ||
private static final int LIMIT_INDEX = 9; | ||
|
||
private final ObjectMapper objectMapper; | ||
private final ZSetRedisRepository zSetRedisRepository; | ||
|
||
public void addRanking(RankingInfo rankingInfo, Long totalCertifyCount) { | ||
zSetRedisRepository.add(RANKING, rankingInfo, totalCertifyCount); | ||
} | ||
|
||
public void updateScores(List<UpdateRanking> updateRankings) { | ||
updateRankings.forEach(updateRanking -> | ||
zSetRedisRepository.add(RANKING, updateRanking.rankingInfo(), updateRanking.score())); | ||
} | ||
|
||
public void changeInfos(RankingInfo before, RankingInfo after) { | ||
zSetRedisRepository.changeMember(RANKING, before, after); | ||
} | ||
|
||
public void removeRanking(RankingInfo rankingInfo) { | ||
zSetRedisRepository.delete(RANKING, rankingInfo); | ||
} | ||
|
||
public TopRankingResponse getMemberRanking(UpdateRanking myRankingInfo) { | ||
List<TopRankingInfo> topRankings = getTopRankings(); | ||
Long myRanking = zSetRedisRepository.reverseRank(RANKING, myRankingInfo.rankingInfo()); | ||
TopRankingInfo myRankingInfoResponse = | ||
RankingMapper.topRankingResponse(myRanking.intValue(), myRankingInfo); | ||
|
||
return RankingMapper.topRankingResponses(myRankingInfoResponse, topRankings); | ||
} | ||
|
||
private List<TopRankingInfo> getTopRankings() { | ||
Set<ZSetOperations.TypedTuple<Object>> topRankings = | ||
zSetRedisRepository.rangeJson(RANKING, START_INDEX, LIMIT_INDEX); | ||
|
||
Set<Long> scoreSet = new HashSet<>(); | ||
List<TopRankingInfo> topRankingInfo = new ArrayList<>(); | ||
|
||
for (ZSetOperations.TypedTuple<Object> topRanking : topRankings) { | ||
long score = requireNonNull(topRanking.getScore()).longValue(); | ||
scoreSet.add(score); | ||
|
||
RankingInfo rankingInfo = objectMapper.convertValue(topRanking.getValue(), RankingInfo.class); | ||
topRankingInfo.add(RankingMapper.topRankingResponse(scoreSet.size(), score, rankingInfo)); | ||
} | ||
|
||
return topRankingInfo; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.moabam.api.dto.ranking; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record RankingInfo( | ||
Long memberId, | ||
String nickname, | ||
String image | ||
) { | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/moabam/api/dto/ranking/TopRankingInfo.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,14 @@ | ||
package com.moabam.api.dto.ranking; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record TopRankingInfo( | ||
int rank, | ||
Long memberId, | ||
Long score, | ||
String nickname, | ||
String image | ||
) { | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/moabam/api/dto/ranking/TopRankingResponse.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,13 @@ | ||
package com.moabam.api.dto.ranking; | ||
|
||
import java.util.List; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record TopRankingResponse( | ||
List<TopRankingInfo> topRankings, | ||
TopRankingInfo myRanking | ||
) { | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/moabam/api/dto/ranking/UpdateRanking.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,11 @@ | ||
package com.moabam.api.dto.ranking; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record UpdateRanking( | ||
RankingInfo rankingInfo, | ||
Long score | ||
) { | ||
|
||
} |
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.