EventStream.publish() can now receive vararg
This commit is contained in:
@@ -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>,
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
@@ -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> {
|
||||||
|
|||||||
@@ -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
|
|
||||||
|
|||||||
@@ -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),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user