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 e5a99bfd..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 @@ -1,9 +1,14 @@ 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.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; @@ -24,12 +29,24 @@ public class UserService { private final JwtTokenProvider jwtTokenProvider; 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( @@ -44,14 +61,23 @@ 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("testImage") + .build(); + return guestRepository.save(guest); } public User getBySocialId( @@ -77,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 ); @@ -107,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)); + } }