Skip to content

Commit

Permalink
feat(#30): 회원가입 API 구현
Browse files Browse the repository at this point in the history
- 회원가입 기능을 구현했어요.
- 회원가입시 이메일 인증이 되어있지 않으면 예외를 발생시켜요.
- 회원가입시 이미 존재하는 유저면 예외를 발생시켜요.
  • Loading branch information
cabbage16 committed Nov 27, 2024
1 parent 589ffbb commit 4910015
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.bamdoliro.sinabro.application.user;

import com.bamdoliro.sinabro.domain.user.domain.SignUpVerification;
import com.bamdoliro.sinabro.domain.user.domain.User;
import com.bamdoliro.sinabro.domain.user.domain.type.Authority;
import com.bamdoliro.sinabro.infrastructure.persistence.user.SignUpVerificationRepository;
import com.bamdoliro.sinabro.infrastructure.persistence.user.UserRepository;
import com.bamdoliro.sinabro.presentation.user.dto.request.SignUpRequest;
import com.bamdoliro.sinabro.shared.annotation.UseCase;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@UseCase
public class SignUpUseCase {

private final UserRepository userRepository;
private final SignUpVerificationRepository signUpVerificationRepository;

@Transactional
public void execute(SignUpRequest request) {
validate(request);

userRepository.save(
User.builder()
.email(request.getEmail())
.name(request.getName())
.password(request.getPassword())
.authority(Authority.USER)
.build()
);
}

private void validate(SignUpRequest request) {
SignUpVerification signUpVerification = signUpVerificationRepository.findById(request.getEmail())
.orElseThrow(RuntimeException::new);

if (!signUpVerification.getIsVerified()) {
throw new RuntimeException();
}

if (userRepository.existsByEmail(request.getEmail())) {
throw new RuntimeException();
}

signUpVerificationRepository.delete(signUpVerification);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> findByEmail(String email);

boolean existsByEmail(String email);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.bamdoliro.sinabro.presentation.user.dto.request;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class SignUpRequest {

@NotBlank(message = "필수값입니다.")
@Email(message = "이메일 형식이어야 합니다")
private String email;

@NotBlank(message = "필수값입니다.")
@Size(max = 50, message = "50자 이하여야 합니다.")
private String name;

@NotBlank(message = "필수값입니다.")
@Size(max = 20, message = "비밀번호는 20글자 이하여야 합니다.")
@Pattern(regexp = "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{8,}$",
message = "비밀번호는 최소 하나의 문자, 숫자, 특수문자를 포함하며 8 글자 이상이어야 합니다.")
private String password;
}

0 comments on commit 4910015

Please sign in to comment.