EventStream.publish() can now receive vararg

This commit is contained in:
2024-02-28 23:39:57 +01:00
parent 43b5f27e50
commit 8beb66d8dc
5 changed files with 13 additions and 10 deletions

View File

@@ -7,13 +7,17 @@ class EventStream<ID : AggregateId> {
private val eventBus: MutableMap<ID, MutableList<Event<ID>>> = mutableMapOf() private val eventBus: MutableMap<ID, MutableList<Event<ID>>> = mutableMapOf()
fun publish(event: Event<ID>) { fun publish(event: Event<ID>) {
eventBus.getOrPut(event.aggregateId) { mutableListOf() }.add(event) eventBus.getOrPut(event.id) { mutableListOf() }.add(event)
logger.atInfo { logger.atInfo {
message = "Event published" message = "Event published: $event"
payload = mapOf("event" to event) payload = mapOf("event" to event)
} }
} }
fun publish(vararg events: Event<ID>) {
events.forEach { publish(it) }
}
fun <U : Event<ID>> read( fun <U : Event<ID>> read(
aggregateId: ID, aggregateId: ID,
eventClass: Class<U>, eventClass: Class<U>,

View File

@@ -1,10 +1,10 @@
package eventDemo.app package eventDemo.app
sealed interface Event<ID : AggregateId> { sealed interface Event<ID : AggregateId> {
val aggregateId: ID val id: ID
} }
data class PlayCardEvent( data class PlayCardEvent(
override val aggregateId: GameId, override val id: GameId,
val card: Card, val card: Card,
) : Event<GameId> ) : Event<GameId>

View File

@@ -37,7 +37,7 @@ class Game(
} }
} }
fun Routing.putCard() { fun Routing.card() {
val eventStream by inject<EventStream<GameId>>() val eventStream by inject<EventStream<GameId>>()
post<Game.Card.PutCard> { post<Game.Card.PutCard> {

View File

@@ -1,6 +1,6 @@
package eventDemo.plugins package eventDemo.plugins
import eventDemo.app.actions.putCard import eventDemo.app.actions.card
import io.ktor.http.HttpStatusCode import io.ktor.http.HttpStatusCode
import io.ktor.server.application.Application import io.ktor.server.application.Application
import io.ktor.server.application.install import io.ktor.server.application.install
@@ -24,8 +24,6 @@ fun Application.configureRouting() {
} }
routing { routing {
putCard() card()
} }
} }
private typealias ConverterDeclaration = Configuration.() -> Unit

View File

@@ -19,7 +19,7 @@ import org.koin.core.context.stopKoin
import org.koin.ktor.ext.inject import org.koin.ktor.ext.inject
import kotlin.test.assertEquals import kotlin.test.assertEquals
class PutCardTest : FunSpec({ class CardTest : FunSpec({
test("/game/{id}/card") { test("/game/{id}/card") {
testApplication { testApplication {
val client = httpClient() val client = httpClient()
@@ -48,6 +48,7 @@ class PutCardTest : FunSpec({
module() module()
val eventStream by inject<EventStream<GameId>>() val eventStream by inject<EventStream<GameId>>()
eventStream.publish( eventStream.publish(
PlayCardEvent(GameId(), Card.Simple(2, Card.Color.Yellow)),
PlayCardEvent(id, card), PlayCardEvent(id, card),
) )
} }