Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ public interface StatJpaRepository extends JpaRepository<Stat, Long> {
s.user.id = :userId
""")
void updateStatByUserIdCaseLose(long deltaScore, long userId);

@Query(
"""
SELECT new io.f1.backend.domain.stat.dto.StatWithNicknameAndUserId(
u.id, u.nickname, s.totalGames, s.winningGames, s.score
)
FROM Stat s JOIN s.user u
WHERE u.id = :userId
""")
Optional<StatWithNicknameAndUserId> findByUserId(long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,15 @@ public MyPageInfo getMyPageByUserId(long userId) {
log.error("Redis miss, fallback to MySQL for userId={}", userId, e);
}

StatWithNicknameAndUserId stat = findFirstMatchingStat(userId);
StatWithNicknameAndUserId stat = findStatByUserId(userId);
long rank = jpaRepository.countByScoreGreaterThan(stat.score()) + 1;

return new MyPageInfo(
stat.nickname(), rank, stat.totalGames(), stat.winningGames(), stat.score());
}

private StatWithNicknameAndUserId findFirstMatchingStat(long userId) {
return jpaRepository.findAllStatWithNicknameAndUserId().stream()
.filter(stat -> stat.userId() == userId)
.findFirst()
.orElseThrow(() -> new CustomException(UserErrorCode.USER_NOT_FOUND));
private StatWithNicknameAndUserId findStatByUserId(long userId) {
return jpaRepository.findByUserId(userId)
.orElseThrow(() -> new CustomException(UserErrorCode.USER_NOT_FOUND));
}
}