private void validateUser(User user) { if (user == null) throw new UserServiceException("User cannot be null"); if (user.getUsername() == null || user.getUsername().trim().isEmpty()) { throw new UserServiceException("username of user cannot be null or empty"); } if (user.getEmail() == null || user.getUsername().trim().isEmpty()) { throw new UserServiceException("email of user cannot be null or empty"); } }
@Override public User addUser(User user) { validateUser(user); try { String unencryptedPassword = user.getPassword(); String encryptedPassword = passwordEncoder.encode(unencryptedPassword); user.setPassword(encryptedPassword); return userRepository.save(user); } catch (Exception e) { throw new UserServiceException( String.format("Could not save user with username %s", user.getUsername())); } }