Skip to content

Commit

Permalink
[Feat] 챌린지 포인트 CQRS 구현 (#22-3)
Browse files Browse the repository at this point in the history
  • Loading branch information
bayy1216 committed Jun 1, 2024
1 parent 5f9c3ef commit 56c608d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -34,12 +37,30 @@ public Page<ChallengeGroupModel.Info> getChallengeGroupsShortsPaging(Pageable pa
}

public Page<ChallengeGroupModel.Ranking> getChallengeGroupRankingsPaging(Long challengeGroupId, Pageable pageable) {
ChallengeGroup challengeGroup = challengeGroupReader.getById(challengeGroupId);
throw new UnsupportedOperationException("Not implemented yet");
Page<ChallengeGroupUserExp> challengeGroupUserExps
= challengeGroupReader.getByChallengeGroupId(challengeGroupId, pageable);

return getRankingPage(pageable, challengeGroupUserExps);
}

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.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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public interface ChallengeGroupReader {
Page<ChallengeGroup> getChallengeGroupsShortsPaging(Pageable pageable, Long userId);

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

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

ChallengeGroupModel.Ranking getRanking(Long challengeGroupId, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -85,4 +90,57 @@ public Optional<ChallengeGroupUserExp> findByChallengeGroupIdAndUserId(Long chal
return Optional.ofNullable(result);
}

@Override
public Page<ChallengeGroupUserExp> getByChallengeGroupId(Long challengeGroupId, Pageable pageable) {
Long count = queryFactory
.select(QChallengeGroupUserExp.challengeGroupUserExp.count())
.from(QChallengeGroupUserExp.challengeGroupUserExp)
.where(QChallengeGroupUserExp.challengeGroupUserExp.challengeGroup.id.eq(challengeGroupId))
.fetchOne();

List<ChallengeGroupUserExp> 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();
}

}

0 comments on commit 56c608d

Please sign in to comment.