Skip to content

Commit

Permalink
Merge pull request #46 from KNU-HAEDAL/refactor/rank
Browse files Browse the repository at this point in the history
[Refactor]: 챌린지그룹별 랭킹 페이징 코드 개선
  • Loading branch information
momnpa333 authored Jul 21, 2024
2 parents 5c7cf1c + db5fc75 commit 59f6138
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Builder;
import org.haedal.zzansuni.domain.challengegroup.challenge.ChallengeModel;
import org.haedal.zzansuni.domain.challengegroup.image.ChallengeGroupImage;
import org.haedal.zzansuni.domain.challengegroup.userexp.ChallengeGroupUserExp;
import org.haedal.zzansuni.domain.user.UserModel;

import java.time.LocalDate;
Expand Down Expand Up @@ -119,6 +120,13 @@ public record Ranking(
Integer rank,
Integer accumulatedPoint
) {
public static Ranking from(ChallengeGroupUserExp challengeGroupUserExp, int rank) {
return Ranking.builder()
.user(UserModel.Main.from(challengeGroupUserExp.getUser()))
.rank(rank)
.accumulatedPoint(challengeGroupUserExp.getTotalExp())
.build();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,20 @@ public Page<ChallengeGroupModel.Info> getChallengeGroupsShortsPaging(Pageable pa
return challengeGroups.map(ChallengeGroupModel.Info::from);
}

@Transactional(readOnly = true)
public Page<ChallengeGroupModel.Ranking> getChallengeGroupRankingsPaging(Long challengeGroupId, Pageable pageable) {
Page<ChallengeGroupUserExp> challengeGroupUserExps
= challengeGroupReader.getByChallengeGroupId(challengeGroupId, pageable);

return getRankingPage(pageable, challengeGroupUserExps);
= challengeGroupReader.getUserExpPagingWithUserByChallengeGroupId(challengeGroupId, pageable);

// Page<ChallengeGroupUserExp>를 Page<ChallengeGroupModel.Ranking>으로 변환
// [rank]는 [Pageable]의 위치에 따라 계산된다.
return challengeGroupUserExps.map(e->{
int rank = challengeGroupUserExps.getNumber() * challengeGroupUserExps.getSize() + 1
+ challengeGroupUserExps.getContent().indexOf(e);
return ChallengeGroupModel.Ranking.from(e, rank);
});
}

private static PageImpl<ChallengeGroupModel.Ranking> getRankingPage(Pageable pageable, Page<ChallengeGroupUserExp> challengeGroupUserExps) {
List<ChallengeGroupModel.Ranking> rankings = new ArrayList<>();
for(int i = 0; i < challengeGroupUserExps.getContent().size(); i++) {
Integer rank = challengeGroupUserExps.getNumber() * challengeGroupUserExps.getSize() + 1 + i;
ChallengeGroupUserExp challengeGroupUserExp = challengeGroupUserExps.getContent().get(i);
var rankingModel = ChallengeGroupModel.Ranking.builder()
.user(UserModel.Main.from(challengeGroupUserExp.getUser()))
.accumulatedPoint(challengeGroupUserExp.getTotalExp())
.rank(rank)
.build();
rankings.add(rankingModel);
}
return new PageImpl<>(rankings, pageable, challengeGroupUserExps.getTotalElements());
}

public ChallengeGroupModel.Ranking getChallengeGroupRanking(Long challengeGroupId, Long id) {
return challengeGroupReader.getRanking(challengeGroupId, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface ChallengeGroupReader {

Optional<ChallengeGroupUserExp> findByChallengeGroupIdAndUserId(Long challengeGroupId, Long userId);

Page<ChallengeGroupUserExp> getByChallengeGroupId(Long challengeGroupId, Pageable pageable);
Page<ChallengeGroupUserExp> getUserExpPagingWithUserByChallengeGroupId(Long challengeGroupId, Pageable pageable);

ChallengeGroupModel.Ranking getRanking(Long challengeGroupId, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public Optional<ChallengeGroupUserExp> findByChallengeGroupIdAndUserId(Long chal
}

@Override
public Page<ChallengeGroupUserExp> getByChallengeGroupId(Long challengeGroupId, Pageable pageable) {
public Page<ChallengeGroupUserExp> getUserExpPagingWithUserByChallengeGroupId(Long challengeGroupId, Pageable pageable) {
Long count = queryFactory
.select(challengeGroupUserExp.count())
.from(challengeGroupUserExp)
Expand All @@ -104,6 +104,7 @@ public Page<ChallengeGroupUserExp> getByChallengeGroupId(Long challengeGroupId,
List<ChallengeGroupUserExp> page = queryFactory
.selectFrom(challengeGroupUserExp)
.where(challengeGroupUserExp.challengeGroup.id.eq(challengeGroupId))
.innerJoin(challengeGroupUserExp.user).fetchJoin()
.orderBy(challengeGroupUserExp.totalExp.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
Expand Down

0 comments on commit 59f6138

Please sign in to comment.