Skip to content

Commit

Permalink
Merge pull request #573 from DKU-Dgaja/refactor(#572)-Score-error
Browse files Browse the repository at this point in the history
[BE] refactor(#572): 스코어 호출 오류
  • Loading branch information
j-ra1n authored Aug 28, 2024
2 parents 63bd9e3 + c211e62 commit d90c1a3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ public UserRankingResponse getUserRankings(User user) {
Double score = zSetOps.score(USER_RANKING_KEY, user.getId());
if (score == null) {
score = (double) user.getScore();
saveUserScore(user.getId(), score.intValue());
saveUserScore(user.getId(), score);
}
Long ranking = zSetOps.reverseRank(USER_RANKING_KEY, user.getId());
if (ranking == null) {
return new UserRankingResponse(score.intValue(), 0L); // 랭킹이 없으면 0 반환
return new UserRankingResponse(score, 0L); // 랭킹이 없으면 0 반환
}
return new UserRankingResponse(score.intValue(), ranking + 1);
return UserRankingResponse.builder()
.ranking(ranking + 1)
.score(score)
.build();
}

// 어플리케이션 로드 시점에 Cache Warming 작업
Expand Down Expand Up @@ -97,16 +100,19 @@ public void saveStudyScore(Long studyInfoId, double score) {
public StudyRankingResponse getStudyRankings(StudyInfo studyInfo) {

ZSetOperations<String, Object> zSetOps = redisTemplate.opsForZSet();
Double score = zSetOps.score(STUDY_RANKING_KEY, studyInfo.getScore());
Double score = zSetOps.score(STUDY_RANKING_KEY, studyInfo.getId());
if (score == null) {
score = (double) studyInfo.getScore();
saveStudyScore(studyInfo.getId(), score.intValue());
saveStudyScore(studyInfo.getId(), score);
}

Long ranking = zSetOps.reverseRank(STUDY_RANKING_KEY, studyInfo.getId());
if (ranking == null) {
return new StudyRankingResponse(score.intValue(), 0L);
return new StudyRankingResponse(score, 0L);
}
return new StudyRankingResponse(score.intValue(), ranking + 1);
return StudyRankingResponse.builder()
.ranking(ranking + 1)
.score(score)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@AllArgsConstructor
public class StudyRankingResponse {

private int score;
private double score;

private Long ranking;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@AllArgsConstructor
public class UserRankingResponse {

private int score;
private double score;

private Long ranking;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.backend.MockTestConfig;
import com.example.backend.domain.define.account.user.repository.UserRepository;
import com.example.backend.domain.define.study.info.repository.StudyInfoRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -28,6 +29,9 @@ public class RankingServiceTest extends MockTestConfig {
@Autowired
private RedisTemplate<String, Object> redisTemplate;

@Autowired
private StudyInfoRepository studyInfoRepository;

private Random random = new Random();
private static final String USER_RANKING_KEY = "user_ranking";
private static final String STUDY_RANKING_KEY = "study_ranking";
Expand Down Expand Up @@ -130,22 +134,28 @@ void tearDown() {
rankingService.updateStudyScore(studyInfoId, 13);
}

/*@Test
/* @Test
void 특정_스터디_랭킹_조회() {
StudyInfo studyInfo1 = studyInfoRepository.save(StudyInfoFixture.createPublicStudyInfoScore(1L, 530));
StudyInfo studyInfo2 = studyInfoRepository.save(StudyInfoFixture.createPublicStudyInfoScore(2L, 100));
StudyInfo studyInfo3 = studyInfoRepository.save(StudyInfoFixture.createPublicStudyInfoScore(3L, 40));
StudyInfo studyInfo4 = studyInfoRepository.save(StudyInfoFixture.createPublicStudyInfoScore(4L, 1200));
StudyInfo studyInfo5 = studyInfoRepository.save(StudyInfoFixture.createPublicStudyInfoScore(5L, 30));
StudyInfo studyInfo6 = studyInfoRepository.save(StudyInfoFixture.createPublicStudyInfoScore(6L, 20));
rankingService.saveStudyScore(studyInfo1.getId(), studyInfo1.getScore());
rankingService.saveStudyScore(studyInfo2.getId(), studyInfo2.getScore());
rankingService.saveStudyScore(studyInfo3.getId(), studyInfo3.getScore());
rankingService.saveStudyScore(studyInfo4.getId(), studyInfo4.getScore());
StudyRankingResponse response1 = rankingService.getStudyRankings(studyInfo1);
StudyRankingResponse response2 = rankingService.getStudyRankings(studyInfo2);
// 3과4는 redis에 점수가 null로 저장되어있을때 잘 가져오는지 확인
StudyRankingResponse response3 = rankingService.getStudyRankings(studyInfo3);
StudyRankingResponse response4 = rankingService.getStudyRankings(studyInfo4);
StudyRankingResponse response5 = rankingService.getStudyRankings(studyInfo3);
StudyRankingResponse response6 = rankingService.getStudyRankings(studyInfo4);
//then
assertEquals(response4.getRanking(), 1);
Expand All @@ -159,5 +169,11 @@ void tearDown() {
assertEquals(response3.getRanking(), 4);
assertEquals(response3.getScore(), 40);
assertEquals(response4.getRanking(), 5);
assertEquals(response4.getScore(), 30);
assertEquals(response5.getRanking(), 6);
assertEquals(response5.getScore(), 20);
}*/
}

0 comments on commit d90c1a3

Please sign in to comment.