Skip to content

Commit

Permalink
Merge pull request #6 from KAKAO-TOUR-API-CONTEST/develop
Browse files Browse the repository at this point in the history
FIX : segregate sign up service
  • Loading branch information
femmefatalehaein authored Aug 15, 2024
2 parents 68c9741 + e8c9786 commit 3764745
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
19 changes: 11 additions & 8 deletions src/main/java/com/example/ai_jeju/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,29 @@

import java.io.IOException;

//HTTP 요청을 처리하는 Controller 클래스

@RestController
@RequestMapping("/api")
@RequestMapping("/api/users")
public class UserController {

@Autowired
private UserService userService;



//회원가입
/*여기 확인해보자*/
@PostMapping("/signup")
@PostMapping("/register")
public void signUp(@RequestBody SignUpRequest signUpRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
//String response = userService.signUp(signUpRequest);
//System.out.println(response);
userService.signUp(signUpRequest,request,response);
}
@GetMapping("/checks")
public Object CheckIfUser(@RequestParam String email){
return userService.checkIfUser(email);
}


@GetMapping("/RandomList")
public Object GetRandomList(@RequestParam String email){
return userService.checkIfUser(email);
}

//탈퇴하기
@GetMapping("/withdraw")
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/example/ai_jeju/domain/Child.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public class Child {
private Boolean gender;

@Column
private long childProfile;
private String realtion;

@Column
private String phoneNum;

@Column
private long childProfile;
}
63 changes: 50 additions & 13 deletions src/main/java/com/example/ai_jeju/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,68 @@ public class UserService {
private RefreshTokenRepository refreshTokenRepository;


private SignUpHandler signUpHandler = new SignUpHandler();
//private SignUpHandler signUpHandler = new SignUpHandler();

public String signUp( SignUpRequest signUpRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
// DB에 회원이 있을 때 -> 기존 회원일 경우
public Object checkIfUser(String email){

System.out.println("회원가입 서비스 동작");
// 이미 가입한 회원인지 확인한다.
Optional<User> existingUserByEmail = userRepository.findByEmail(signUpRequest.getEmail());
Optional<User> existingUser = userRepository.findByEmail(email);

// DB에 회원이 있을 때 -> 기존 회원일 경우
/*--------------------------------------------------------------------------------------------------*/
if (existingUserByEmail.isPresent()) {
User user = this.findByEmail(signUpRequest.getEmail());
String token = tokenProvider.generateToken(user,ACCESS_TOKEN_DURATION);
//accessToken반환
//여기 부분 바꿔야함.
return token;
if (existingUser.isPresent()) {
User user = existingUser.get();
String accessToken = tokenProvider.generateToken(user,ACCESS_TOKEN_DURATION);
return accessToken;
}
/*--------------------------------------------------------------------------------------------------*/

// db에 회원정보 없음 -> 새로운 회원 추가
else{
return successHadler(request,response,signUpRequest);
return false;
}
}


public String signUp( SignUpRequest signUpRequest, HttpServletRequest request, HttpServletResponse response) {

String nick = signUpRequest.getNickname();
if(nick==null){
nick = new NickNameGenerator().getNickname();
}
// Save new user using builder pattern
User newUser = User.builder()
.name(signUpRequest.getName())
.nickname(nick)
.provider(signUpRequest.getProvider())
.email(signUpRequest.getEmail())
.snsprofile(signUpRequest.getProfile())
.provider(signUpRequest.getProvider())
.build();

String accessToken = tokenProvider.generateToken(newUser, REFRESH_TOKEN_DURATION);
/*-------------------------------------------*/
//동반아동
List<ChildRequest> childList = signUpRequest.getChild();
for(int i=0; i<childList.size(); i++){
Child child = Child.builder()
//유저 아이디의 값 그대로 주기.
.userId(newUser.getId())
.childName(childList.get(i).getChildName())
.birthDate(childList.get(i).getBirthDate())
.gender(childList.get(i).getGender())
.build();
childRepository.save(child);
}

userRepository.save(newUser);
String refresh_token = tokenProvider.generateToken(newUser, REFRESH_TOKEN_DURATION);

saveRefreshToken(newUser.getId(), refresh_token);
addRefreshTokenToCookie(request,response,refresh_token);
return accessToken;

}

public User findByEmail(String email){
return userRepository.findByEmail(email)
.orElseThrow(
Expand Down

0 comments on commit 3764745

Please sign in to comment.