diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupQueryService.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupQueryService.java index 32360ce..cd83e87 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupQueryService.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupQueryService.java @@ -1,11 +1,14 @@ package org.haedal.zzansuni.domain.challengegroup; import lombok.RequiredArgsConstructor; +import org.haedal.zzansuni.domain.user.UserModel; import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.List; @Service @@ -34,12 +37,30 @@ public Page getChallengeGroupsShortsPaging(Pageable pa } public Page getChallengeGroupRankingsPaging(Long challengeGroupId, Pageable pageable) { - ChallengeGroup challengeGroup = challengeGroupReader.getById(challengeGroupId); - throw new UnsupportedOperationException("Not implemented yet"); + Page challengeGroupUserExps + = challengeGroupReader.getByChallengeGroupId(challengeGroupId, pageable); + return getRankingPage(pageable, challengeGroupUserExps); + } + + private static PageImpl getRankingPage(Pageable pageable, Page challengeGroupUserExps) { + List 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.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) { - throw new UnsupportedOperationException("Not implemented yet"); + return challengeGroupReader.getRanking(challengeGroupId, id); } + + } diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupReader.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupReader.java index e80db83..4652b6d 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupReader.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/ChallengeGroupReader.java @@ -15,4 +15,8 @@ public interface ChallengeGroupReader { Page getChallengeGroupsShortsPaging(Pageable pageable, Long userId); Optional findByChallengeGroupIdAndUserId(Long challengeGroupId, Long userId); + + Page getByChallengeGroupId(Long challengeGroupId, Pageable pageable); + + ChallengeGroupModel.Ranking getRanking(Long challengeGroupId, Long userId); } diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/challengegroup/ChallengeGroupReaderImpl.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/challengegroup/ChallengeGroupReaderImpl.java index 616e983..93c87f6 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/challengegroup/ChallengeGroupReaderImpl.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/infrastructure/challengegroup/ChallengeGroupReaderImpl.java @@ -7,9 +7,13 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; import org.haedal.zzansuni.domain.challengegroup.*; +import org.haedal.zzansuni.domain.user.QUser; +import org.haedal.zzansuni.domain.user.User; +import org.haedal.zzansuni.domain.user.UserModel; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; import java.util.List; @@ -20,6 +24,7 @@ @RequiredArgsConstructor public class ChallengeGroupReaderImpl implements ChallengeGroupReader { private final JPAQueryFactory queryFactory; + private final JdbcTemplate jdbcTemplate; private final ChallengeGroupRepository challengeGroupRepository; @Override public ChallengeGroup getById(Long challengeGroupId) { @@ -85,4 +90,57 @@ public Optional findByChallengeGroupIdAndUserId(Long chal return Optional.ofNullable(result); } + @Override + public Page getByChallengeGroupId(Long challengeGroupId, Pageable pageable) { + Long count = queryFactory + .select(QChallengeGroupUserExp.challengeGroupUserExp.count()) + .from(QChallengeGroupUserExp.challengeGroupUserExp) + .where(QChallengeGroupUserExp.challengeGroupUserExp.challengeGroup.id.eq(challengeGroupId)) + .fetchOne(); + + List page = queryFactory + .selectFrom(QChallengeGroupUserExp.challengeGroupUserExp) + .where(QChallengeGroupUserExp.challengeGroupUserExp.challengeGroup.id.eq(challengeGroupId)) + .orderBy(QChallengeGroupUserExp.challengeGroupUserExp.totalExp.desc()) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .fetch(); + + return new PageImpl<>(page, pageable, count == null ? 0 : count); + } + + @Override + public ChallengeGroupModel.Ranking getRanking(Long challengeGroupId, Long userId) { + User user = queryFactory + .selectFrom(QUser.user) + .where(QUser.user.id.eq(userId)) + .fetchOne(); + if(user == null) { + throw new NoSuchElementException(); + } + + String sql = "SELECT ranked.rank1 FROM (" + + " SELECT user_id, RANK() OVER (ORDER BY total_exp DESC) as rank1 " + + " FROM challenge_group_user_exp " + + " WHERE challenge_group_id = ?" + + ") as ranked " + + "WHERE ranked.user_id = ?"; + + Integer rank = jdbcTemplate.queryForObject(sql, Integer.class, challengeGroupId, userId); + + + ChallengeGroupUserExp challengeGroupUserExp = queryFactory + .select(QChallengeGroupUserExp.challengeGroupUserExp) + .from(QChallengeGroupUserExp.challengeGroupUserExp) + .where(QChallengeGroupUserExp.challengeGroupUserExp.challengeGroup.id.eq(challengeGroupId) + .and(QChallengeGroupUserExp.challengeGroupUserExp.user.id.eq(userId))) + .fetchOne(); + + return ChallengeGroupModel.Ranking.builder() + .rank(rank==null ? 0 : rank) + .accumulatedPoint(challengeGroupUserExp != null ? challengeGroupUserExp.getTotalExp() : 0) + .user(UserModel.from(user)) + .build(); + } + }