refactoring: Masive refactor to build the V2
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
package eventDemo.contexts.game.application.eventStores
|
||||
|
||||
import eventDemo.contexts.game.application.ports.GameEventStore
|
||||
import eventDemo.contexts.game.domain.game.GameId
|
||||
import eventDemo.contexts.game.domain.game.gameState.Game
|
||||
import eventDemo.libs.eventSource.eventStore.VersionConflictException
|
||||
|
||||
class GameEventStoreRepository(
|
||||
val eventStore: GameEventStore,
|
||||
) : GameRepository {
|
||||
override fun get(id: GameId): Game? {
|
||||
val events =
|
||||
eventStore
|
||||
.getStream(id)
|
||||
.readAll()
|
||||
if (events.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
return events.let { Game.loadFromHistory(it) }
|
||||
}
|
||||
|
||||
@Throws(VersionConflictException::class)
|
||||
override fun save(game: Game) {
|
||||
eventStore.append(game.recordedEvents)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package eventDemo.contexts.game.application.eventStores
|
||||
|
||||
import eventDemo.contexts.game.domain.game.GameId
|
||||
import eventDemo.contexts.game.domain.game.gameState.Game
|
||||
import eventDemo.contexts.game.domain.game.gameState.GameCreated
|
||||
import eventDemo.contexts.game.domain.game.gameState.GameInit
|
||||
import eventDemo.libs.eventSource.eventStore.VersionConflictException
|
||||
|
||||
interface GameRepository {
|
||||
fun get(id: GameId): Game?
|
||||
|
||||
@Throws(VersionConflictException::class)
|
||||
fun save(game: Game)
|
||||
|
||||
fun getOrCreate(gameId: GameId): Game =
|
||||
get(gameId) ?: create(gameId)
|
||||
|
||||
fun create(gameId: GameId = GameId()): GameCreated =
|
||||
GameInit.createNewGame(gameId).also { save(it) }
|
||||
}
|
||||
Reference in New Issue
Block a user