From 6ddb08471126f9877295739a394725ac0c2a357d Mon Sep 17 00:00:00 2001 From: bo-ram-bo-ram Date: Fri, 12 Jul 2024 23:35:33 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat=20:=20user=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=EC=8B=9C=ED=82=AC=20=EB=95=8C=20guest=EB=8F=84=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/api/user/service/UserService.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/java/com/pickple/server/api/user/service/UserService.java b/src/main/java/com/pickple/server/api/user/service/UserService.java index e5a99bfd..ea8ccc91 100644 --- a/src/main/java/com/pickple/server/api/user/service/UserService.java +++ b/src/main/java/com/pickple/server/api/user/service/UserService.java @@ -1,5 +1,7 @@ package com.pickple.server.api.user.service; +import com.pickple.server.api.guest.domain.Guest; +import com.pickple.server.api.guest.repository.GuestRepository; import com.pickple.server.api.user.domain.SocialType; import com.pickple.server.api.user.domain.User; import com.pickple.server.api.user.dto.AccessTokenGetSuccess; @@ -24,6 +26,7 @@ public class UserService { private final JwtTokenProvider jwtTokenProvider; private final TokenService tokenService; private final KakaoSocialService kakaoSocialService; + private final GuestRepository guestRepository; // 의존성 분리하기 public LoginSuccessResponse create( final String authorizationCode, @@ -54,6 +57,16 @@ public Long createUser(final UserInfoResponse userResponse) { return userRepository.save(user).getId(); } + public Guest createGuest(final User user) { + Guest guest = Guest.builder() + .user(user) + .nickname(user.getSocialNickname() + "#" + user.getId()) + .imageUrl("test") + .build(); + + return guestRepository.save(guest); + } + public User getBySocialId( final Long socialId, final SocialType socialType From 58ff0d779d7f35a0933ca12ae4c73d0b833e402a Mon Sep 17 00:00:00 2001 From: bo-ram-bo-ram Date: Fri, 12 Jul 2024 23:48:18 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat=20:=20=ED=9A=8C=EC=9B=90=EA=B0=80?= =?UTF-8?q?=EC=9E=85=20=EC=8B=9C=20=EC=9D=91=EB=8B=B5=EA=B0=92=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 --- .../api/guest/repository/GuestRepository.java | 2 + .../api/user/dto/LoginSuccessResponse.java | 16 +++-- .../pickple/server/api/user/dto/TokenDto.java | 11 ++++ .../server/api/user/service/UserService.java | 63 +++++++++++++------ 4 files changed, 68 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/pickple/server/api/user/dto/TokenDto.java diff --git a/src/main/java/com/pickple/server/api/guest/repository/GuestRepository.java b/src/main/java/com/pickple/server/api/guest/repository/GuestRepository.java index 70846364..7ff39f56 100644 --- a/src/main/java/com/pickple/server/api/guest/repository/GuestRepository.java +++ b/src/main/java/com/pickple/server/api/guest/repository/GuestRepository.java @@ -12,6 +12,8 @@ public interface GuestRepository extends JpaRepository { Guest findGuestByUserId(Long userId); + Optional findByUserId(Long userId); + default Guest findGuestByIdOrThrow(Long id) { return findGuestById(id) .orElseThrow(() -> new CustomException(ErrorCode.GUEST_NOT_FOUND)); diff --git a/src/main/java/com/pickple/server/api/user/dto/LoginSuccessResponse.java b/src/main/java/com/pickple/server/api/user/dto/LoginSuccessResponse.java index b58e8857..7e88c59c 100644 --- a/src/main/java/com/pickple/server/api/user/dto/LoginSuccessResponse.java +++ b/src/main/java/com/pickple/server/api/user/dto/LoginSuccessResponse.java @@ -1,13 +1,19 @@ package com.pickple.server.api.user.dto; public record LoginSuccessResponse( - String accessToken, - String refreshToken + String guestNickname, + Long guestId, + String hostNickname, + Long hostId, + TokenDto token ) { public static LoginSuccessResponse of( - final String accessToken, - final String refreshToken + final String guestNickname, + final Long guestId, + final String hostNickname, + final Long hostId, + final TokenDto token ) { - return new LoginSuccessResponse(accessToken, refreshToken); + return new LoginSuccessResponse(guestNickname, guestId, hostNickname, hostId, token); } } \ No newline at end of file diff --git a/src/main/java/com/pickple/server/api/user/dto/TokenDto.java b/src/main/java/com/pickple/server/api/user/dto/TokenDto.java new file mode 100644 index 00000000..918d8393 --- /dev/null +++ b/src/main/java/com/pickple/server/api/user/dto/TokenDto.java @@ -0,0 +1,11 @@ +package com.pickple.server.api.user.dto; + +public record TokenDto( + String accessToken, + String refreshToken +) { + + public static TokenDto of(String accessToken, String refreshToken) { + return new TokenDto(accessToken, refreshToken); + } +} diff --git a/src/main/java/com/pickple/server/api/user/service/UserService.java b/src/main/java/com/pickple/server/api/user/service/UserService.java index ea8ccc91..85d35b57 100644 --- a/src/main/java/com/pickple/server/api/user/service/UserService.java +++ b/src/main/java/com/pickple/server/api/user/service/UserService.java @@ -2,10 +2,13 @@ import com.pickple.server.api.guest.domain.Guest; import com.pickple.server.api.guest.repository.GuestRepository; +import com.pickple.server.api.host.domain.Host; +import com.pickple.server.api.host.repository.HostRepository; import com.pickple.server.api.user.domain.SocialType; import com.pickple.server.api.user.domain.User; import com.pickple.server.api.user.dto.AccessTokenGetSuccess; import com.pickple.server.api.user.dto.LoginSuccessResponse; +import com.pickple.server.api.user.dto.TokenDto; import com.pickple.server.api.user.repository.UserRepository; import com.pickple.server.global.auth.client.dto.UserInfoResponse; import com.pickple.server.global.auth.client.dto.UserLoginRequest; @@ -27,12 +30,23 @@ public class UserService { private final TokenService tokenService; private final KakaoSocialService kakaoSocialService; private final GuestRepository guestRepository; // 의존성 분리하기 + private final HostRepository hostRepository; // 의존성 분리하기 public LoginSuccessResponse create( final String authorizationCode, final UserLoginRequest loginRequest ) { - return getTokenDto(getUserInfoResponse(authorizationCode, loginRequest)); + User user = getUser(getUserInfoResponse(authorizationCode, loginRequest)); + Guest guest = getOrCreateGuest(user); + TokenDto tokenDto = getTokenDto(user); + + if (isExistingHost(user.getId())) { + Host host = hostRepository.findHostByUserId(user.getId()); + return LoginSuccessResponse.of(guest.getNickname(), guest.getId(), + host.getNickname(), host.getId(), tokenDto); + } else { + return LoginSuccessResponse.of(guest.getNickname(), guest.getId(), null, null, tokenDto); + } } public UserInfoResponse getUserInfoResponse( @@ -47,23 +61,22 @@ public UserInfoResponse getUserInfoResponse( } } - public Long createUser(final UserInfoResponse userResponse) { + public User createUser(final UserInfoResponse userResponse) { User user = User.of( userResponse.socialId(), userResponse.email(), userResponse.socialType(), userResponse.socialNickname() ); - return userRepository.save(user).getId(); + return userRepository.save(user); } public Guest createGuest(final User user) { Guest guest = Guest.builder() .user(user) .nickname(user.getSocialNickname() + "#" + user.getId()) - .imageUrl("test") + .imageUrl("testImage") .build(); - return guestRepository.save(guest); } @@ -90,20 +103,13 @@ public AccessTokenGetSuccess refreshToken( ); } - public boolean isExistingUser( - final Long socialId, - final SocialType socialType - ) { - return userRepository.findBySocialTypeAndSocialId(socialId, socialType).isPresent(); - } - - public LoginSuccessResponse getTokenByUserId( + public TokenDto getTokenByUserId( final Long id ) { UserAuthentication userAuthentication = new UserAuthentication(id, null, null); String refreshToken = jwtTokenProvider.issueRefreshToken(userAuthentication); tokenService.saveRefreshToken(id, refreshToken); - return LoginSuccessResponse.of( + return TokenDto.of( jwtTokenProvider.issueAccessToken(userAuthentication), refreshToken ); @@ -120,14 +126,33 @@ public void deleteUser( userRepository.delete(user); } - private LoginSuccessResponse getTokenDto( - final UserInfoResponse userResponse + private TokenDto getTokenDto( + final User user ) { + return getTokenByUserId(user.getId()); + } + + private User getUser(final UserInfoResponse userResponse) { if (isExistingUser(userResponse.socialId(), userResponse.socialType())) { - return getTokenByUserId(getBySocialId(userResponse.socialId(), userResponse.socialType()).getId()); + return getBySocialId(userResponse.socialId(), userResponse.socialType()); } else { - Long id = createUser(userResponse); - return getTokenByUserId(id); + return createUser(userResponse); } } + + private boolean isExistingUser( + final Long socialId, + final SocialType socialType + ) { + return userRepository.findBySocialTypeAndSocialId(socialId, socialType).isPresent(); + } + + private boolean isExistingHost(final Long userId) { + return hostRepository.existsById(userId); + } + + private Guest getOrCreateGuest(final User user) { + return guestRepository.findByUserId(user.getId()) + .orElseGet(() -> createGuest(user)); + } }