Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

[BE] refactor: 회원조회할때 커버링 인덱스가 적용되도록 수정 #424

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.donggle.backend.application.client;

import org.donggle.backend.domain.oauth.SocialType;
import org.donggle.backend.infrastructure.oauth.kakao.dto.response.UserInfo;
import org.donggle.backend.infrastructure.oauth.kakao.dto.response.SocialUserInfo;

public interface LoginClient {
String createRedirectUri(final String redirectUri);

String requestToken(final String authCode, final String redirectUri);

UserInfo findUserInfo(final String accessToken);
SocialUserInfo findUserInfo(final String accessToken);

SocialType getSocialType();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.donggle.backend.application.repository;

import org.donggle.backend.application.repository.dto.MemberInfo;
import org.donggle.backend.domain.member.Member;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface MemberRepository extends JpaRepository<Member, Long> {
Optional<Member> findBySocialId(final Long socialId);
Optional<MemberInfo> findBySocialId(final Long socialId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.donggle.backend.application.repository.dto;

public record MemberInfo(
Long id,
Long socialId
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.RequiredArgsConstructor;
import org.donggle.backend.application.service.request.OAuthAccessTokenRequest;
import org.donggle.backend.domain.oauth.SocialType;
import org.donggle.backend.infrastructure.oauth.kakao.dto.response.UserInfo;
import org.donggle.backend.infrastructure.oauth.kakao.dto.response.SocialUserInfo;
import org.donggle.backend.ui.response.TokenResponse;
import org.springframework.stereotype.Service;

Expand All @@ -18,11 +18,11 @@ public String createAuthorizeRedirectUri(final String socialType, final String r
}

public TokenResponse login(final String socialType, final OAuthAccessTokenRequest request) {
final UserInfo userInfo = oauthClients.findUserInfo(
final SocialUserInfo socialUserInfo = oauthClients.findUserInfo(
SocialType.from(socialType),
request.code(),
request.redirect_uri());
return authService.login(userInfo);
return authService.login(socialUserInfo);
}

public void logout(final Long memberId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import org.donggle.backend.domain.category.Category;
import org.donggle.backend.domain.member.Member;
import org.donggle.backend.domain.member.MemberCredentials;
import org.donggle.backend.domain.member.MemberName;
import org.donggle.backend.exception.business.DuplicatedMemberException;
import org.donggle.backend.exception.notfound.MemberNotFoundException;
import org.donggle.backend.infrastructure.oauth.kakao.dto.response.UserInfo;
import org.donggle.backend.infrastructure.oauth.kakao.dto.response.SocialUserInfo;
import org.donggle.backend.ui.response.TokenResponse;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
Expand All @@ -28,26 +29,28 @@ public class AuthService {
private final MemberCredentialsRepository memberCredentialsRepository;
private final CategoryRepository categoryRepository;

public TokenResponse login(final UserInfo userInfo) {
final Member loginMember = memberRepository.findBySocialId(userInfo.socialId())
.orElseGet(() -> initializeMember(userInfo));
return createTokens(loginMember);

public TokenResponse login(final SocialUserInfo socialUserInfo) {
return memberRepository.findBySocialId(socialUserInfo.socialId())
.map(memberInfo -> new Member(memberInfo.id(), new MemberName(socialUserInfo.nickname()), memberInfo.socialId()))
.map(this::createTokens)
.orElse(createTokens(initializeMember(socialUserInfo)));
}

public void logout(final Long memberId) {
tokenRepository.deleteByMemberId(memberId);
}

private Member initializeMember(final UserInfo userInfo) {
Member member = userInfo.toMember();
private Member initializeMember(final SocialUserInfo socialUserInfo) {
Member member = socialUserInfo.toMember();
final Category basicCategory = Category.basic(member);
final MemberCredentials basic = MemberCredentials.basic(member);
try {
member = memberRepository.save(member);
categoryRepository.save(basicCategory);
memberCredentialsRepository.save(basic);
} catch (final DuplicateKeyException e) {
throw new DuplicatedMemberException(userInfo.socialType().name());
throw new DuplicatedMemberException(socialUserInfo.socialType().name());
}
return member;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.donggle.backend.application.client.LoginClient;
import org.donggle.backend.domain.oauth.SocialType;
import org.donggle.backend.infrastructure.oauth.kakao.dto.response.UserInfo;
import org.donggle.backend.infrastructure.oauth.kakao.dto.response.SocialUserInfo;

import java.util.EnumMap;
import java.util.Map;
Expand All @@ -23,7 +23,7 @@ public String redirectUri(final SocialType socialType, final String redirectUri)
return client.createRedirectUri(redirectUri);
}

public UserInfo findUserInfo(final SocialType socialType, final String code, final String redirectUri) {
public SocialUserInfo findUserInfo(final SocialType socialType, final String code, final String redirectUri) {
final LoginClient client = getClient(socialType);
final String accessToken = client.requestToken(code, redirectUri);
return client.findUserInfo(accessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ private Member(final MemberName memberName, final Long socialId) {
this.socialId = socialId;
}

public Member(final Long id, final MemberName memberName, final Long socialId) {
this.id = id;
this.memberName = memberName;
this.socialId = socialId;
}

public static Member of(final MemberName memberName, final Long socialId) {
return new Member(memberName, socialId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.RequiredArgsConstructor;
import org.donggle.backend.application.client.LoginClient;
import org.donggle.backend.domain.oauth.SocialType;
import org.donggle.backend.infrastructure.oauth.kakao.dto.response.UserInfo;
import org.donggle.backend.infrastructure.oauth.kakao.dto.response.SocialUserInfo;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -24,7 +24,7 @@ public String requestToken(final String authCode, final String redirectUri) {
}

@Override
public UserInfo findUserInfo(final String accessToken) {
public SocialUserInfo findUserInfo(final String accessToken) {
return userInfoClient.request(accessToken);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.donggle.backend.infrastructure.oauth.kakao;

import org.donggle.backend.infrastructure.oauth.kakao.dto.response.KakaoProfileResponse;
import org.donggle.backend.infrastructure.oauth.kakao.dto.response.UserInfo;
import org.donggle.backend.infrastructure.client.exception.ClientException;
import org.donggle.backend.infrastructure.oauth.kakao.dto.response.KakaoProfileResponse;
import org.donggle.backend.infrastructure.oauth.kakao.dto.response.SocialUserInfo;
import org.springframework.http.HttpStatusCode;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
Expand All @@ -22,7 +22,7 @@ public KakaoLoginUserInfoClient() {
this.webClient = WebClient.create();
}

public UserInfo request(final String accessToken) {
public SocialUserInfo request(final String accessToken) {
return Objects.requireNonNull(webClient.get()
.uri(PROFILE_URL)
.header("Authorization", "Bearer " + accessToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public record KakaoProfileResponse(
@JsonProperty("kakao_account") KakaoAccount kakaoAccount
) {

public UserInfo toUserInfo() {
public SocialUserInfo toUserInfo() {
final Profile profile = kakaoAccount.profile;
return new UserInfo(
return new SocialUserInfo(
id,
SocialType.KAKAO,
profile.nickname
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.donggle.backend.infrastructure.oauth.kakao.dto.response;

import org.donggle.backend.domain.oauth.SocialType;
import org.donggle.backend.domain.member.Member;
import org.donggle.backend.domain.member.MemberName;
import org.donggle.backend.domain.oauth.SocialType;

public record UserInfo(
public record SocialUserInfo(
Long socialId,
SocialType socialType,
String nickname
Expand Down