diff --git a/src/main/kotlin/org/gitanimals/render/app/AnimationFacade.kt b/src/main/kotlin/org/gitanimals/render/app/AnimationFacade.kt index 41c737f..fd5d15d 100644 --- a/src/main/kotlin/org/gitanimals/render/app/AnimationFacade.kt +++ b/src/main/kotlin/org/gitanimals/render/app/AnimationFacade.kt @@ -1,22 +1,19 @@ package org.gitanimals.render.app -import jakarta.annotation.PostConstruct import org.gitanimals.render.domain.User import org.gitanimals.render.domain.UserService import org.gitanimals.render.domain.event.Visited -import org.rooftop.netx.api.* +import org.rooftop.netx.api.SagaManager import org.springframework.stereotype.Service +import org.springframework.web.client.RestClientException @Service class AnimationFacade( private val userService: UserService, private val contributionApi: ContributionApi, - private val orchestratorFactory: OrchestratorFactory, private val sagaManager: SagaManager, ) { - private lateinit var registerNewUserOrchestrator: Orchestrator - fun getFarmAnimation(username: String): String { return when (userService.existsByName(username)) { true -> { @@ -26,15 +23,14 @@ class AnimationFacade( } false -> { - registerNewUserOrchestrator.sagaSync(10000, username) - - userService.getFarmAnimationByUsername(username) + val user = createNewUser(username) + userService.getFarmAnimationByUsername(user.name) } } } fun getLineAnimation(username: String, personaId: Long): String { - return when(userService.existsByName(username)) { + return when (userService.existsByName(username)) { true -> { val svgAnimation = userService.getLineAnimationByUsername(username, personaId) sagaManager.startSync(Visited(username)) @@ -42,45 +38,21 @@ class AnimationFacade( } false -> { - registerNewUserOrchestrator.sagaSync(10000, username) - - userService.getLineAnimationByUsername(username ,personaId) + val user = createNewUser(username) + userService.getLineAnimationByUsername(user.name, personaId) } } } - @PostConstruct - fun registerNewUserOrchestrator() { - registerNewUserOrchestrator = orchestratorFactory.create("register new user") - .startWithContext( - contextOrchestrate = { context, username -> - context.set("username", username) - contributionApi.getAllContributionYears(username) - } - ) - .joinWithContext( - contextOrchestrate = object : ContextOrchestrate, Map> { - override fun orchestrate(context: Context, request: List): Map { - val username = context.decodeContext("username", String::class) - return contributionApi.getContributionCount(username, request) - } - - override fun reified(): TypeReference> { - return object : TypeReference>() {} - } - } - ) - .commitWithContext( - contextOrchestrate = object : ContextOrchestrate, User> { - override fun orchestrate(context: Context, request: Map): User { - val username = context.decodeContext("username", String::class) - return userService.createNewUser(username, request) - } - - override fun reified(): TypeReference> { - return object : TypeReference>() {} - } - } - ) + fun createNewUser(username: String): User { + return runCatching { + val contributionYears = contributionApi.getAllContributionYears(username) + val contributionCountPerYear = + contributionApi.getContributionCount(username, contributionYears) + userService.createNewUser(username, contributionCountPerYear) + }.getOrElse { + require(it !is RestClientException) { "Cannot create new user from username \"$username\"" } + throw it + } } } diff --git a/src/main/kotlin/org/gitanimals/render/controller/SentryController.kt b/src/main/kotlin/org/gitanimals/render/controller/SentryController.kt deleted file mode 100644 index 364bfd4..0000000 --- a/src/main/kotlin/org/gitanimals/render/controller/SentryController.kt +++ /dev/null @@ -1,14 +0,0 @@ -package org.gitanimals.render.controller - -import io.sentry.Sentry -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.RestController - -@RestController -class SentryController { - - @GetMapping("/sentries") - fun sentryCall() { - Sentry.captureException(IllegalArgumentException("This is test.")) - } -} diff --git a/src/main/kotlin/org/gitanimals/render/domain/User.kt b/src/main/kotlin/org/gitanimals/render/domain/User.kt index c831a33..8cad6f2 100644 --- a/src/main/kotlin/org/gitanimals/render/domain/User.kt +++ b/src/main/kotlin/org/gitanimals/render/domain/User.kt @@ -1,5 +1,6 @@ package org.gitanimals.render.domain +import com.fasterxml.jackson.annotation.JsonIgnore import jakarta.persistence.* import org.gitanimals.render.core.AggregateRoot import org.gitanimals.render.domain.value.Contribution @@ -50,6 +51,7 @@ class User( personas.forEach { it.user = this } } + @JsonIgnore fun isContributionUpdatedBeforeOneHour(): Boolean { val currentYear = ZonedDateTime.now(ZoneId.of("UTC")).year val currentYearContribution =