This repository has been archived by the owner on Nov 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/main/java/com/example/daemawiki/domain/auth/service/Signup.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.example.daemawiki.domain.auth.service; | ||
|
||
import com.example.daemawiki.domain.auth.dto.SignupRequest; | ||
import com.example.daemawiki.domain.mail.repository.AuthMailRepository; | ||
import com.example.daemawiki.domain.user.model.User; | ||
import com.example.daemawiki.domain.user.repository.UserRepository; | ||
import com.example.daemawiki.global.exception.EmailAlreadyExistsException; | ||
import com.example.daemawiki.global.exception.UnVerifiedEmailException; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.scheduler.Scheduler; | ||
|
||
@Service | ||
public class Signup { | ||
private final UserRepository userRepository; | ||
private final AuthMailRepository authMailRepository; | ||
private final PasswordEncoder passwordEncoder; | ||
private final Scheduler scheduler; | ||
|
||
public Signup(UserRepository userRepository,AuthMailRepository authMailRepository, PasswordEncoder passwordEncoder, Scheduler scheduler) { | ||
this.userRepository = userRepository; | ||
this.authMailRepository = authMailRepository; | ||
this.passwordEncoder = passwordEncoder; | ||
this.scheduler = scheduler; | ||
} | ||
|
||
public Mono<Void> execute(SignupRequest request) { | ||
return userRepository.findByEmail(request.email()) | ||
.flatMap(user -> Mono.error(EmailAlreadyExistsException.EXCEPTION)) | ||
.switchIfEmpty(Mono.defer(() -> authMailRepository.findByMail(request.email()) | ||
.flatMap(verified -> { | ||
if (!verified) { | ||
return Mono.error(UnVerifiedEmailException.EXCEPTION); | ||
} | ||
|
||
return Mono.fromCallable(() -> passwordEncoder.encode(request.password())) | ||
.subscribeOn(scheduler) | ||
.map(password -> User.builder() | ||
.nickname(request.nickname()) | ||
.email(request.email()) | ||
.password(password) | ||
.build()) | ||
.flatMap(userRepository::save); | ||
}))).then(); | ||
} | ||
|
||
} |