Skip to content

Commit

Permalink
Merge pull request #26 from Leets-Official/25-fix-랭킹-유저-도메인-저장
Browse files Browse the repository at this point in the history
25 fix 랭킹기능에 순위 추가
  • Loading branch information
zzu-yaaa authored Jul 29, 2024
2 parents 2e19983 + 3606cdc commit 1ec05d5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.leets.commitatobe.domain.user.domain.repository.UserRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -69,6 +70,18 @@ public void calculateAndSaveExp(String githubId){
user.updateTotalCommitCount(totalCommitCount);
user.updateTodayCommitCount(todayCommitCount);

List<User> allUsers = userRepository.findAllByOrderByExpDesc(Pageable.unpaged()).getContent();
int ranking = 0;
int previousExp = -1;
for (User userToUpdate : allUsers){
if (userToUpdate.getExp()!=previousExp) {
ranking += 1;
previousExp=userToUpdate.getExp();//만약 경험치 같으면 동일한 랭킹부여.
}
userToUpdate.updateRank(ranking);//랭킹 업데이트
userRepository.save(userToUpdate);//데이터베이스에 저장
}

commitRepository.saveAll(commits);//변경된 커밋 정보 데이터베이스에 저장
userRepository.save(user);//변경된 사용자 정보 데이터베이스에 저장
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class User extends BaseTimeEntity {
@Column
private Integer todayCommitCount;

@Column
private Integer ranking;// 랭킹 추가

@OneToMany(mappedBy = "user")
@JsonManagedReference
private List<Commit> commitList;
Expand Down Expand Up @@ -77,4 +80,6 @@ public void updateTotalCommitCount(Integer totalCommitCount){
public void updateTodayCommitCount(Integer todayCommitCount){
this.todayCommitCount=todayCommitCount;
}

public void updateRank(Integer ranking) { this.ranking = ranking; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public record UserRankResponse(
String username,
Integer exp,
Integer consecutiveCommitDays,
String tierName
String tierName,
Integer ranking//랭킹 추가
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public Page<UserRankResponse> getUsersByExp(Pageable pageable){//경험치 순
user.getUsername(),
user.getExp(),
user.getConsecutiveCommitDays(),
tier!=null?tier.getTierName():"Unranked");
tier!=null?tier.getTierName():"Unranked",
user.getRanking());//랭킹 추가
});
}

Expand Down

0 comments on commit 1ec05d5

Please sign in to comment.