Skip to content

Commit

Permalink
Temporary removing spring-hateoas and fixed some issues in example pr…
Browse files Browse the repository at this point in the history
…oject
  • Loading branch information
johanhaleby committed Dec 22, 2023
1 parent 8948d0d commit 375b096
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@ package org.occurrent.example.domain.rps.decidermodel.web.cqrs.gameplay
import org.occurrent.application.service.blocking.ApplicationService
import org.occurrent.dsl.decider.execute
import org.occurrent.example.domain.rps.decidermodel.*
import org.occurrent.example.domain.rps.decidermodel.HandGesture.ROCK
import org.occurrent.example.domain.rps.decidermodel.web.common.loggerFor
import org.springframework.hateoas.EntityModel
import org.springframework.hateoas.IanaLinkRelations
import org.springframework.hateoas.RepresentationModel
import org.springframework.hateoas.server.mvc.andAffordances
import org.springframework.hateoas.server.mvc.linkTo
import org.springframework.hateoas.server.mvc.withRel
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*
Expand All @@ -40,26 +34,28 @@ class GamePlayController(private val applicationService: ApplicationService<Game
private val log = loggerFor<GamePlayController>()


// : ResponseEntity<EntityModel<PlayGameRepresentationModel>> {
@PutMapping("{gameId}")
fun initializeNewGame(@PathVariable("gameId") gameId: GameId, @RequestParam playerId: PlayerId): ResponseEntity<EntityModel<PlayGameRepresentationModel>> {
fun initializeNewGame(@PathVariable("gameId") gameId: GameId, @RequestParam playerId: PlayerId) : ResponseEntity<Unit> {
log.info("Initializing new game (gameId=$gameId)")

// val self = linkTo(methodOn(GameViewController::class.java).showGame(gameId) as Any).withSelfRel()
// val selfWithAffordances = self.andAffordance(afford(methodOn(GamePlayController::class.java).playGame(gameId, playerId, ROCK)))


val self = linkTo<GameViewController> { showGame(gameId) } withRel IanaLinkRelations.SELF
val selfWithAffordances = self andAffordances {
afford<GamePlayController> { playGame(gameId, playerId, ROCK) }
}

val model = EntityModel.of(PlayGameRepresentationModel())
.add(selfWithAffordances)
// val self = linkTo<GameViewController> { showGame(gameId) } withRel IanaLinkRelations.SELF
// val selfWithAffordances = self andAffordances {
// afford<GamePlayController> { playGame(gameId, playerId, ROCK) }
// }
//
// val model = EntityModel.of(PlayGameRepresentationModel())
// .add(selfWithAffordances)

val cmd = InitiateNewGame(gameId, Timestamp.now(), playerId)
applicationService.execute(gameId, cmd, rps)

return ResponseEntity.ok().header("Location", "/games/$gameId").body(model)
return ResponseEntity.noContent().header("Location", "/games/$gameId").build()
// return ResponseEntity.ok().header("Location", "/games/$gameId").body(model)
}

@PostMapping("{gameId}/play")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.occurrent.dsl.view.updateView
import org.occurrent.dsl.view.view
import org.occurrent.example.domain.rps.decidermodel.*
import org.occurrent.example.domain.rps.decidermodel.web.common.loggerFor
import org.occurrent.example.domain.rps.decidermodel.web.cqrs.gameplay.GameStatus.*
import org.springframework.data.annotation.Id
import org.springframework.data.annotation.TypeAlias
import org.springframework.data.mongodb.core.MongoOperations
Expand All @@ -51,28 +52,20 @@ sealed interface GameReadModel {
val status: GameStatus

@TypeAlias("Initialized")
data class Initialized(override val gameId: GameId, val initializedBy: PlayerId) : GameReadModel {
override val status = GameStatus.Initialized
}
data class Initialized(override val gameId: GameId, val initializedBy: PlayerId, override val status: GameStatus) : GameReadModel

@TypeAlias("Ongoing")
data class Ongoing(override val gameId: GameId, val firstMove: Move, val secondMove: Move? = null) : GameReadModel {
override val status = GameStatus.Ongoing
}
data class Ongoing(override val gameId: GameId, val firstMove: Move, val secondMove: Move? = null, override val status: GameStatus) : GameReadModel

sealed interface Ended : GameReadModel {
val firstMove: Move
val secondMove: Move

@TypeAlias("Tied")
data class Tied(override val gameId: GameId, override val firstMove: Move, override val secondMove: Move) : Ended {
override val status = GameStatus.Tied
}
data class Tied(override val gameId: GameId, override val firstMove: Move, override val secondMove: Move, override val status: GameStatus) : Ended

@TypeAlias("Won")
data class Won(override val gameId: GameId, override val firstMove: Move, override val secondMove: Move, val winner: PlayerId) : Ended {
override val status = GameStatus.Won
}
data class Won(override val gameId: GameId, override val firstMove: Move, override val secondMove: Move, val winner: PlayerId, override val status: GameStatus) : Ended
}
}

Expand All @@ -88,23 +81,23 @@ private val gameView = view<GameReadModel?, GameEvent>(
initialState = null,
updateState = { game, e ->
when (e) {
is NewGameInitiated -> GameReadModel.Initialized(e.gameId, e.playerId)
is NewGameInitiated -> GameReadModel.Initialized(e.gameId, e.playerId, Initialized)
is GameStarted -> game
is HandGestureShown -> when (game) {
is GameReadModel.Initialized -> GameReadModel.Ongoing(e.gameId, firstMove = Move(e.player, e.gesture))
is GameReadModel.Initialized -> GameReadModel.Ongoing(e.gameId, firstMove = Move(e.player, e.gesture), status = Ongoing)
is GameReadModel.Ongoing -> game.copy(secondMove = Move(e.player, e.gesture))
else -> game
}

is GameEnded -> game
is GameTied -> {
val ongoingGame = game as GameReadModel.Ongoing
GameReadModel.Ended.Tied(e.gameId, ongoingGame.firstMove, ongoingGame.secondMove!!)
GameReadModel.Ended.Tied(e.gameId, ongoingGame.firstMove, ongoingGame.secondMove!!, Tied)
}

is GameWon -> {
val ongoingGame = game as GameReadModel.Ongoing
GameReadModel.Ended.Won(e.gameId, ongoingGame.firstMove, ongoingGame.secondMove!!, e.winner)
GameReadModel.Ended.Won(e.gameId, ongoingGame.firstMove, ongoingGame.secondMove!!, e.winner, Won)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import org.springframework.boot.testcontainers.service.connection.ServiceConnect
import org.springframework.boot.with
import org.springframework.context.annotation.Bean
import org.testcontainers.containers.MongoDBContainer
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy
import org.testcontainers.containers.wait.strategy.WaitStrategy


@TestConfiguration(proxyBeanMethods = false)
Expand All @@ -36,7 +34,9 @@ class TestBootstrap {
@Bean
@ServiceConnection
@RestartScope
fun mongoDbContainer(): MongoDBContainer = MongoDBContainer("mongo:4.2.8")
fun mongoDbContainer(): MongoDBContainer = MongoDBContainer("mongo:4.2.8").apply {
portBindings = listOf("27017:27017")
}
}

fun main(args: Array<String>) {
Expand Down

0 comments on commit 375b096

Please sign in to comment.