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

Commit

Permalink
회원가입 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ori0o0p committed Jan 29, 2024
1 parent f98f3b9 commit ccf63a7
Showing 1 changed file with 48 additions and 0 deletions.
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();
}

}

0 comments on commit ccf63a7

Please sign in to comment.