-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: OauthService 회원 가입, 로그인, 탈퇴 시 Oauth id token, access token …
…요청/응답과 db 트랜잭션 분리 (#276) * Refactor: OauthService에서 socialProfile 생성 로직을 SocialProfileService로 분리 * Refactor: SignupEvent와 WithdrawEvent 추가 * Refactor: MemberWithdrawService에서 탈퇴 시 러닝 데이터가 없어도 탈퇴 id를 로깅하도룩 수정 * Fix: social profile 조회 시 member도 fetch join하도록 수정 * Test: OauthServiceTest에 리팩토링 사항 반영
- Loading branch information
1 parent
e6d098d
commit 4e4e322
Showing
9 changed files
with
151 additions
and
114 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/main/java/com/dnd/runus/application/member/MemberEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.dnd.runus.application.member; | ||
|
||
import com.dnd.runus.application.member.event.SignupEvent; | ||
import com.dnd.runus.application.member.event.WithdrawEvent; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class MemberEventHandler { | ||
|
||
private final MemberService memberService; | ||
private final MemberWithdrawService memberWithdrawService; | ||
|
||
@EventListener | ||
public void handleSignupEvent(SignupEvent signupEvent) { | ||
memberService.initMember(signupEvent.member()); | ||
} | ||
|
||
@EventListener | ||
public void handleWithdrawEvent(WithdrawEvent withdrawEvent) { | ||
memberWithdrawService.deleteAllDataAboutMember(withdrawEvent.memberId()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/java/com/dnd/runus/application/member/event/SignupEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.dnd.runus.application.member.event; | ||
|
||
import com.dnd.runus.domain.member.Member; | ||
|
||
public record SignupEvent(Member member) {} |
3 changes: 3 additions & 0 deletions
3
src/main/java/com/dnd/runus/application/member/event/WithdrawEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.dnd.runus.application.member.event; | ||
|
||
public record WithdrawEvent(long memberId) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/com/dnd/runus/application/oauth/SocialProfileService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.dnd.runus.application.oauth; | ||
|
||
import com.dnd.runus.domain.member.Member; | ||
import com.dnd.runus.domain.member.MemberRepository; | ||
import com.dnd.runus.domain.member.SocialProfile; | ||
import com.dnd.runus.domain.member.SocialProfileRepository; | ||
import com.dnd.runus.global.constant.MemberRole; | ||
import com.dnd.runus.global.constant.SocialType; | ||
import com.dnd.runus.global.exception.NotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class SocialProfileService { | ||
|
||
private final SocialProfileRepository socialProfileRepository; | ||
private final MemberRepository memberRepository; | ||
|
||
@Transactional(readOnly = true) | ||
public boolean isSocialMemberExists(SocialType socialType, String oauthId, long memberId) { | ||
return socialProfileRepository | ||
.findBySocialTypeAndOauthId(socialType, oauthId) | ||
.filter(profile -> profile.member().memberId() == memberId) | ||
.isPresent(); | ||
} | ||
|
||
@Transactional | ||
public SocialProfile findOrThrow(SocialType socialType, String oauthId, String email) { | ||
SocialProfile socialProfile = socialProfileRepository | ||
.findBySocialTypeAndOauthId(socialType, oauthId) | ||
.orElseThrow(() -> new NotFoundException(SocialProfile.class, oauthId)); | ||
|
||
updateEmailIfChanged(socialProfile, email); | ||
return socialProfile; | ||
} | ||
|
||
@Transactional | ||
public SocialProfile findOrCreate(SocialType socialType, String oauthId, String email, String nickname) { | ||
Member member = memberRepository.save(new Member(MemberRole.USER, nickname)); | ||
|
||
SocialProfile socialProfile = socialProfileRepository | ||
.findBySocialTypeAndOauthId(socialType, oauthId) | ||
.orElseGet(() -> createSocialProfile(member, socialType, oauthId, email)); | ||
|
||
updateEmailIfChanged(socialProfile, email); | ||
return socialProfile; | ||
} | ||
|
||
private SocialProfile createSocialProfile(Member member, SocialType socialType, String oauthId, String email) { | ||
return socialProfileRepository.save(SocialProfile.builder() | ||
.member(member) | ||
.socialType(socialType) | ||
.oauthId(oauthId) | ||
.oauthEmail(email) | ||
.build()); | ||
} | ||
|
||
private void updateEmailIfChanged(SocialProfile socialProfile, String email) { | ||
if (!email.equals(socialProfile.oauthEmail())) { | ||
socialProfileRepository.updateOauthEmail(socialProfile.socialProfileId(), email); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.