Last active
May 6, 2020 11:16
-
-
Save kaibeedevadmin/49d8093b2369f7479e21884bd239a81a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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