-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: ranking system 구현 #189
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f33cc2f
feat: 회원의 랭킹 redis에 추가 및 삭제, 업데이트 기능 추가
parksey 55bc1c5
Merge branch 'develop' of https://github.com/team-moabam/moabam-BE in…
parksey 490b4a9
test: 회원 정보 변경 및 삭제 추가에 따른 랭킹 참여, 제외 테스트 코드 추가
parksey 51fe353
feat: 랭킹시스템 API 추가 및 랭킹 조회 기능 추가
parksey fbe427a
Merge branch 'develop' of https://github.com/team-moabam/moabam-BE in…
parksey 0ac854c
feat: 랭킹 조회 테스트 코드 추가 및 랭킹 업데이트 로직 각 업데이트 -> 스케쥴러
parksey ff26e4d
Merge branch 'develop' of https://github.com/team-moabam/moabam-BE in…
parksey f585949
style: checkstyle 에러 fix
parksey f5783bb
refactor: 응답 객체명 변경 TopRankingInfoResponse -> TopRankingInfo
parksey b0c12ca
fix: 랭킹 업데이트 시간 15분 매초마다 동작하는 방식 -> 15분에 한 번만 실행되도록 변경
parksey 286820b
refactor: 랭킹 응답 반환 객체 변수면 s 제거
parksey 431c95b
refactor: ToprankingResponses 응답 객체 반환명 TopRankingResponse로 변경
parksey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.TopRankingResponses; | ||
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 TopRankingResponses topRankingResponses(TopRankingInfo myRanking, | ||
List<TopRankingInfo> topRankings) { | ||
return TopRankingResponses.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.TopRankingResponses; | ||
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 TopRankingResponses 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/TopRankingResponses.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 TopRankingResponses( | ||
parksey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,9 @@ | |
import java.util.Set; | ||
|
||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.ZSetOperations; | ||
import org.springframework.data.redis.core.ZSetOperations.TypedTuple; | ||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
@@ -35,4 +38,41 @@ public Long rank(String key, Object value) { | |
.opsForZSet() | ||
.rank(key, value); | ||
} | ||
|
||
public void add(String key, Object value, double score) { | ||
redisTemplate | ||
.opsForZSet() | ||
.add(requireNonNull(key), requireNonNull(value), score); | ||
} | ||
|
||
public void changeMember(String key, Object before, Object after) { | ||
Double score = redisTemplate.opsForZSet().score(key, before); | ||
|
||
if (score == null) { | ||
return; | ||
} | ||
|
||
delete(key, before); | ||
add(key, after, score); | ||
} | ||
|
||
public void delete(String key, Object value) { | ||
redisTemplate.opsForZSet().remove(key, value); | ||
} | ||
|
||
public Set<TypedTuple<Object>> rangeJson(String key, int startIndex, int limitIndex) { | ||
setSerialize(Object.class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 직렬화하는 이유가 궁금합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재 직렬화를 하지 않으면 redis로 들어가는 데이터가 string으로 되어있는데, |
||
Set<ZSetOperations.TypedTuple<Object>> rankings = redisTemplate.opsForZSet() | ||
.reverseRangeWithScores(key, startIndex, limitIndex); | ||
setSerialize(String.class); | ||
return rankings; | ||
} | ||
|
||
public Long reverseRank(String key, Object myRankingInfo) { | ||
return redisTemplate.opsForZSet().reverseRank(key, myRankingInfo); | ||
} | ||
|
||
private void setSerialize(Class classes) { | ||
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(classes)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C: RankingService에 있는건 어떤가요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
의존성 순환 생겨서 memberSearchService로 옮겨야하는데 좀 바뀌는게 많아서 불편해질 때 변경하겠습니다!