From 2a47dda1a3f1ca76de31473503f298998ce9c8c5 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Wed, 1 Nov 2023 22:23:15 +0900 Subject: [PATCH] =?UTF-8?q?NABI-105--feat=20:=20=EC=99=B8=EB=B6=80api?= =?UTF-8?q?=EB=A5=BC=20=EC=9D=B4=EC=9A=A9=ED=95=9C=20=EB=8B=89=EB=84=A4?= =?UTF-8?q?=EC=9E=84=20=EB=9E=9C=EB=8D=A4=EC=83=9D=EC=84=B1=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/service/RandomNicknameGenerator.java | 39 +++++++++++++++++++ .../domain/user/service/SignService.java | 12 +++--- .../google/service/GoogleOAuth2Service.java | 9 ++++- 3 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/user/service/RandomNicknameGenerator.java diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/user/service/RandomNicknameGenerator.java b/src/main/java/org/prgrms/nabimarketbe/domain/user/service/RandomNicknameGenerator.java new file mode 100644 index 00000000..6a74df7d --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/user/service/RandomNicknameGenerator.java @@ -0,0 +1,39 @@ +package org.prgrms.nabimarketbe.domain.user.service; + +import java.util.List; +import java.util.Map; + +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponents; +import org.springframework.web.util.UriComponentsBuilder; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Component +public class RandomNicknameGenerator { + private static final String GENERATOR_URL = "https://nickname.hwanmoo.kr/?format=json&count=1"; + + private static final int NICKNAME_MAX_LENGTH = 6; + + private final RestTemplate restTemplate; + + private final ObjectMapper objectMapper; + + public String generateRandomNickname() throws JsonProcessingException { + UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(GENERATOR_URL) + .queryParam("max_length", NICKNAME_MAX_LENGTH).build(); + + String apiResult = restTemplate.getForObject(uriComponents.toString(), String.class); + Map wordMap = objectMapper.readValue(apiResult, Map.class); + + List randomNicknames = (List) wordMap.get("words"); + String randomNickname = randomNicknames.get(0); + + return randomNickname; + } +} diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/user/service/SignService.java b/src/main/java/org/prgrms/nabimarketbe/domain/user/service/SignService.java index 32d24388..fd00cf6a 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/user/service/SignService.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/user/service/SignService.java @@ -19,6 +19,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.fasterxml.jackson.core.JsonProcessingException; + import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -36,6 +38,8 @@ public class SignService { private final RefreshTokenJpaRepo tokenJpaRepo; + private final RandomNicknameGenerator randomNicknameGenerator; + @Transactional public CommonResult signupBySocial(String accessToken) { KakaoProfile kakaoProfile = oAuth2Service.getKakaoProfile(accessToken); @@ -97,13 +101,11 @@ public TokenResponseDTO reissue(TokenRequestDto tokenRequestDto) { } @Transactional - public User signUp(GoogleUserInfoDTO googleUserInfoDTO) { - User user = googleUserInfoDTO.toEntity("randomNickName"); + public User signUp(GoogleUserInfoDTO googleUserInfoDTO) throws JsonProcessingException { + String randomNickname = randomNicknameGenerator.generateRandomNickname(); + User user = googleUserInfoDTO.toEntity(randomNickname); User savedUser = userJpaRepo.save(user); - // TODO : 닉네임 랜덤 생성 - log.info("modified date : {}", savedUser.getModifiedDate()); - return savedUser; } } diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/service/GoogleOAuth2Service.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/service/GoogleOAuth2Service.java index ccd52b95..94dcf22c 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/service/GoogleOAuth2Service.java +++ b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/service/GoogleOAuth2Service.java @@ -43,7 +43,14 @@ public UserLoginResponseDTO OAuth2Login(String code) throws JsonProcessingExcept Optional optionalUser = userRepository.findByNameAttributeKey(nameAttributeKey); - User user = optionalUser.orElseGet(() -> signService.signUp(googleUserInfoDTO)); + User user = optionalUser.orElseGet(() -> { + try { + return signService.signUp(googleUserInfoDTO); + } catch (JsonProcessingException e) { + throw new RuntimeException("json parse failed"); + } + }); + TokenResponseDTO tokenResponseDTO = jwtProvider.createTokenDto(user.getUserId(), user.getRoles()); UserLoginResponseDTO response = UserLoginResponseDTO.from(user, tokenResponseDTO);