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

View File

@@ -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<GameId>>()
eventStream.publish(
PlayCardEvent(GameId(), Card.Simple(2, Card.Color.Yellow)),
PlayCardEvent(id, card),
)
}