Skip to content

Commit

Permalink
[hotfix] #150 - join 하였을시 자동 업데이트 사항 진행
Browse files Browse the repository at this point in the history
- 오류를 수정하였지만 왜 save를 지웠을때 이것이 더티 체킹을 통해 진행되는 것은 알게 되었습니다.
- 이 부분은 추후에 왜 이렇게 작동이 되는지 명확하게 알아서 공유드리겠습니다.
  • Loading branch information
khyojun committed Jun 18, 2024
1 parent b816422 commit 0085b86
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 29 deletions.
17 changes: 8 additions & 9 deletions src/main/java/gitbal/backend/api/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,9 @@ public void join(JoinRequestDto joinRequestDto, CustomUserDetails user) {
UserDto userDto = initUserDto(joinRequestDto, gitbalApiDto, nickname);

joinUpdate(findUser, userDto);
updateRanking();
updateRank();
}

private void updateRanking() {
userService.updateUserRank();
schoolService.updateSchoolRank();
schoolService.updateSchoolGrade();
userService.updateUserGrade();
}

private UserDto initUserDto(JoinRequestDto joinRequestDto, GitbalApiDto gitbalApiDto,
String nickname) {
Expand Down Expand Up @@ -97,12 +91,10 @@ private void joinUpdate(User findUser, UserDto userDto) {
userDto.nickname(),
userDto.score(),
userDto.profile_img(),
Grade.YELLOW, //TODO : 일단 들어올 때 최하 등급으로 측정하기로 함! -> 이후에 기획 확정된 이후에 등급 계산 로직 넣고 수정해야함!
0
);
schoolService.joinNewUserScore(findUser);
regionService.joinNewUserScore(findUser);

}


Expand All @@ -123,4 +115,11 @@ public String logoutUser(String username) {
throw new LogoutException("로그아웃 실패");
}
}

private void updateRank() {
userService.updateUserRank();
userService.updateUserGrade();
schoolService.updateSchoolGrade();
schoolService.updateSchoolRank();
}
}
8 changes: 4 additions & 4 deletions src/main/java/gitbal/backend/domain/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public class User extends BaseTimeEntity {

@Column(name = "user_rank")
@ColumnDefault("0")
private Integer userRank;
private int userRank;

public void setGrade(Grade grade) { this.grade = grade; }

public void setUserRank(Integer userRank) {this.userRank = userRank;}
public void setUserRank(int userRank) {this.userRank = userRank;}

public void setSchool(School school) {
this.school = school;
Expand All @@ -95,7 +95,7 @@ public void setProfileImg(String profile_img) {
@Builder
public User(School school, Region region, OneDayCommit oneDayCommit,
List<MajorLanguage> majorLanguages,
String nickname, Long score, String profile_img, Grade grade,Integer userRank) {
String nickname, Long score, String profile_img, Grade grade,int userRank) {
this.school = school;
this.region = region;
this.oneDayCommit = oneDayCommit;
Expand All @@ -110,7 +110,7 @@ public User(School school, Region region, OneDayCommit oneDayCommit,

public void joinUpdateUser(School school, Region region, OneDayCommit oneDayCommit,
List<MajorLanguage> majorLanguages,
String nickname, Long score, String profile_img, Grade grade, Integer userRank) {
String nickname, Long score, String profile_img, int userRank) {
this.school = school;
this.region = region;
this.oneDayCommit = oneDayCommit;
Expand Down
19 changes: 5 additions & 14 deletions src/main/java/gitbal/backend/domain/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import gitbal.backend.global.exception.NotFoundUserException;
import gitbal.backend.global.security.GithubOAuth2UserInfo;
import gitbal.backend.global.util.SurroundingRankStatus;
import jakarta.transaction.Transactional;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -30,7 +30,7 @@ public class UserService {
private final UserInfoService userInfoService;
private final UserRepository userRepository;
private final int USER_AROUND_RANGE = 2;
private final int FIRST_RANK = 1;



public Long calculateUserScore(String nickname) {
Expand All @@ -42,7 +42,6 @@ public Long calculateUserScore(String nickname) {
return delegateToGitbalScore(dataNode);
} catch (JsonProcessingException e) {
throw new NotFoundUserException();
//throw new IllegalArgumentException(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
throw new NotFoundUserException();
Expand Down Expand Up @@ -177,16 +176,9 @@ public List<MajorLanguage> findMajorLanguagesByUsername(String username) {
@Transactional
public void updateUserRank() {
List<User> users = userRepository.findAll(Sort.by("score").descending());
int rank = FIRST_RANK;
int prevRank = FIRST_RANK;
for (int i = 0; i < users.size(); i++) {
User user = users.get(i);
if (i > 0 && users.get(i - 1).getScore() != user.getScore()) {
prevRank = rank;
}
user.setUserRank(prevRank);
userRepository.save(user);
rank = prevRank + 1;
int rank = 1;
for (User user : users) {
user.setUserRank(rank++);
}
}

Expand Down Expand Up @@ -226,6 +218,5 @@ private void updateGrade(List<User> users) {
user.setGrade(Grade.PURPLE);
}
}
userRepository.saveAll(users);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void run(String... args) throws Exception {

// Update user with the new relations
newUser.joinUpdateUser(randomSchool, randomRegion, oneDayCommit, majorLanguages,
newUser.getNickname(), newUser.getScore(), newUser.getProfile_img(), newUser.getGrade(), 0);
newUser.getNickname(), newUser.getScore(), newUser.getProfile_img(), 0);
User saveUser = userRepository.save(newUser);
scoring(saveUser);
}
Expand Down Expand Up @@ -142,7 +142,7 @@ private void createUserWithNickname(String nickName) {
OneDayCommit oneDayCommit1 = OneDayCommit.of(true);
oneDayCommitRepository.save(oneDayCommit1);
leesj000603.joinUpdateUser(school, region, oneDayCommit, majorLanguages,
leesj000603.getNickname(), leesj000603.getScore(), leesj000603.getProfile_img(), Grade.YELLOW, 0);
leesj000603.getNickname(), leesj000603.getScore(), leesj000603.getProfile_img(), 0);
userRepository.save(leesj000603);

}
Expand Down

0 comments on commit 0085b86

Please sign in to comment.