Skip to content

Commit

Permalink
feat: 학생이 아닌 유저의 회원가입 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jcy0308 committed Aug 2, 2024
1 parent ba2b71e commit 90ef9e5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,40 @@
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class RegisterRequest {

@NotBlank(message = "이름을 입력해주세요.")
private final String name;
private String name;

@NotBlank(message = "전화번호를 입력해주세요.")
private final String phoneNumber;
private String phoneNumber;

@NotNull(message = "회원 유형을 입력해주세요.")
private final UserType userType;
private UserType userType;

@NotBlank(message = "이메일을 입력해주세요.")
private final String email;
private String email;

private final String signUpSource;
private String signUpSource;

private final StudentInfoDto studentInfo;
private StudentInfoDto studentInfo;

private String division;

private String position;

public RegisterRequest(String name, String phoneNumber, UserType userType, String email, String signUpSource,
StudentInfoDto studentInfo) {
StudentInfoDto studentInfo, String division, String position) {
validateStudentInfo(userType, studentInfo);
this.name = name;
this.phoneNumber = phoneNumber;
this.userType = userType;
this.email = email;
this.signUpSource = signUpSource;
this.studentInfo = studentInfo;
if (division != null) this.division = division;
if (position != null) this.position = position;
}

private void validateStudentInfo(UserType userType, StudentInfoDto studentInfo) {
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/com/scg/stop/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
import com.scg.stop.global.exception.BadRequestException;
import com.scg.stop.global.exception.ExceptionCode;
import com.scg.stop.global.exception.InvalidJwtException;
import com.scg.stop.user.domain.Application;
import com.scg.stop.user.domain.Department;
import com.scg.stop.user.domain.Student;
import com.scg.stop.user.domain.User;
import com.scg.stop.user.domain.UserType;
import com.scg.stop.user.repository.ApplicationRepository;
import com.scg.stop.user.repository.DepartmentRepository;
import com.scg.stop.user.repository.StudentRepository;
import com.scg.stop.user.repository.UserRepository;
import java.util.Arrays;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -30,6 +33,7 @@ public class AuthService {
private final StudentRepository studentRepository;
private final DepartmentRepository departmentRepository;
private final UserRepository userRepository;
private final ApplicationRepository applicationRepository;
private final JwtUtil jwtUtil;
private final KakaoOAuthProvider kakaoOAuthProvider;

Expand All @@ -52,24 +56,28 @@ private User findOrCreateUser(String socialLoginId) {
}

public RegisterResponse finishRegister(User user, RegisterRequest registerRequest) {
User foundUser = userRepository.findById(user.getId())
.orElseThrow(() -> new BadRequestException(ExceptionCode.NOT_FOUND_USER_ID));
if (foundUser.getUserType().equals(UserType.STUDENT)) {
if (user.getUserType().equals(UserType.STUDENT)) {
Department department = departmentRepository.findByName(
registerRequest.getStudentInfo().getDepartment())
.orElseThrow(() -> new BadRequestException(ExceptionCode.NOT_FOUND_DEPARTMENT));//TODO: department가 null인경우 예외처리
.orElseThrow(() -> new BadRequestException(ExceptionCode.NOT_FOUND_DEPARTMENT));

Student student = Student.of(registerRequest.getStudentInfo().getStudentNumber(),
foundUser
user
, department);
studentRepository.save(student);
}
foundUser.register(registerRequest.getName(),
else if (Arrays.asList(UserType.INACTIVE_PROFESSOR, UserType.COMPANY, UserType.INACTIVE_COMPANY, UserType.PROFESSOR)
.contains(user.getUserType())) {
Application application = new Application(registerRequest.getDivision(), registerRequest.getPosition(),
user);
applicationRepository.save(application);
}
user.register(registerRequest.getName(),
registerRequest.getEmail(),
registerRequest.getPhoneNumber(),
registerRequest.getUserType(),
registerRequest.getSignUpSource());
return RegisterResponse.from(foundUser);
return RegisterResponse.from(user);
}
private User createUser(String socialLoginId) {
return userRepository.save(new User(socialLoginId));
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/scg/stop/user/domain/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,31 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = PROTECTED)
@AllArgsConstructor
public class Application extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

private String division;
private String division; //소속

private String position;
private String position; // 직책

@OneToOne(fetch = LAZY)
@JoinColumn(name = "user_id")
private User user;

public Application(String division, String position, User user) {
this.division = division;
this.position = position;
this.user = user;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.scg.stop.user.repository;

import com.scg.stop.user.domain.Application;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ApplicationRepository extends JpaRepository<Application, Long> {
void findByName();
}

0 comments on commit 90ef9e5

Please sign in to comment.