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()
fun publish(event: Event<ID>) {
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<ID>) {
events.forEach { publish(it) }
}
fun <U : Event<ID>> read(
aggregateId: ID,
eventClass: Class<U>,

View File

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

View File

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

View File

@@ -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