11package org.gitanimals.quiz.infra.hibernate
22
3+ import org.gitanimals.core.GracefulShutdownDispatcher.gracefulLaunch
34import org.gitanimals.core.IdGenerator
45import org.gitanimals.inbox.domain.InboxType
56import org.gitanimals.quiz.app.IdentityApi
@@ -12,45 +13,72 @@ import org.hibernate.event.spi.PostUpdateEvent
1213import org.hibernate.event.spi.PostUpdateEventListener
1314import org.hibernate.persister.entity.EntityPersister
1415import org.slf4j.LoggerFactory
16+ import org.springframework.context.ApplicationEventPublisher
17+ import org.springframework.context.event.EventListener
1518import org.springframework.stereotype.Component
1619
1720@Component
1821class QuizSolveContextDoneHibernateEventListener (
19- private val inboxApi : InboxApi ,
20- private val identityApi : IdentityApi ,
22+ private val applicationEventPublisher : ApplicationEventPublisher ,
2123) : PostUpdateEventListener {
2224
23- private val logger = LoggerFactory .getLogger(this ::class .simpleName)
24-
2525 override fun requiresPostCommitHandling (persister : EntityPersister ): Boolean =
2626 persister.mappedClass == QuizSolveContext ::class .java
2727
2828 override fun onPostUpdate (event : PostUpdateEvent ) {
2929 if (event.entity is QuizSolveContext ) {
3030 val quizSolveContext = event.entity as QuizSolveContext
31- if (quizSolveContext.getStatus() == QuizSolveContextStatus .DONE ) {
31+ applicationEventPublisher.publishEvent(
32+ QuizSolveContextDoneLogicDelegator .QuizSolveContextDone (
33+ userId = quizSolveContext.userId,
34+ prize = quizSolveContext.getPrize(),
35+ status = quizSolveContext.getStatus(),
36+ )
37+ )
38+ }
39+ }
40+ }
41+
42+ @Component
43+ class QuizSolveContextDoneLogicDelegator (
44+ private val inboxApi : InboxApi ,
45+ private val identityApi : IdentityApi ,
46+ ) {
47+
48+ private val logger = LoggerFactory .getLogger(this ::class .simpleName)
49+
50+ data class QuizSolveContextDone (
51+ val userId : Long ,
52+ val prize : Int ,
53+ val status : QuizSolveContextStatus ,
54+ )
55+
56+ @EventListener(QuizSolveContextDone ::class )
57+ fun listenQUizSolveContextDone (event : QuizSolveContextDone ) {
58+ gracefulLaunch {
59+ if (event.status == QuizSolveContextStatus .DONE ) {
3260 runCatching {
3361 identityApi.increaseUserPointsById(
34- userId = quizSolveContext .userId,
35- point = quizSolveContext.getPrize() .toLong(),
62+ userId = event .userId,
63+ point = event.prize .toLong(),
3664 idempotencyKey = IdGenerator .generate().toString(),
3765 )
3866 }.onSuccess {
3967 inboxApi.inputInbox(
4068 InboxInputRequest (
4169 inboxData = InboxData (
42- userId = quizSolveContext .userId,
70+ userId = event .userId,
4371 type = InboxType .INBOX ,
4472 title = " Quiz prize arrived." ,
45- body = " Congratulations! You got ${quizSolveContext.getPrize() } point by solving quiz." ,
73+ body = " Congratulations! You got ${event.prize } point by solving quiz." ,
4674 image = " https://avatars.githubusercontent.com/u/171903401?s=200&v=4" ,
4775 redirectTo = " NO_REDIRECT" ,
4876 )
4977 )
5078 )
5179 }.onFailure {
5280 logger.error(
53- " Cannot give point to user. userId: \" ${quizSolveContext .userId} \" , missing point: \" ${quizSolveContext.getPrize() } \" " ,
81+ " Cannot give point to user. userId: \" ${event .userId} \" , missing point: \" ${event.prize } \" " ,
5482 it
5583 )
5684 }
0 commit comments