Skip to content

Commit

Permalink
Merge pull request #591 from DKU-Dgaja/refactor(#590)-ranking
Browse files Browse the repository at this point in the history
[BE] refactor(#590): 랭킹 시스템 로직 수정
  • Loading branch information
saesang authored Sep 6, 2024
2 parents f4eeac0 + c4e177b commit f679b66
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ public void userDelete(User contextUser, String reason) {
throw new UserException(ExceptionMessage.USER_NOT_FOUND);
});

// 랭킹 스코어 삭제
rankingService.deleteUserScore(findUser.getId());

// 사용자가 운영하던 스터디 종료
studyManagementService.closeStudiesOwnedByUser(findUser.getId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.example.backend.auth.api.service.rank.response.StudyRankingResponse;
import com.example.backend.auth.api.service.rank.response.UserRankingResponse;
import com.example.backend.domain.define.account.user.User;
import com.example.backend.domain.define.account.user.constant.UserRole;
import com.example.backend.domain.define.account.user.repository.UserRepository;
import com.example.backend.domain.define.study.info.StudyInfo;
import com.example.backend.domain.define.study.info.constant.StudyStatus;
import com.example.backend.domain.define.study.info.repository.StudyInfoRepository;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -66,20 +68,36 @@ public UserRankingResponse getUserRankings(User user) {
.build();
}

// 유저 점수 삭제
public void deleteUserScore(Long userId) {
ZSetOperations<String, Object> zSetOps = redisTemplate.opsForZSet();
zSetOps.remove(USER_RANKING_KEY, userId);
}

// 어플리케이션 로드 시점에 Cache Warming 작업
public void updateUserRanking() {
ZSetOperations<String, Object> zSetOps = redisTemplate.opsForZSet();
// USER 역할인 사용자만 필터링하여 랭킹에 추가
List<User> users = userRepository.findAll();
for (User user : users) {
zSetOps.add(USER_RANKING_KEY, user.getId(), user.getScore());
if (user.getRole() == UserRole.USER) {
zSetOps.add(USER_RANKING_KEY, user.getId(), user.getScore());
}
}
}

// 마찬가지로 로드 시점에 Cache Warming
public void updateStudyRanking() {
ZSetOperations<String, Object> zSetOps = redisTemplate.opsForZSet();
List<StudyInfo> studyInfos = studyInfoRepository.findAll();
for (StudyInfo studyInfo : studyInfos) {

// STUDY_DELETED 상태를 가진 스터디 제외
List<StudyInfo> filteredStudyInfos = studyInfos.stream()
.filter(studyInfo -> !StudyStatus.STUDY_DELETED.equals(studyInfo.getStatus()))
.toList();

// 필터링된 스터디 정보를 Redis에 업데이트
for (StudyInfo studyInfo : filteredStudyInfos) {
zSetOps.add(STUDY_RANKING_KEY, studyInfo.getId(), studyInfo.getScore());
}
}
Expand Down Expand Up @@ -115,4 +133,10 @@ public StudyRankingResponse getStudyRankings(StudyInfo studyInfo) {
.score(score)
.build();
}

// 스터디 점수 삭제
public void deleteStudyScore(Long studyInfoId) {
ZSetOperations<String, Object> zSetOps = redisTemplate.opsForZSet();
zSetOps.remove(STUDY_RANKING_KEY, studyInfoId);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.backend.study.api.service.info;

import com.example.backend.auth.api.controller.auth.response.UserInfoResponse;
import com.example.backend.auth.api.service.rank.RankingService;
import com.example.backend.auth.api.service.rank.event.StudyScoreSaveEvent;
import com.example.backend.auth.api.service.rank.event.UserScoreUpdateEvent;
import com.example.backend.common.exception.ExceptionMessage;
Expand Down Expand Up @@ -59,6 +60,7 @@ public class StudyInfoService {
private final GithubApiService githubApiService;
private final GithubApiTokenService githubApiTokenService;
private final ApplicationEventPublisher eventPublisher;
private final RankingService rankingService;

@Transactional
public StudyInfoRegisterResponse registerStudy(StudyInfoRegisterRequest request, UserInfoResponse userInfo) {
Expand Down Expand Up @@ -126,6 +128,9 @@ public boolean deleteStudy(Long studyInfoId) {
// 스터디 멤버 상태정보 변경
updateWithdrawalStudyMember(studyInfoId);

// 랭킹 스코어 삭제
rankingService.deleteStudyScore(studyInfo.getId());

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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 com.example.backend.study.api.service.info.StudyInfoService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -32,6 +33,9 @@ public class RankingServiceTest extends MockTestConfig {
@Autowired
private StudyInfoRepository studyInfoRepository;

@Autowired
private StudyInfoService studyInfoService;

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 @@ -176,4 +180,87 @@ void tearDown() {
assertEquals(response5.getRanking(), 6);
assertEquals(response5.getScore(), 20);
}*/
/*
@Test
void 스터디_score_정렬_같은점수시_studyInfoId_순으로_재정렬_테스트() {
//given
User user = UserFixture.generateAuthUser();
userRepository.save(user);
User user2 = UserFixture.generateKaKaoUser();
userRepository.save(user2);
StudyInfo studyInfo1 = studyInfoRepository.save(StudyInfoFixture.createPublicStudyInfoScore(1L, 530));
StudyInfo studyInfo2 = studyInfoRepository.save(StudyInfoFixture.createPublicStudyInfoScore(2L, 2));
StudyInfo studyInfo3 = studyInfoRepository.save(StudyInfoFixture.createPublicStudyInfoScore(3L, 2));
StudyInfo studyInfo4 = studyInfoRepository.save(StudyInfoFixture.createPublicStudyInfoScore(4L, 1200));
StudyInfo studyInfo5 = studyInfoRepository.save(StudyInfoFixture.createPublicStudyInfoScore(5L, 2));
StudyInfo studyInfo6 = studyInfoRepository.save(StudyInfoFixture.createPublicStudyInfoScore(6L, 2));
rankingService.saveStudyScore(studyInfo1.getId(), studyInfo1.getScore());
rankingService.saveStudyScore(studyInfo2.getId(), studyInfo2.getScore());
rankingService.saveStudyScore(studyInfo3.getId(), studyInfo3.getScore());
rankingService.saveStudyScore(studyInfo4.getId(), studyInfo4.getScore());
rankingService.saveStudyScore(studyInfo5.getId(), studyInfo5.getScore());
rankingService.saveStudyScore(studyInfo6.getId(), studyInfo6.getScore());
// when
StudyInfoListAndCursorIdxResponse response1 = studyInfoService.selectStudyInfoList(user2.getId()
, null
, 10L
, "score"
, false);
StudyRankingResponse si1 = rankingService.getStudyRankings(studyInfo1);
StudyRankingResponse si2 = rankingService.getStudyRankings(studyInfo2);
StudyRankingResponse si3 = rankingService.getStudyRankings(studyInfo3);
StudyRankingResponse si4 = rankingService.getStudyRankings(studyInfo4);
StudyRankingResponse si5 = rankingService.getStudyRankings(studyInfo5);
StudyRankingResponse si6 = rankingService.getStudyRankings(studyInfo6);
// then
assertEquals(response1.getStudyInfoList().get(0).getId(), studyInfo4.getId());
assertEquals(response1.getStudyInfoList().get(1).getId(), studyInfo1.getId());
assertEquals(response1.getStudyInfoList().get(2).getId(), studyInfo6.getId());
assertEquals(response1.getStudyInfoList().get(3).getId(), studyInfo5.getId());
assertEquals(response1.getStudyInfoList().get(4).getId(), studyInfo3.getId());
assertEquals(response1.getStudyInfoList().get(5).getId(), studyInfo2.getId());
assertEquals(si1.getRanking(), 2);
assertEquals(si1.getScore(), 530);
assertEquals(si2.getRanking(), 6);
assertEquals(si2.getScore(), 2);
assertEquals(si3.getRanking(), 5);
assertEquals(si3.getScore(), 2);
assertEquals(si4.getRanking(), 1);
assertEquals(si4.getScore(), 1200);
assertEquals(si5.getRanking(), 4);
assertEquals(si5.getScore(), 2);
assertEquals(si6.getRanking(), 3);
assertEquals(si6.getScore(), 2);
// when
studyInfoService.deleteStudy(studyInfo6.getId());
rankingService.updateStudyRanking(); // 레디스 캐시 초기화
StudyRankingResponse si55 = rankingService.getStudyRankings(studyInfo5);
// then
assertEquals(si55.getRanking(), 3);
assertEquals(si55.getScore(), 2);
}*/
}

0 comments on commit f679b66

Please sign in to comment.