-
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.
- Loading branch information
1 parent
7d6aca3
commit f2fc624
Showing
10 changed files
with
259 additions
and
212 deletions.
There are no files selected for viewing
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
35 changes: 35 additions & 0 deletions
35
src/main/java/com/dnd/gooding/common/event/EventResetProcessor.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,35 @@ | ||
package com.dnd.gooding.common.event; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Aspect | ||
@Order(0) | ||
@Component | ||
public class EventResetProcessor { | ||
|
||
private ThreadLocal<Integer> nestedCount = | ||
new ThreadLocal<Integer>() { | ||
|
||
@Override | ||
protected Integer initialValue() { | ||
return 0; | ||
} | ||
}; | ||
|
||
@Around("@within(org.springframework.stereotype.Service)") | ||
public Object doReset(ProceedingJoinPoint joinPoint) throws Throwable { | ||
nestedCount.set(nestedCount.get() + 1); | ||
try { | ||
return joinPoint.proceed(); | ||
} finally { | ||
nestedCount.set(nestedCount.get() - 1); | ||
if (nestedCount.get() == 0) { | ||
Events.reset(); | ||
} | ||
} | ||
} | ||
} |
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
83 changes: 41 additions & 42 deletions
83
src/main/java/com/dnd/gooding/oauth/command/application/CreateOAuthService.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 |
---|---|---|
@@ -1,55 +1,54 @@ | ||
package com.dnd.gooding.oauth.command.application; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.dnd.gooding.common.event.Events; | ||
import com.dnd.gooding.oauth.command.domain.ExternalLogin; | ||
import com.dnd.gooding.oauth.command.domain.OAuth; | ||
import com.dnd.gooding.oauth.command.domain.OAuthId; | ||
import com.dnd.gooding.oauth.command.domain.OAuthRepository; | ||
import com.dnd.gooding.oauth.command.model.OAuthMember; | ||
import com.dnd.gooding.user.command.application.CreateMemberService; | ||
import com.dnd.gooding.user.command.domain.MemberCreatedEvent; | ||
import com.dnd.gooding.oauth.infra.MemberCreatedHandler; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
public class CreateOAuthService { | ||
|
||
private CreateMemberService createMemberService; | ||
private OAuthRepository oAuthRepository; | ||
|
||
private ExternalLogin externalLogin; | ||
|
||
public CreateOAuthService(CreateMemberService createMemberService, | ||
OAuthRepository oAuthRepository, ExternalLogin externalLogin) { | ||
this.createMemberService = createMemberService; | ||
this.oAuthRepository = oAuthRepository; | ||
this.externalLogin = externalLogin; | ||
} | ||
|
||
@Transactional | ||
public OAuth create(String code) { | ||
OAuthMember oAuthMember = externalLogin.getOauthToken(code); | ||
Events.handle((MemberCreatedEvent evt) -> | ||
createMemberService.create(evt.getEmail(), evt.getoAuthId().getId())); | ||
|
||
OAuth oAuth = new OAuth( | ||
new OAuthId(oAuthMember.getoAuthId()), | ||
oAuthMember.getImageUrl(), | ||
oAuthMember.getProvider(), | ||
oAuthMember.getEmail()); | ||
createOAuth(oAuth); | ||
return oAuth; | ||
} | ||
|
||
@Transactional | ||
public void createOAuth(OAuth oAuth) { | ||
oAuthRepository | ||
.findByoAuthId(oAuth.getoAuthId()) | ||
.ifPresentOrElse(x -> { | ||
}, () -> { | ||
oAuthRepository.save(oAuth); | ||
}); | ||
} | ||
private OAuthRepository oAuthRepository; | ||
|
||
private ExternalLogin externalLogin; | ||
|
||
@Autowired private MemberCreatedHandler memberCreatedHandler; | ||
|
||
public CreateOAuthService(OAuthRepository oAuthRepository, ExternalLogin externalLogin) { | ||
this.oAuthRepository = oAuthRepository; | ||
this.externalLogin = externalLogin; | ||
} | ||
|
||
@Transactional | ||
public OAuth create(String code) { | ||
OAuthMember oAuthMember = externalLogin.getOauthToken(code); | ||
Events.handle(memberCreatedHandler); | ||
|
||
OAuth oAuth = | ||
new OAuth( | ||
new OAuthId(oAuthMember.getoAuthId()), | ||
oAuthMember.getImageUrl(), | ||
oAuthMember.getProvider(), | ||
oAuthMember.getEmail()); | ||
|
||
createOAuth(oAuth); | ||
return oAuth; | ||
} | ||
|
||
@Transactional | ||
public void createOAuth(OAuth oAuth) { | ||
oAuthRepository | ||
.findByoAuthId(oAuth.getoAuthId()) | ||
.ifPresentOrElse( | ||
x -> {}, | ||
() -> { | ||
oAuthRepository.save(oAuth); | ||
}); | ||
} | ||
} |
67 changes: 32 additions & 35 deletions
67
src/main/java/com/dnd/gooding/oauth/command/domain/OAuth.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 |
---|---|---|
@@ -1,46 +1,43 @@ | ||
package com.dnd.gooding.oauth.command.domain; | ||
|
||
import com.dnd.gooding.common.event.Events; | ||
import com.dnd.gooding.user.command.domain.MemberCreatedEvent; | ||
import javax.persistence.EmbeddedId; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Table; | ||
|
||
import com.dnd.gooding.common.event.Events; | ||
import com.dnd.gooding.user.command.domain.MemberCreatedEvent; | ||
|
||
@Entity | ||
@Table(name = "oauth") | ||
public class OAuth { | ||
|
||
@EmbeddedId | ||
private OAuthId oAuthId; | ||
private String imageUrl; | ||
private String provider; | ||
private String email; | ||
|
||
protected OAuth() { | ||
} | ||
|
||
public OAuth(OAuthId oAuthId, String imageUrl, String provider, String email) { | ||
this.oAuthId = oAuthId; | ||
this.imageUrl = imageUrl; | ||
this.provider = provider; | ||
this.email = email; | ||
Events.raise(new MemberCreatedEvent(email, oAuthId)); | ||
} | ||
|
||
public OAuthId getoAuthId() { | ||
return oAuthId; | ||
} | ||
|
||
public String getImageUrl() { | ||
return imageUrl; | ||
} | ||
|
||
public String getProvider() { | ||
return provider; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
@EmbeddedId private OAuthId oAuthId; | ||
private String imageUrl; | ||
private String provider; | ||
private String email; | ||
|
||
protected OAuth() {} | ||
|
||
public OAuth(OAuthId oAuthId, String imageUrl, String provider, String email) { | ||
this.oAuthId = oAuthId; | ||
this.imageUrl = imageUrl; | ||
this.provider = provider; | ||
this.email = email; | ||
Events.raise(new MemberCreatedEvent(email, oAuthId)); | ||
} | ||
|
||
public OAuthId getoAuthId() { | ||
return oAuthId; | ||
} | ||
|
||
public String getImageUrl() { | ||
return imageUrl; | ||
} | ||
|
||
public String getProvider() { | ||
return provider; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/dnd/gooding/oauth/infra/MemberCreatedHandler.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,21 @@ | ||
package com.dnd.gooding.oauth.infra; | ||
|
||
import com.dnd.gooding.common.event.EventHandler; | ||
import com.dnd.gooding.user.command.application.CreateMemberService; | ||
import com.dnd.gooding.user.command.domain.MemberCreatedEvent; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class MemberCreatedHandler implements EventHandler<MemberCreatedEvent> { | ||
|
||
private CreateMemberService createMemberService; | ||
|
||
public MemberCreatedHandler(CreateMemberService createMemberService) { | ||
this.createMemberService = createMemberService; | ||
} | ||
|
||
@Override | ||
public void handle(MemberCreatedEvent event) { | ||
createMemberService.create(event.getEmail(), event.getoAuthId().getId()); | ||
} | ||
} |
Oops, something went wrong.