diff --git a/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/RegisterGameUsecase.kt b/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/RegisterGameUsecase.kt index b0d4a3f4..ecb437a2 100644 --- a/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/RegisterGameUsecase.kt +++ b/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/RegisterGameUsecase.kt @@ -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 @@ -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 = @@ -70,4 +73,5 @@ private fun GameRegistration.toRegisteredGameEvent(): RegisteredGameEvent = maxPlayers, frontEndUrl, backEndUrl, + createdOn, ) diff --git a/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/UpdateGameRegistrationUseCase.kt b/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/UpdateGameRegistrationUseCase.kt index cde27f1f..ce2ea580 100644 --- a/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/UpdateGameRegistrationUseCase.kt +++ b/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/UpdateGameRegistrationUseCase.kt @@ -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, @@ -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 = diff --git a/domain/src/main/kotlin/tw/waterballsa/gaas/domain/GameRegistration.kt b/domain/src/main/kotlin/tw/waterballsa/gaas/domain/GameRegistration.kt index 3a5203d6..0df8c9db 100644 --- a/domain/src/main/kotlin/tw/waterballsa/gaas/domain/GameRegistration.kt +++ b/domain/src/main/kotlin/tw/waterballsa/gaas/domain/GameRegistration.kt @@ -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) diff --git a/domain/src/main/kotlin/tw/waterballsa/gaas/events/RegisteredGameEvent.kt b/domain/src/main/kotlin/tw/waterballsa/gaas/events/RegisteredGameEvent.kt index fdb9bcb4..45ee5296 100644 --- a/domain/src/main/kotlin/tw/waterballsa/gaas/events/RegisteredGameEvent.kt +++ b/domain/src/main/kotlin/tw/waterballsa/gaas/events/RegisteredGameEvent.kt @@ -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, @@ -13,4 +14,5 @@ data class RegisteredGameEvent( val maxPlayers: Int, val frontEndUrl: String, val backEndUrl: String, + val createdOn: Instant, ) : DomainEvent() diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/GameRegistrationController.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/GameRegistrationController.kt index 365a4cad..f65fb835 100644 --- a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/GameRegistrationController.kt +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/GameRegistrationController.kt @@ -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") @@ -95,7 +96,8 @@ class RegisterGamePresenter : Presenter { minPlayers, maxPlayers, frontEndUrl, - backEndUrl + backEndUrl, + createdOn, ) data class RegisterGameViewModel( @@ -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, ) } diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/data/GameRegistrationData.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/data/GameRegistrationData.kt index bcec06eb..6ef8e23e 100644 --- a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/data/GameRegistrationData.kt +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/data/GameRegistrationData.kt @@ -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( @@ -20,6 +21,7 @@ class GameRegistrationData( var maxPlayers: Int?, var frontEndUrl: String?, var backEndUrl: String?, + val createdOn: Instant?, ) { @DBRef var logs: MutableList = mutableListOf() @@ -35,7 +37,8 @@ class GameRegistrationData( minPlayers!!, maxPlayers!!, frontEndUrl!!, - backEndUrl!! + backEndUrl!!, + createdOn!! ) } @@ -50,5 +53,6 @@ fun GameRegistration.toData(): GameRegistrationData = minPlayers = minPlayers, maxPlayers = maxPlayers, frontEndUrl = frontEndUrl, - backEndUrl = backEndUrl + backEndUrl = backEndUrl, + createdOn = createdOn, ) diff --git a/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/controllers/GameRegistrationControllerTest.kt b/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/controllers/GameRegistrationControllerTest.kt index b4fcc033..49a7defd 100644 --- a/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/controllers/GameRegistrationControllerTest.kt +++ b/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/controllers/GameRegistrationControllerTest.kt @@ -198,6 +198,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) } } diff --git a/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/controllers/RoomControllerTest.kt b/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/controllers/RoomControllerTest.kt index c21918ba..aa5ecf27 100644 --- a/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/controllers/RoomControllerTest.kt +++ b/spring/src/test/kotlin/tw/waterballsa/gaas/spring/it/controllers/RoomControllerTest.kt @@ -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 @@ -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"), ) ) diff --git a/spring/src/test/kotlin/tw/waterballsa/gaas/spring/models/TestGameRegistrationRequest.kt b/spring/src/test/kotlin/tw/waterballsa/gaas/spring/models/TestGameRegistrationRequest.kt index 2dccdff1..eb283d48 100644 --- a/spring/src/test/kotlin/tw/waterballsa/gaas/spring/models/TestGameRegistrationRequest.kt +++ b/spring/src/test/kotlin/tw/waterballsa/gaas/spring/models/TestGameRegistrationRequest.kt @@ -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, @@ -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"), ) }