Skip to content

Commit

Permalink
Fixing the errors that caused build failure
Browse files Browse the repository at this point in the history
  • Loading branch information
surajcm committed Feb 22, 2024
1 parent 4872355 commit 8613c05
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
1 change: 0 additions & 1 deletion config/checkstyle/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ WHAT AND WHY CHECKS SUPPRESSED
<suppress files="SecurityConfig.java" checks="MethodLength"/>
<suppress files="SecurityConfig.java" checks="IllegalCatch"/>
<suppress files="RegistrationController.java" checks="MethodLength"/>
<suppress files="UserServiceImpl.java" checks="MethodLength"/>
</suppressions>
50 changes: 27 additions & 23 deletions src/main/java/com/quiz/darkhold/user/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,31 @@ public UserServiceImpl(final UserRepository userRepository,

@Override
public void save(final User user) {
var isUpdating = (user.getId() != null);
if (isUpdating) {
var existingUser = userRepository.findById(user.getId()).get();
if (user.getPassword().isEmpty()) {
user.setPassword(existingUser.getPassword());
} else {
var pass = encoder.encode(user.getPassword());
user.setPassword(pass);
}
if (isUpdatingUser(user)) {
var existingUser = findExistingUser(user.getId());
user.setPassword(getPassword(user, existingUser));
} else {
var pass = encoder.encode(user.getPassword());
user.setPassword(pass);
user.setPassword(encodePassword(user.getPassword()));
}
userRepository.save(user);
}

private boolean isUpdatingUser(final User user) {
return user.getId() != null;
}

private User findExistingUser(final Long id) {
return userRepository.findById(id).get();
}

private String getPassword(final User newUser, final User existingUser) {
return newUser.getPassword().isEmpty() ? existingUser.getPassword() : encodePassword(newUser.getPassword());
}

private String encodePassword(final String password) {
return encoder.encode(password);
}

@Override
public User findByUsername(final String email) {
return userRepository.findByEmail(email);
Expand All @@ -63,21 +72,16 @@ public Boolean isEmailUnique(final Long id, final String email) {
if (userByEmail == null) {
return true;
}
var isCreatingNew = (id == null);
if (isCreatingNew) {
if (userByEmail != null) {
return false;
}
} else {
if (userByEmail.getId().equals(id)) {
return false;
}
}
return userByEmail == null;
return isCreatingNew(id) ? userByEmail == null : !userByEmail.getId().equals(id);
}

private boolean isCreatingNew(final Long id) {
return id == null;
}

@Override
public User get(final Long id) throws UserNotFoundException {
return userRepository.findById(id).orElseThrow(() -> new UserNotFoundException("Could not find any user with the id " + id));
return userRepository.findById(id)
.orElseThrow(() -> new UserNotFoundException("Could not find any user with the id " + id));
}
}

0 comments on commit 8613c05

Please sign in to comment.