Skip to content

Commit

Permalink
release: 0.2.0 (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb authored Apr 15, 2024
2 parents c69e55b + d110bcd commit 456117d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 59 deletions.
62 changes: 17 additions & 45 deletions src/main/kotlin/org/gitanimals/render/app/AnimationFacade.kt
Original file line number Diff line number Diff line change
@@ -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<String, User>

fun getFarmAnimation(username: String): String {
return when (userService.existsByName(username)) {
true -> {
Expand All @@ -26,61 +23,36 @@ 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))
svgAnimation
}

false -> {
registerNewUserOrchestrator.sagaSync(10000, username)

userService.getLineAnimationByUsername(username ,personaId)
val user = createNewUser(username)
userService.getLineAnimationByUsername(user.name, personaId)
}
}
}

@PostConstruct
fun registerNewUserOrchestrator() {
registerNewUserOrchestrator = orchestratorFactory.create<String>("register new user")
.startWithContext(
contextOrchestrate = { context, username ->
context.set("username", username)
contributionApi.getAllContributionYears(username)
}
)
.joinWithContext(
contextOrchestrate = object : ContextOrchestrate<List<Int>, Map<Int, Int>> {
override fun orchestrate(context: Context, request: List<Int>): Map<Int, Int> {
val username = context.decodeContext("username", String::class)
return contributionApi.getContributionCount(username, request)
}

override fun reified(): TypeReference<List<Int>> {
return object : TypeReference<List<Int>>() {}
}
}
)
.commitWithContext(
contextOrchestrate = object : ContextOrchestrate<Map<Int, Int>, User> {
override fun orchestrate(context: Context, request: Map<Int, Int>): User {
val username = context.decodeContext("username", String::class)
return userService.createNewUser(username, request)
}

override fun reified(): TypeReference<Map<Int, Int>> {
return object : TypeReference<Map<Int, Int>>() {}
}
}
)
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
}
}
}

This file was deleted.

2 changes: 2 additions & 0 deletions src/main/kotlin/org/gitanimals/render/domain/User.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 456117d

Please sign in to comment.