-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature: update game registration * feature: update game registration usecase * test: update game registration * chore: coding format * refactor: coding style * fix: test failed issue
- Loading branch information
Showing
10 changed files
with
307 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...src/main/kotlin/tw/waterballsa/gaas/application/usecases/UpdateGameRegistrationUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package tw.waterballsa.gaas.application.usecases | ||
|
||
import tw.waterballsa.gaas.application.eventbus.EventBus | ||
import tw.waterballsa.gaas.application.repositories.GameRegistrationRepository | ||
import tw.waterballsa.gaas.domain.GameRegistration | ||
import tw.waterballsa.gaas.events.GameRegistrationUpdatedEvent | ||
import tw.waterballsa.gaas.exceptions.NotFoundException.Companion.notFound | ||
import tw.waterballsa.gaas.exceptions.PlatformException | ||
import tw.waterballsa.gaas.exceptions.enums.PlatformError.GAME_EXISTS | ||
import tw.waterballsa.gaas.exceptions.enums.PlatformError.GAME_NOT_FOUND | ||
import javax.inject.Named | ||
|
||
@Named | ||
class UpdateGameRegistrationUseCase( | ||
private val gameRegistrationRepository: GameRegistrationRepository, | ||
private val eventBus: EventBus, | ||
) { | ||
|
||
fun execute(request: Request, presenter: Presenter) { | ||
with(request) { | ||
validateGameExist() | ||
validateUniqueNameDuplicated() | ||
val gameRegistration = updateGameRegistration() | ||
|
||
val event = gameRegistration.toGameRegistrationUpdatedEvent() | ||
presenter.present(event) | ||
eventBus.broadcast(event) | ||
} | ||
} | ||
|
||
private fun Request.validateGameExist() { | ||
gameRegistrationRepository.findById(gameId) | ||
?: throw notFound(GAME_NOT_FOUND, GameRegistration::class).id(gameId) | ||
} | ||
|
||
private fun Request.validateUniqueNameDuplicated() { | ||
gameRegistrationRepository.findGameRegistrationByUniqueName(uniqueName) | ||
?.takeIf { it.id != gameId } | ||
?.let { | ||
throw PlatformException( | ||
GAME_EXISTS, | ||
"$uniqueName already exists", | ||
) | ||
} | ||
} | ||
|
||
private fun Request.updateGameRegistration(): GameRegistration = | ||
gameRegistrationRepository.updateGame(toGameRegistration()) | ||
|
||
data class Request( | ||
val gameId: GameRegistration.Id, | ||
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, | ||
) { | ||
fun toGameRegistration(): GameRegistration = GameRegistration( | ||
gameId, | ||
uniqueName, | ||
displayName, | ||
shortDescription, | ||
rule, | ||
imageUrl, | ||
minPlayers, | ||
maxPlayers, | ||
frontEndUrl, | ||
backEndUrl, | ||
) | ||
} | ||
} | ||
|
||
private fun GameRegistration.toGameRegistrationUpdatedEvent(): GameRegistrationUpdatedEvent = | ||
GameRegistrationUpdatedEvent( | ||
id!!, | ||
uniqueName, | ||
displayName, | ||
shortDescription, | ||
rule, | ||
imageUrl, | ||
minPlayers, | ||
maxPlayers, | ||
frontEndUrl, | ||
backEndUrl, | ||
) |
16 changes: 16 additions & 0 deletions
16
domain/src/main/kotlin/tw/waterballsa/gaas/events/GameRegistrationUpdatedEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package tw.waterballsa.gaas.events | ||
|
||
import tw.waterballsa.gaas.domain.GameRegistration | ||
|
||
class GameRegistrationUpdatedEvent( | ||
val id: GameRegistration.Id, | ||
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, | ||
) : DomainEvent() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...g/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/UpdateGameRegistrationRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package tw.waterballsa.gaas.spring.controllers | ||
|
||
import tw.waterballsa.gaas.application.usecases.UpdateGameRegistrationUseCase | ||
import tw.waterballsa.gaas.domain.GameRegistration | ||
|
||
data class UpdateGameRegistrationRequest( | ||
private val uniqueName: String, | ||
private val displayName: String, | ||
private val shortDescription: String, | ||
private val rule: String, | ||
private val imageUrl: String, | ||
private val minPlayers: Int, | ||
private val maxPlayers: Int, | ||
private val frontEndUrl: String, | ||
private val backEndUrl: String, | ||
) { | ||
fun toRequest(gameId: String): UpdateGameRegistrationUseCase.Request = UpdateGameRegistrationUseCase.Request( | ||
GameRegistration.Id(gameId), | ||
uniqueName, | ||
displayName, | ||
shortDescription, | ||
rule, | ||
imageUrl, | ||
minPlayers, | ||
maxPlayers, | ||
frontEndUrl, | ||
backEndUrl | ||
) | ||
} |
32 changes: 32 additions & 0 deletions
32
...otlin/tw/waterballsa/gaas/spring/controllers/presenter/UpdateGameRegistrationPresenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package tw.waterballsa.gaas.spring.controllers.presenter | ||
|
||
import tw.waterballsa.gaas.application.usecases.Presenter | ||
import tw.waterballsa.gaas.events.DomainEvent | ||
import tw.waterballsa.gaas.events.GameRegistrationUpdatedEvent | ||
import tw.waterballsa.gaas.spring.controllers.viewmodel.UpdateGameRegistrationViewModel | ||
import tw.waterballsa.gaas.spring.extensions.getEvent | ||
|
||
class UpdateGameRegistrationPresenter : Presenter { | ||
|
||
lateinit var viewModel: UpdateGameRegistrationViewModel | ||
private set | ||
|
||
override fun present(vararg events: DomainEvent) { | ||
viewModel = events.getEvent(GameRegistrationUpdatedEvent::class)!!.toViewModel() | ||
} | ||
|
||
private fun GameRegistrationUpdatedEvent.toViewModel(): UpdateGameRegistrationViewModel = | ||
UpdateGameRegistrationViewModel( | ||
id, | ||
uniqueName, | ||
displayName, | ||
shortDescription, | ||
rule, | ||
imageUrl, | ||
minPlayers, | ||
maxPlayers, | ||
frontEndUrl, | ||
backEndUrl | ||
) | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...otlin/tw/waterballsa/gaas/spring/controllers/viewmodel/UpdateGameRegistrationViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package tw.waterballsa.gaas.spring.controllers.viewmodel | ||
|
||
import tw.waterballsa.gaas.domain.GameRegistration | ||
|
||
data class UpdateGameRegistrationViewModel( | ||
val id: GameRegistration.Id, | ||
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters