From 8beb66d8dc08547272d9d9ea5c4364cc7d2f1eef Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Wed, 28 Feb 2024 23:39:57 +0100 Subject: [PATCH] EventStream.publish() can now receive vararg --- src/main/kotlin/eventDemo/app/EventStream.kt | 8 ++++++-- src/main/kotlin/eventDemo/app/Events.kt | 4 ++-- src/main/kotlin/eventDemo/app/actions/PutCard.kt | 2 +- src/main/kotlin/eventDemo/plugins/Routing.kt | 6 ++---- .../eventDemo/app/actions/{PutCardTest.kt => CardTest.kt} | 3 ++- 5 files changed, 13 insertions(+), 10 deletions(-) rename src/test/kotlin/eventDemo/app/actions/{PutCardTest.kt => CardTest.kt} (94%) diff --git a/src/main/kotlin/eventDemo/app/EventStream.kt b/src/main/kotlin/eventDemo/app/EventStream.kt index e71670b..80801d4 100644 --- a/src/main/kotlin/eventDemo/app/EventStream.kt +++ b/src/main/kotlin/eventDemo/app/EventStream.kt @@ -7,13 +7,17 @@ class EventStream { private val eventBus: MutableMap>> = mutableMapOf() fun publish(event: Event) { - eventBus.getOrPut(event.aggregateId) { mutableListOf() }.add(event) + eventBus.getOrPut(event.id) { mutableListOf() }.add(event) logger.atInfo { - message = "Event published" + message = "Event published: $event" payload = mapOf("event" to event) } } + fun publish(vararg events: Event) { + events.forEach { publish(it) } + } + fun > read( aggregateId: ID, eventClass: Class, diff --git a/src/main/kotlin/eventDemo/app/Events.kt b/src/main/kotlin/eventDemo/app/Events.kt index 57c09c4..0b30b69 100644 --- a/src/main/kotlin/eventDemo/app/Events.kt +++ b/src/main/kotlin/eventDemo/app/Events.kt @@ -1,10 +1,10 @@ package eventDemo.app sealed interface Event { - val aggregateId: ID + val id: ID } data class PlayCardEvent( - override val aggregateId: GameId, + override val id: GameId, val card: Card, ) : Event diff --git a/src/main/kotlin/eventDemo/app/actions/PutCard.kt b/src/main/kotlin/eventDemo/app/actions/PutCard.kt index 9f307c3..8063b03 100644 --- a/src/main/kotlin/eventDemo/app/actions/PutCard.kt +++ b/src/main/kotlin/eventDemo/app/actions/PutCard.kt @@ -37,7 +37,7 @@ class Game( } } -fun Routing.putCard() { +fun Routing.card() { val eventStream by inject>() post { diff --git a/src/main/kotlin/eventDemo/plugins/Routing.kt b/src/main/kotlin/eventDemo/plugins/Routing.kt index 3c3be37..2adb606 100644 --- a/src/main/kotlin/eventDemo/plugins/Routing.kt +++ b/src/main/kotlin/eventDemo/plugins/Routing.kt @@ -1,6 +1,6 @@ package eventDemo.plugins -import eventDemo.app.actions.putCard +import eventDemo.app.actions.card import io.ktor.http.HttpStatusCode import io.ktor.server.application.Application import io.ktor.server.application.install @@ -24,8 +24,6 @@ fun Application.configureRouting() { } routing { - putCard() + card() } } - -private typealias ConverterDeclaration = Configuration.() -> Unit diff --git a/src/test/kotlin/eventDemo/app/actions/PutCardTest.kt b/src/test/kotlin/eventDemo/app/actions/CardTest.kt similarity index 94% rename from src/test/kotlin/eventDemo/app/actions/PutCardTest.kt rename to src/test/kotlin/eventDemo/app/actions/CardTest.kt index 1540482..5cb3931 100644 --- a/src/test/kotlin/eventDemo/app/actions/PutCardTest.kt +++ b/src/test/kotlin/eventDemo/app/actions/CardTest.kt @@ -19,7 +19,7 @@ import org.koin.core.context.stopKoin import org.koin.ktor.ext.inject import kotlin.test.assertEquals -class PutCardTest : FunSpec({ +class CardTest : FunSpec({ test("/game/{id}/card") { testApplication { val client = httpClient() @@ -48,6 +48,7 @@ class PutCardTest : FunSpec({ module() val eventStream by inject>() eventStream.publish( + PlayCardEvent(GameId(), Card.Simple(2, Card.Color.Yellow)), PlayCardEvent(id, card), ) }