Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
refactor Signup.java
Browse files Browse the repository at this point in the history
  • Loading branch information
ori0o0p committed Mar 16, 2024
1 parent 4040d28 commit b0f78f0
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/main/java/com/example/daemawiki/domain/auth/service/Signup.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,27 @@ public Signup(UserRepository userRepository,AuthMailRepository authMailRepositor
public Mono<Void> execute(SignupRequest request) {
return userRepository.findByEmail(request.email())
.flatMap(user -> Mono.error(EmailAlreadyExistsException.EXCEPTION))
.switchIfEmpty(Mono.defer(() -> authMailRepository.findByMail(request.email())
.filter(verified -> verified)
.switchIfEmpty(Mono.error(UnVerifiedEmailException.EXCEPTION))
.flatMap(verified -> Mono.fromSupplier(() -> passwordEncoder.encode(request.password()))
.subscribeOn(scheduler)
.flatMap(password -> createUser(request, password))
.flatMap(user -> userRepository.save(user)
.flatMap(savedUser -> createDocumentByUser.execute(savedUser)
.doOnNext(document -> savedUser.setDocumentId(document.getId()))
.flatMap(document -> userRepository.save(savedUser))
.then(authMailRepository.delete(savedUser.getEmail()))))
.onErrorMap(e -> ExecuteFailedException.EXCEPTION)
))).then();
.switchIfEmpty(Mono.defer(() -> checkAndCreateUser(request)))
.then();
}

private Mono<Void> checkAndCreateUser(SignupRequest request) {
return authMailRepository.findByMail(request.email())
.filter(verified -> verified)
.switchIfEmpty(Mono.error(UnVerifiedEmailException.EXCEPTION))
.flatMap(verified -> Mono.fromSupplier(() -> passwordEncoder.encode(request.password()))
.subscribeOn(scheduler)
.flatMap(password -> createUser(request, password))
.flatMap(this::saveUserAndCreateDocument)
.then(authMailRepository.delete(request.email()))
.onErrorMap(e -> ExecuteFailedException.EXCEPTION));
}

private Mono<User> saveUserAndCreateDocument(User user) {
return userRepository.save(user)
.flatMap(savedUser -> createDocumentByUser.execute(savedUser)
.doOnNext(document -> savedUser.setDocumentId(document.getId()))
.flatMap(document -> userRepository.save(savedUser)));
}

private Mono<User> createUser(SignupRequest request, String password) {
Expand Down

0 comments on commit b0f78f0

Please sign in to comment.