From 95734bd3e4ee30d067e3d5cf6b491760fd0133b6 Mon Sep 17 00:00:00 2001 From: HeeSung98 Date: Wed, 6 Dec 2023 11:03:10 +0900 Subject: [PATCH] =?UTF-8?q?google=20=EC=9C=A0=EC=A0=80=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=EC=A0=95=EB=B3=B4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/UserController.java | 12 ++++++-- .../java/com/weather/user/dto/UserDTO.java | 2 +- .../java/com/weather/user/entity/User.java | 6 ++-- .../com/weather/user/service/UserService.java | 4 +-- .../weather/user/service/UserServiceImpl.java | 29 +++++++++++++++---- 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/user/src/main/java/com/weather/user/controller/UserController.java b/user/src/main/java/com/weather/user/controller/UserController.java index 227c914..dd07b42 100644 --- a/user/src/main/java/com/weather/user/controller/UserController.java +++ b/user/src/main/java/com/weather/user/controller/UserController.java @@ -1,9 +1,7 @@ package com.weather.user.controller; import com.weather.user.dto.GoogleUserDTO; -import com.weather.user.dto.MailCodeDTO; import com.weather.user.dto.UserDTO; -import com.weather.user.service.MailService; import com.weather.user.service.UserService; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; @@ -31,6 +29,16 @@ public ResponseEntity google(@RequestBody GoogleUserDTO googleUserDTO) return new ResponseEntity<>(result, HttpStatus.OK); } + @PostMapping("/login/google/additional") + public ResponseEntity googleAdditional(@RequestBody UserDTO userDTO) throws Exception { + log.info(" ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ google controller ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ"); + log.info("userDTO: " + userDTO); + + UserDTO result = userService.googleUserAdditional(userDTO); + + return new ResponseEntity<>(result, HttpStatus.OK); + } + @PostMapping("/signup/email") public ResponseEntity verifyEmail(@RequestBody UserDTO userDTO) { log.info(" ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ verifyEmail controller ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ"); diff --git a/user/src/main/java/com/weather/user/dto/UserDTO.java b/user/src/main/java/com/weather/user/dto/UserDTO.java index a020bcd..08d446b 100644 --- a/user/src/main/java/com/weather/user/dto/UserDTO.java +++ b/user/src/main/java/com/weather/user/dto/UserDTO.java @@ -9,7 +9,7 @@ public class UserDTO { private int id; - private String email, name, nickname, phone, image, password, token; + private String email, name, nickname, image, password, token; private boolean fromSocial, status; } diff --git a/user/src/main/java/com/weather/user/entity/User.java b/user/src/main/java/com/weather/user/entity/User.java index fc091f9..c819402 100644 --- a/user/src/main/java/com/weather/user/entity/User.java +++ b/user/src/main/java/com/weather/user/entity/User.java @@ -33,9 +33,9 @@ public void addRole(UserRole role) { roleSet.add(role); } - public void changeNickname(String nickname) { - this.nickname = nickname; - } + public void changeNickname(String nickname) { this.nickname = nickname; } + + public void changeName(String name) { this.name = name; } public void changeImage(String image) { this.image = image; diff --git a/user/src/main/java/com/weather/user/service/UserService.java b/user/src/main/java/com/weather/user/service/UserService.java index 4819406..1dc8620 100644 --- a/user/src/main/java/com/weather/user/service/UserService.java +++ b/user/src/main/java/com/weather/user/service/UserService.java @@ -22,13 +22,14 @@ public interface UserService { UserDTO googleUserCheck(GoogleUserDTO googleUserDTO); + UserDTO googleUserAdditional(UserDTO userDTO) throws Exception; + default User dtoToEntity(UserDTO userDTO) { User user = User.builder() .email(userDTO.getEmail()) .name(userDTO.getName()) .nickname(userDTO.getNickname()) .password(userDTO.getPassword()) - .phone(userDTO.getPhone()) .image(userDTO.getImage()) .fromSocial(userDTO.isFromSocial()) .status(userDTO.isStatus()).build(); @@ -43,7 +44,6 @@ default UserDTO entityToDTO(User user) { .name(user.getName()) .nickname(user.getNickname()) .password(user.getPassword()) - .phone(user.getPhone()) .image(user.getImage()) .fromSocial(user.isFromSocial()) .status(user.isStatus()).build(); diff --git a/user/src/main/java/com/weather/user/service/UserServiceImpl.java b/user/src/main/java/com/weather/user/service/UserServiceImpl.java index fca7a80..f46046d 100644 --- a/user/src/main/java/com/weather/user/service/UserServiceImpl.java +++ b/user/src/main/java/com/weather/user/service/UserServiceImpl.java @@ -139,23 +139,40 @@ public UserDTO googleUserCheck(GoogleUserDTO googleUserDTO) { Optional optionalUser = userRepository.findByEmail(email, true); if(optionalUser.isPresent()) { - User socialUser = optionalUser.get(); - UserDTO result = entityToDTO(socialUser); + User user = optionalUser.get(); + UserDTO result = entityToDTO(user); return result; } else { - User socialUser = User.builder() + User user = User.builder() .email(email) .password(passwordEncoder.encode("1234")) .image(image) .fromSocial(true) .status(true) .build(); - socialUser.addRole(UserRole.USER); + user.addRole(UserRole.USER); + + userRepository.save(user); + + UserDTO result = entityToDTO(user); + return result; + } + } - userRepository.save(socialUser); + @Override + public UserDTO googleUserAdditional(UserDTO userDTO) throws Exception{ + log.info("ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ"); + Optional optionalUser = userRepository.findByEmail(userDTO.getEmail(), true); - UserDTO result = entityToDTO(socialUser); + if(optionalUser.isPresent()) { + User user = optionalUser.get(); + user.changeNickname(userDTO.getNickname()); + user.changeName(userDTO.getName()); + userRepository.save(user); + UserDTO result = entityToDTO(user); return result; + } else { + throw new Exception("잘못 된 접근입니다."); } } }