Ejemplo n.º 1
0
  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");
    }
  }
Ejemplo n.º 2
0
  @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()));
    }
  }