-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#9 refactored REST module to be more hexagonal
- Loading branch information
Showing
15 changed files
with
244 additions
and
227 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
76 changes: 76 additions & 0 deletions
76
backend/src/main/kotlin/org/quizmania/rest/adapter/in/rest/GameReadController.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,76 @@ | ||
package org.quizmania.rest.adapter.`in`.rest | ||
|
||
import mu.KLogging | ||
import org.quizmania.rest.application.domain.Game | ||
import org.quizmania.rest.application.domain.GameStatus | ||
import org.quizmania.rest.application.domain.GameUser | ||
import org.quizmania.rest.port.`in`.FindGamePort | ||
import org.springframework.http.MediaType | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.transaction.annotation.Transactional | ||
import org.springframework.web.bind.annotation.* | ||
import java.util.* | ||
|
||
@RestController | ||
@RequestMapping(value = ["/api/game"], produces = [MediaType.APPLICATION_JSON_VALUE]) | ||
@Transactional | ||
class GameReadController( | ||
val findGamePort: FindGamePort | ||
) { | ||
|
||
companion object : KLogging() | ||
|
||
|
||
@GetMapping("/") | ||
fun search( | ||
@RequestParam( | ||
name = "gameStatus", | ||
required = false | ||
) gameStatus: GameStatus? | ||
): ResponseEntity<List<GameDto>> { | ||
val games = if (gameStatus != null) findGamePort.findByStatus(gameStatus) else findGamePort.findAll() | ||
return ResponseEntity.ok(games.map { it.toDto() }) | ||
} | ||
|
||
@GetMapping("/{gameId}") | ||
fun get(@PathVariable("gameId") gameId: UUID): ResponseEntity<GameDto> { | ||
findGamePort.findById(gameId)?.let { | ||
return ResponseEntity.ok(it.toDto()) | ||
} | ||
|
||
return ResponseEntity.notFound().build() | ||
} | ||
} | ||
|
||
data class GameDto( | ||
val id: UUID, | ||
val name: String, | ||
val maxPlayers: Int, | ||
val numQuestions: Int, | ||
val creator: String, | ||
val moderator: String?, | ||
val status: GameStatus, | ||
val users: List<GameUserDto>, | ||
) | ||
|
||
data class GameUserDto( | ||
val id: UUID, | ||
val name: String, | ||
) | ||
|
||
fun Game.toDto(): GameDto { | ||
return GameDto( | ||
id = gameId, | ||
name = name, | ||
maxPlayers = maxPlayers, | ||
numQuestions = numQuestions, | ||
creator = creator, | ||
moderator = moderator, | ||
status = status, | ||
users = users.map { it.toDto() }, | ||
) | ||
} | ||
|
||
fun GameUser.toDto(): GameUserDto { | ||
return GameUserDto(gameUserId, username) | ||
} |
93 changes: 82 additions & 11 deletions
93
backend/src/main/kotlin/org/quizmania/rest/adapter/out/GameJPARepositoryAdapter.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 |
---|---|---|
@@ -1,36 +1,107 @@ | ||
package org.quizmania.rest.adapter.out | ||
|
||
import jakarta.persistence.* | ||
import org.quizmania.game.common.GameId | ||
import org.quizmania.rest.application.domain.GameEntity | ||
import org.quizmania.rest.application.domain.Game | ||
import org.quizmania.rest.application.domain.GameStatus | ||
import org.quizmania.rest.application.domain.GameUser | ||
import org.quizmania.rest.port.out.GameRepository | ||
import org.springframework.data.repository.CrudRepository | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Component | ||
import java.util.* | ||
|
||
@Component | ||
class GameJPARepositoryAdapter( | ||
val repository: GameJPARepository | ||
) : GameRepository { | ||
override fun save(game: GameEntity) { | ||
repository.save(game) | ||
override fun save(game: Game) { | ||
repository.save(GameEntity.fromModel(game)) | ||
} | ||
|
||
override fun findById(gameId: GameId): GameEntity? { | ||
return repository.findByIdOrNull(gameId) | ||
override fun findById(gameId: GameId): Game? { | ||
return repository.findByIdOrNull(gameId)?.toModel() | ||
} | ||
|
||
override fun findAll(): List<GameEntity> { | ||
return repository.findAll().toList() | ||
override fun findAll(): List<Game> { | ||
return repository.findAll().map { it.toModel() } .toList() | ||
} | ||
|
||
override fun findByStatus(status: GameStatus): List<GameEntity> { | ||
return repository.findByStatus(status) | ||
override fun findByStatus(status: GameStatus): List<Game> { | ||
return repository.findByStatus(status).map { it.toModel() } | ||
} | ||
|
||
} | ||
|
||
interface GameJPARepository : CrudRepository<GameEntity, GameId> { | ||
fun findByStatus(status: GameStatus): List<GameEntity> | ||
} | ||
|
||
@Entity(name = "GAME") | ||
class GameEntity( | ||
@Id | ||
val gameId: UUID, | ||
var name: String, | ||
var maxPlayers: Int, | ||
var numQuestions: Int, | ||
var creator: String, | ||
var moderator: String?, | ||
|
||
var questionTimeout: Long, | ||
@Enumerated(EnumType.STRING) | ||
var status: GameStatus, | ||
|
||
@OneToMany(cascade = [CascadeType.ALL], fetch = FetchType.LAZY) | ||
@JoinColumn(name = "game_id") | ||
var users: MutableList<GameUserEntity> = mutableListOf(), | ||
) { | ||
|
||
companion object { | ||
fun fromModel(model: Game): GameEntity { | ||
return GameEntity( | ||
gameId = model.gameId, | ||
name = model.name, | ||
maxPlayers = model.maxPlayers, | ||
numQuestions = model.numQuestions, | ||
creator = model.creator, | ||
moderator = model.moderator, | ||
questionTimeout = model.questionTimeout, | ||
status = model.status, | ||
users = model.users.map { GameUserEntity.fromModel(it) }.toMutableList() // mutability needed for JPA? | ||
) | ||
} | ||
} | ||
|
||
} | ||
fun toModel(): Game = Game( | ||
gameId = this.gameId, | ||
name = this.name, | ||
maxPlayers = this.maxPlayers, | ||
numQuestions = this.numQuestions, | ||
creator = this.creator, | ||
moderator = this.moderator, | ||
questionTimeout = this.questionTimeout, | ||
status = this.status, | ||
users = this.users.map { it.toModel() }.toMutableList() | ||
) | ||
} | ||
|
||
@Entity(name = "GAME_USER") | ||
class GameUserEntity( | ||
@Id | ||
val gameUserId: UUID, | ||
var username: String, | ||
) { | ||
|
||
companion object { | ||
fun fromModel(model: GameUser): GameUserEntity { | ||
return GameUserEntity( | ||
gameUserId = model.gameUserId, | ||
username = model.username, | ||
) | ||
} | ||
} | ||
|
||
fun toModel(): GameUser = GameUser( | ||
gameUserId = this.gameUserId, | ||
username = this.username, | ||
) | ||
} |
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
8 changes: 8 additions & 0 deletions
8
backend/src/main/kotlin/org/quizmania/rest/application/domain/GameUser.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,8 @@ | ||
package org.quizmania.rest.application.domain | ||
|
||
import java.util.* | ||
|
||
data class GameUser( | ||
val gameUserId: UUID, | ||
val username: String, | ||
) |
Oops, something went wrong.