From c032890c954f3eec8e3b1e93bc3bf39b25945da9 Mon Sep 17 00:00:00 2001 From: born-A <93516595+born-A@users.noreply.github.com> Date: Tue, 7 Nov 2023 18:26:36 +0900 Subject: [PATCH 1/7] =?UTF-8?q?NO-JIRA-refactor=20:=20=EC=B9=B4=EC=B9=B4?= =?UTF-8?q?=EC=98=A4=20-=20user=20=EC=A0=95=EB=B3=B4=20=EB=82=B4=EB=A0=A4?= =?UTF-8?q?=EC=A3=BC=EB=8A=94=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/user/service/SignService.java | 41 ++++++++++--------- .../oauth2/kakao/api/OAuth2Controller.java | 14 ++----- .../oauth2/kakao/service/OAuth2Service.java | 2 + 3 files changed, 27 insertions(+), 30 deletions(-) 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 4480362b..78cd2856 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 @@ -8,8 +8,6 @@ import org.prgrms.nabimarketbe.domain.user.repository.UserRepository; import org.prgrms.nabimarketbe.global.security.jwt.dto.TokenDTO; import org.prgrms.nabimarketbe.global.security.jwt.provider.JwtProvider; -import org.prgrms.nabimarketbe.global.util.ResponseFactory; -import org.prgrms.nabimarketbe.global.util.model.CommonResult; import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleUserInfoDTO; import org.prgrms.nabimarketbe.oauth2.kakao.dto.KakaoProfile; import org.springframework.stereotype.Service; @@ -31,30 +29,33 @@ public class SignService { private final RandomNicknameGenerator randomNicknameGenerator; @Transactional - public CommonResult signInBySocial(KakaoProfile kakaoProfile) { - CommonResult result = signIn(UserSignInRequestDTO.builder() + public UserLoginResponseDTO signInBySocial(KakaoProfile kakaoProfile) { + UserSignInRequestDTO userSignInRequestDTO = UserSignInRequestDTO.builder() .accountId(kakaoProfile.getId()) .nickname(kakaoProfile.getProperties().getNickname()) .provider("kakao") - .build()); + .build(); - return ResponseFactory.getSingleResult(result); + UserLoginResponseDTO result = signIn(userSignInRequestDTO); + + return result; } + @Transactional - public CommonResult signIn(UserSignInRequestDTO userSignInRequestDTO) { - Optional user = userRepository.findByAccountIdAndProvider( - userSignInRequestDTO.accountId(), - userSignInRequestDTO.provider() - ); - - if (user.isPresent()) { - return ResponseFactory.getSingleResult(jwtProvider.createTokenDTO( - user.get().getUserId(), user.get().getRole()) - ); - } - - User savedUser = userRepository.save(userSignInRequestDTO.toEntity()); - return ResponseFactory.getSingleResult(jwtProvider.createTokenDTO(savedUser.getUserId(), savedUser.getRole())); + public UserLoginResponseDTO signIn(UserSignInRequestDTO userSignInRequestDTO) { + String accountId = userSignInRequestDTO.accountId(); + + Optional optionalUser = userRepository.findByAccountId(accountId); + + User user = optionalUser.orElseGet(() -> { + return userRepository.save(userSignInRequestDTO.toEntity()); + }); + + TokenDTO tokenDTO = jwtProvider.createTokenDTO(user.getUserId(), user.getRole()); + + UserLoginResponseDTO response = UserLoginResponseDTO.of(user, tokenDTO); + + return response; } @Transactional diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/api/OAuth2Controller.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/api/OAuth2Controller.java index 37163673..58df02f3 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/api/OAuth2Controller.java +++ b/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/api/OAuth2Controller.java @@ -4,22 +4,16 @@ import javax.servlet.http.HttpServletResponse; +import org.prgrms.nabimarketbe.domain.user.dto.response.UserLoginResponseDTO; import org.prgrms.nabimarketbe.domain.user.service.SignService; import org.prgrms.nabimarketbe.global.util.ResponseFactory; import org.prgrms.nabimarketbe.global.util.model.CommonResult; import org.prgrms.nabimarketbe.oauth2.kakao.dto.KakaoProfile; import org.prgrms.nabimarketbe.oauth2.kakao.service.OAuth2Service; import org.springframework.core.env.Environment; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; +import org.springframework.http.*; import org.springframework.util.MultiValueMap; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import lombok.RequiredArgsConstructor; @@ -45,7 +39,7 @@ public void socialLogin(HttpServletResponse response) throws IOException { } @GetMapping(value = "/redirect") - public CommonResult redirectKakao(@RequestParam String code) { + public UserLoginResponseDTO redirectKakao(@RequestParam String code) { KakaoProfile profile = OAuth2Service.getResultProfile(code); if (profile == null) throw new RuntimeException("카카오에 해당 회원이 없습니다."); diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/service/OAuth2Service.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/service/OAuth2Service.java index 04db56b5..93be90d3 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/service/OAuth2Service.java +++ b/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/service/OAuth2Service.java @@ -1,8 +1,10 @@ package org.prgrms.nabimarketbe.oauth2.kakao.service; import com.google.gson.Gson; + import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; + import org.prgrms.nabimarketbe.oauth2.kakao.dto.KakaoProfile; import org.prgrms.nabimarketbe.oauth2.kakao.dto.RetKakaoOAuth; import org.springframework.beans.factory.annotation.Value; From b478c1448aca7181c370dc882f21c6a70b564f7a Mon Sep 17 00:00:00 2001 From: born-A <93516595+born-A@users.noreply.github.com> Date: Tue, 7 Nov 2023 18:26:58 +0900 Subject: [PATCH 2/7] =?UTF-8?q?NO-JIRA-refactor=20:=20@ValidEnum=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/prgrms/nabimarketbe/domain/card/entity/Card.java | 2 -- .../java/org/prgrms/nabimarketbe/domain/item/entity/Item.java | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java index fccb4de5..9e28c928 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java @@ -46,12 +46,10 @@ public class Card extends BaseEntity { @Column(name = "poke", nullable = false) private Boolean poke; - @ValidEnum(enumClass = TradeType.class, message = "유효하지 않은 거래 방식입니다.") @Enumerated(EnumType.STRING) @Column(name = "trade_type", nullable = false) private TradeType tradeType; - @ValidEnum(enumClass = CardStatus.class, message = "유효하지 않은 카드 상태입니다.") @Enumerated(EnumType.STRING) @Column(name = "status", nullable = false) private CardStatus status; diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/item/entity/Item.java b/src/main/java/org/prgrms/nabimarketbe/domain/item/entity/Item.java index af7c9593..f9b5d0a6 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/item/entity/Item.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/item/entity/Item.java @@ -23,11 +23,9 @@ public class Item extends BaseEntity { @Column(name = "item_id", nullable = false) private Long itemId; - @NotBlank(message = "공백을 허용하지 않습니다.") @Column(name = "item_name", nullable = false) private String itemName; -// @ValidEnum(enumClass = PriceRange.class, message = "유효하지 않은 가격대입니다.") @Enumerated(EnumType.STRING) @Column(name = "price_range", nullable = false) private PriceRange priceRange; From ca5152d911559ea59a071922bfe950d8e4f81bc2 Mon Sep 17 00:00:00 2001 From: born-A <93516595+born-A@users.noreply.github.com> Date: Tue, 7 Nov 2023 19:05:33 +0900 Subject: [PATCH 3/7] =?UTF-8?q?NO-JIRA-refactor=20:=20response=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nabimarketbe/oauth2/kakao/api/OAuth2Controller.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/api/OAuth2Controller.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/api/OAuth2Controller.java index 58df02f3..8976e09b 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/api/OAuth2Controller.java +++ b/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/api/OAuth2Controller.java @@ -8,6 +8,7 @@ import org.prgrms.nabimarketbe.domain.user.service.SignService; import org.prgrms.nabimarketbe.global.util.ResponseFactory; import org.prgrms.nabimarketbe.global.util.model.CommonResult; +import org.prgrms.nabimarketbe.global.util.model.SingleResult; import org.prgrms.nabimarketbe.oauth2.kakao.dto.KakaoProfile; import org.prgrms.nabimarketbe.oauth2.kakao.service.OAuth2Service; import org.springframework.core.env.Environment; @@ -39,11 +40,13 @@ public void socialLogin(HttpServletResponse response) throws IOException { } @GetMapping(value = "/redirect") - public UserLoginResponseDTO redirectKakao(@RequestParam String code) { + public ResponseEntity> redirectKakao(@RequestParam String code) { KakaoProfile profile = OAuth2Service.getResultProfile(code); if (profile == null) throw new RuntimeException("카카오에 해당 회원이 없습니다."); + UserLoginResponseDTO userLoginResponseDTO = signService.signInBySocial(profile); + SingleResult response = ResponseFactory.getSingleResult(userLoginResponseDTO); - return signService.signInBySocial(profile); + return ResponseEntity.ok(response); } //TODO : 사용자가 accessToken 넘기는건 아닌거 같음 From deb447f1b9827483a6deeebdeef9b2c6729791d4 Mon Sep 17 00:00:00 2001 From: born-A <93516595+born-A@users.noreply.github.com> Date: Tue, 7 Nov 2023 21:58:31 +0900 Subject: [PATCH 4/7] NO-JIRA-refactor : sign up method overriding --- .../user/dto/request/UserSignInRequestDTO.java | 9 +++++++++ .../domain/user/service/SignService.java | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/request/UserSignInRequestDTO.java b/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/request/UserSignInRequestDTO.java index a2a75a40..49f91ae8 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/request/UserSignInRequestDTO.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/request/UserSignInRequestDTO.java @@ -19,5 +19,14 @@ public User toEntity() { .role(Role.USER.getKey()) .build(); } + + public User toEntity(String nickName) { + return User.builder() + .accountId(accountId) + .nickname(nickName) + .provider(provider) + .role(Role.USER.getKey()) + .build(); + } } 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 78cd2856..fd538fe8 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 @@ -48,7 +48,11 @@ public UserLoginResponseDTO signIn(UserSignInRequestDTO userSignInRequestDTO) { Optional optionalUser = userRepository.findByAccountId(accountId); User user = optionalUser.orElseGet(() -> { - return userRepository.save(userSignInRequestDTO.toEntity()); + try { + return signUp(userSignInRequestDTO); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } }); TokenDTO tokenDTO = jwtProvider.createTokenDTO(user.getUserId(), user.getRole()); @@ -86,4 +90,13 @@ public User signUp(GoogleUserInfoDTO googleUserInfoDTO) throws JsonProcessingExc return savedUser; } + + @Transactional + public User signUp(UserSignInRequestDTO userSignInRequestDTO) throws JsonProcessingException { + String randomNickname = randomNicknameGenerator.generateRandomNickname(); + User user = userSignInRequestDTO.toEntity(randomNickname); + User savedUser = userRepository.save(user); + + return savedUser; + } } From 23d2b87950a264c6e2aebfeedb76c62967b9c830 Mon Sep 17 00:00:00 2001 From: born-A <93516595+born-A@users.noreply.github.com> Date: Tue, 7 Nov 2023 21:59:03 +0900 Subject: [PATCH 5/7] =?UTF-8?q?NO-JIRA-refactor=20:=20validenum=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/prgrms/nabimarketbe/domain/card/entity/Card.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java index 68ef50d7..dbf4bd69 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java @@ -6,7 +6,6 @@ import lombok.NoArgsConstructor; import org.prgrms.nabimarketbe.global.BaseEntity; import org.prgrms.nabimarketbe.domain.item.entity.Item; -import org.prgrms.nabimarketbe.global.annotation.ValidEnum; import org.prgrms.nabimarketbe.global.error.BaseException; import org.prgrms.nabimarketbe.global.error.ErrorCode; From 5f2c58907735d2f139d3e2b98941dbd16e38d102 Mon Sep 17 00:00:00 2001 From: born-A <93516595+born-A@users.noreply.github.com> Date: Tue, 7 Nov 2023 21:59:31 +0900 Subject: [PATCH 6/7] =?UTF-8?q?NO-JIRA-refactor=20:=20=EA=B0=9C=ED=96=89?= =?UTF-8?q?=20=EC=BB=A8=EB=B2=A4=EC=85=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/prgrms/nabimarketbe/global/config/WebConfig.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/global/config/WebConfig.java b/src/main/java/org/prgrms/nabimarketbe/global/config/WebConfig.java index c8344832..8075da3b 100644 --- a/src/main/java/org/prgrms/nabimarketbe/global/config/WebConfig.java +++ b/src/main/java/org/prgrms/nabimarketbe/global/config/WebConfig.java @@ -6,7 +6,6 @@ @Configuration public class WebConfig { - @Bean public RestTemplate restTemplate() { return new RestTemplate(); From 11945dfd8cdecf7cb74dba225ed393efcf3280a5 Mon Sep 17 00:00:00 2001 From: born-A <93516595+born-A@users.noreply.github.com> Date: Tue, 7 Nov 2023 23:04:23 +0900 Subject: [PATCH 7/7] NO-JIRA-refactor --- src/main/resources/application.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 35dbdef5..f08c04e6 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,7 +1,6 @@ spring: profiles: - active: local, jwt - + active: local springdoc: version: '@springdoc.version@'