Skip to content

Commit

Permalink
Feature/game create on (#213)
Browse files Browse the repository at this point in the history
* feature: game createdOn field

* feature: game api response createdOn field
  • Loading branch information
kuoche1712003 authored Apr 12, 2024
1 parent 9af7872 commit 81ce439
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import tw.waterballsa.gaas.domain.GameRegistration
import tw.waterballsa.gaas.events.RegisteredGameEvent
import tw.waterballsa.gaas.exceptions.PlatformException
import tw.waterballsa.gaas.exceptions.enums.PlatformError.GAME_EXISTS
import java.time.Instant
import java.time.temporal.ChronoUnit
import javax.inject.Named

@Named
Expand Down Expand Up @@ -56,6 +58,7 @@ private fun RegisterGameUsecase.Request.toGameRegistration(): GameRegistration =
maxPlayers = maxPlayers,
frontEndUrl = frontEndUrl,
backEndUrl = backEndUrl,
createdOn = Instant.now().truncatedTo(ChronoUnit.DAYS),
)

private fun GameRegistration.toRegisteredGameEvent(): RegisteredGameEvent =
Expand All @@ -70,4 +73,5 @@ private fun GameRegistration.toRegisteredGameEvent(): RegisteredGameEvent =
maxPlayers,
frontEndUrl,
backEndUrl,
createdOn,
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,49 @@ class UpdateGameRegistrationUseCase(

fun execute(request: Request, presenter: Presenter) {
with(request) {
validateGameExist()
validateUniqueNameDuplicated()
val gameRegistration = updateGameRegistration()

val gameRegistration = findGame(gameId)
.validateUniqueNameDuplicated(request)
.setValueByRequest(this)
.updateGame()
val event = gameRegistration.toGameRegistrationUpdatedEvent()
presenter.present(event)
eventBus.broadcast(event)
}
}

private fun Request.validateGameExist() {
gameRegistrationRepository.findById(gameId)
private fun findGame(gameId: GameRegistration.Id): GameRegistration {
return gameRegistrationRepository.findById(gameId)
?: throw notFound(GAME_NOT_FOUND, GameRegistration::class).id(gameId)
}

private fun Request.validateUniqueNameDuplicated() {
gameRegistrationRepository.findGameRegistrationByUniqueName(uniqueName)
?.takeIf { it.id != gameId }
private fun GameRegistration.validateUniqueNameDuplicated(request: Request): GameRegistration {
gameRegistrationRepository.findGameRegistrationByUniqueName(request.uniqueName)
?.takeIf { it.id != request.gameId }
?.let {
throw PlatformException(
GAME_EXISTS,
"$uniqueName already exists",
"${request.uniqueName} already exists",
)
}
return this
}

private fun GameRegistration.setValueByRequest(request: Request): GameRegistration {
uniqueName = request.uniqueName
displayName = request.displayName
shortDescription = request.shortDescription
rule = request.rule
imageUrl = request.imageUrl
minPlayers = request.minPlayers
maxPlayers = request.maxPlayers
frontEndUrl = request.frontEndUrl
backEndUrl = request.backEndUrl
return this
}

private fun Request.updateGameRegistration(): GameRegistration =
gameRegistrationRepository.updateGame(toGameRegistration())
private fun GameRegistration.updateGame(): GameRegistration {
return gameRegistrationRepository.updateGame(this)
}

data class Request(
val gameId: GameRegistration.Id,
Expand All @@ -59,20 +74,7 @@ class UpdateGameRegistrationUseCase(
val maxPlayers: Int,
val frontEndUrl: String,
val backEndUrl: String,
) {
fun toGameRegistration(): GameRegistration = GameRegistration(
gameId,
uniqueName,
displayName,
shortDescription,
rule,
imageUrl,
minPlayers,
maxPlayers,
frontEndUrl,
backEndUrl,
)
}
)
}

private fun GameRegistration.toGameRegistrationUpdatedEvent(): GameRegistrationUpdatedEvent =
Expand All @@ -87,4 +89,5 @@ private fun GameRegistration.toGameRegistrationUpdatedEvent(): GameRegistrationU
maxPlayers,
frontEndUrl,
backEndUrl,
createdOn,
)
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package tw.waterballsa.gaas.domain

import java.time.Instant

class GameRegistration(
val id: Id? = null,
val uniqueName: String,
val displayName: String,
val shortDescription: String,
val rule: String,
val imageUrl: String,
val minPlayers: Int,
val maxPlayers: Int,
val frontEndUrl: String,
val backEndUrl: String,
var uniqueName: String,
var displayName: String,
var shortDescription: String,
var rule: String,
var imageUrl: String,
var minPlayers: Int,
var maxPlayers: Int,
var frontEndUrl: String,
var backEndUrl: String,
val createdOn: Instant,
) {
@JvmInline
value class Id(val value: String)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tw.waterballsa.gaas.events

import tw.waterballsa.gaas.domain.GameRegistration
import java.time.Instant

class GameRegistrationUpdatedEvent(
val id: GameRegistration.Id,
Expand All @@ -13,4 +14,5 @@ class GameRegistrationUpdatedEvent(
val maxPlayers: Int,
val frontEndUrl: String,
val backEndUrl: String,
val createdOn: Instant,
) : DomainEvent()
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tw.waterballsa.gaas.events

import tw.waterballsa.gaas.domain.GameRegistration
import java.time.Instant

data class RegisteredGameEvent(
val id: GameRegistration.Id,
Expand All @@ -13,4 +14,5 @@ data class RegisteredGameEvent(
val maxPlayers: Int,
val frontEndUrl: String,
val backEndUrl: String,
val createdOn: Instant,
) : DomainEvent()
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import tw.waterballsa.gaas.spring.controllers.GetGameRegistrationPresenter.GetGa
import tw.waterballsa.gaas.spring.controllers.presenter.UpdateGameRegistrationPresenter
import tw.waterballsa.gaas.spring.controllers.viewmodel.UpdateGameRegistrationViewModel
import tw.waterballsa.gaas.spring.extensions.getEvent
import java.time.Instant

@RestController
@RequestMapping("/games")
Expand Down Expand Up @@ -95,7 +96,8 @@ class RegisterGamePresenter : Presenter {
minPlayers,
maxPlayers,
frontEndUrl,
backEndUrl
backEndUrl,
createdOn,
)

data class RegisterGameViewModel(
Expand All @@ -108,7 +110,8 @@ class RegisterGamePresenter : Presenter {
val minPlayers: Int,
val maxPlayers: Int,
val frontEndUrl: String,
val backEndUrl: String
val backEndUrl: String,
val createdOn: Instant,
)
}

Expand All @@ -126,7 +129,8 @@ class GetGameRegistrationPresenter : GetGameRegistrationsUsecase.Presenter {
name = displayName,
img = imageUrl,
minPlayers = minPlayers,
maxPlayers = maxPlayers
maxPlayers = maxPlayers,
createdOn = createdOn,
)

data class GetGamesViewModel(
Expand All @@ -135,5 +139,6 @@ class GetGameRegistrationPresenter : GetGameRegistrationsUsecase.Presenter {
val img: String,
val minPlayers: Int,
val maxPlayers: Int,
val createdOn: Instant,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class UpdateGameRegistrationPresenter : Presenter {
minPlayers,
maxPlayers,
frontEndUrl,
backEndUrl
backEndUrl,
createdOn,
)

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tw.waterballsa.gaas.spring.controllers.viewmodel

import tw.waterballsa.gaas.domain.GameRegistration
import java.time.Instant

data class UpdateGameRegistrationViewModel(
val id: GameRegistration.Id,
Expand All @@ -13,4 +14,5 @@ data class UpdateGameRegistrationViewModel(
val maxPlayers: Int,
val frontEndUrl: String,
val backEndUrl: String,
val createdOn: Instant,
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.springframework.data.mongodb.core.index.Indexed
import org.springframework.data.mongodb.core.mapping.DBRef
import org.springframework.data.mongodb.core.mapping.Document
import tw.waterballsa.gaas.domain.GameRegistration
import java.time.Instant

@Document
class GameRegistrationData(
Expand All @@ -20,6 +21,7 @@ class GameRegistrationData(
var maxPlayers: Int?,
var frontEndUrl: String?,
var backEndUrl: String?,
val createdOn: Instant?,
) {
@DBRef
var logs: MutableList<GameDevelopmentLog> = mutableListOf()
Expand All @@ -35,7 +37,8 @@ class GameRegistrationData(
minPlayers!!,
maxPlayers!!,
frontEndUrl!!,
backEndUrl!!
backEndUrl!!,
createdOn!!
)
}

Expand All @@ -50,5 +53,6 @@ fun GameRegistration.toData(): GameRegistrationData =
minPlayers = minPlayers,
maxPlayers = maxPlayers,
frontEndUrl = frontEndUrl,
backEndUrl = backEndUrl
backEndUrl = backEndUrl,
createdOn = createdOn,
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package tw.waterballsa.gaas.spring.it.controllers

import com.fasterxml.jackson.core.type.TypeReference
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.fail
import org.assertj.core.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
Expand Down Expand Up @@ -198,6 +197,7 @@ class GameRegistrationControllerTest @Autowired constructor(
assertThat(it.maxPlayers).isEqualTo(maxPlayers)
assertThat(it.frontEndUrl).isEqualTo(frontEndUrl)
assertThat(it.backEndUrl).isEqualTo(backEndUrl)
assertThat(it.createdOn).isEqualTo(createdOn)
}
}

Expand Down Expand Up @@ -259,6 +259,7 @@ class GameRegistrationControllerTest @Autowired constructor(
assertThat(it.imageUrl).isEqualTo(img)
assertThat(it.minPlayers).isEqualTo(minPlayers)
assertThat(it.maxPlayers).isEqualTo(maxPlayers)
assertThat(it.createdOn).isEqualTo(createdOn)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import tw.waterballsa.gaas.spring.models.TestGetRoomsRequest
import tw.waterballsa.gaas.spring.models.TestJoinRoomRequest
import tw.waterballsa.gaas.spring.utils.MockitoUtils.Companion.anyObject
import tw.waterballsa.gaas.spring.utils.Users.Companion.defaultUser
import java.time.Instant
import java.util.UUID.randomUUID
import kotlin.reflect.KClass

Expand Down Expand Up @@ -687,7 +688,8 @@ class RoomControllerTest @Autowired constructor(
minPlayers = 2,
maxPlayers = 4,
frontEndUrl = "https://example.com/play/game01",
backEndUrl = "https://example.com/api/game01"
backEndUrl = "https://example.com/api/game01",
createdOn = Instant.parse("2024-03-01T14:00:00.00Z"),
)
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tw.waterballsa.gaas.spring.models

import tw.waterballsa.gaas.domain.GameRegistration
import java.time.Instant

class TestGameRegistrationRequest(
val uniqueName: String,
Expand All @@ -23,6 +24,7 @@ class TestGameRegistrationRequest(
minPlayers = minPlayers,
maxPlayers = maxPlayers,
frontEndUrl = frontEndUrl,
backEndUrl = backEndUrl
backEndUrl = backEndUrl,
createdOn = Instant.parse("2024-03-01T14:00:00.00Z"),
)
}

0 comments on commit 81ce439

Please sign in to comment.