com.clouway.kcqrs.testing.InMemoryEventStore.kt Maven / Gradle / Ivy
The newest version!
package com.clouway.kcqrs.testing
import com.clouway.kcqrs.core.Event
import com.clouway.kcqrs.core.EventStore
import java.util.*
/**
* @author Miroslav Genov ([email protected])
*/
class InMemoryEventStore : EventStore {
private val idToEvents = mutableMapOf>()
override fun saveEvents(aggregateId: UUID, expectedVersion: Int, events: Iterable) {
if (!idToEvents.contains(aggregateId)) {
idToEvents[aggregateId] = mutableListOf()
}
idToEvents[aggregateId]!!.addAll(events)
}
override fun getEvents(aggregateId: UUID, aggregateType: Class): Iterable {
if (!idToEvents.containsKey(aggregateId)) {
return listOf()
}
return idToEvents[aggregateId]!!
}
override fun revertEvents(aggregateId: UUID, events: Iterable) {
val newEvents = idToEvents[aggregateId]!!.filter { !events.contains(it) }.toMutableList()
idToEvents[aggregateId] = newEvents
}
}