Skip to content

Instantly share code, notes, and snippets.

@kaibeedevadmin
Last active May 6, 2020 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaibeedevadmin/49d8093b2369f7479e21884bd239a81a to your computer and use it in GitHub Desktop.
Save kaibeedevadmin/49d8093b2369f7479e21884bd239a81a to your computer and use it in GitHub Desktop.
public class UserFactory {
private UserRepository userRepository;
public UserFactory(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User createUser(UserInfo userInfo) {
String email = userInfo.getEmail();
Optional<User> userFoundbyEmail = userRepository.findByEmail(email);
if (userFoundbyEmail.isPresent()) {
throw new UserEmailAlreadyUsedException();
}
if (isEmailInvalid(email)) {
throw new InvalidUserEmailException();
}
return userRepository.save(mapToUser(userInfo));
}
private boolean isEmailInvalid(String userInfo) {
// for example only
return false;
}
private User mapToUser(UserInfo userInfo) {
// for example only
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment