Skip to content

Commit

Permalink
NABI-105--feat : 외부api를 이용한 닉네임 랜덤생성 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
BeommoKoo-dev committed Nov 1, 2023
1 parent 7e4fad9 commit 2a47dda
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, Object> wordMap = objectMapper.readValue(apiResult, Map.class);

List<String> randomNicknames = (List<String>) wordMap.get("words");
String randomNickname = randomNicknames.get(0);

return randomNickname;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ public UserLoginResponseDTO OAuth2Login(String code) throws JsonProcessingExcept

Optional<User> 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);
Expand Down

0 comments on commit 2a47dda

Please sign in to comment.