-
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.
實作查看房間列表(#72)
- Loading branch information
Showing
10 changed files
with
235 additions
and
10 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
application/src/main/kotlin/tw/waterballsa/gaas/application/model/Pagination.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 tw.waterballsa.gaas.application.model | ||
|
||
class Pagination<T>( | ||
val page: Int, | ||
val offset: Int, | ||
val data: List<T> = emptyList() | ||
) | ||
|
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
application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/GetRoomsUseCase.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.application.usecases | ||
|
||
import tw.waterballsa.gaas.application.model.Pagination | ||
import tw.waterballsa.gaas.application.repositories.RoomRepository | ||
import tw.waterballsa.gaas.domain.Room | ||
import javax.inject.Named | ||
|
||
@Named | ||
class GetRoomsUseCase( | ||
private val roomRepository: RoomRepository, | ||
) { | ||
|
||
fun execute(request: Request, presenter: GetRoomsPresenter) = | ||
roomRepository.findByStatus(request.status, request.toPagination()) | ||
.also { presenter.present(it) } | ||
|
||
class Request( | ||
val status: Room.Status, | ||
val page: Int, | ||
val offset: Int | ||
) | ||
|
||
interface GetRoomsPresenter { | ||
fun present(rooms: Pagination<Room>) | ||
} | ||
} | ||
|
||
private fun GetRoomsUseCase.Request.toPagination(): Pagination<Any> = | ||
Pagination(page, offset) |
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
48 changes: 48 additions & 0 deletions
48
spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/presenter/GetRoomsPresenter.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,48 @@ | ||
package tw.waterballsa.gaas.spring.controllers.presenter | ||
|
||
import tw.waterballsa.gaas.application.model.Pagination | ||
import tw.waterballsa.gaas.application.usecases.GetRoomsUseCase | ||
import tw.waterballsa.gaas.domain.GameRegistration | ||
import tw.waterballsa.gaas.domain.Room | ||
import tw.waterballsa.gaas.spring.controllers.viewmodel.GetRoomsViewModel | ||
|
||
class GetRoomsPresenter : GetRoomsUseCase.GetRoomsPresenter { | ||
lateinit var viewModel: GetRoomsViewModel | ||
private set | ||
|
||
override fun present(rooms: Pagination<Room>) { | ||
viewModel = rooms.toViewModel() | ||
} | ||
|
||
private fun Pagination<Room>.toViewModel(): GetRoomsViewModel = | ||
GetRoomsViewModel( | ||
rooms = data.map { it.toRoomsViewModel() }, | ||
page = toPage(data.size) | ||
) | ||
} | ||
|
||
|
||
private fun Room.toRoomsViewModel(): GetRoomsViewModel.RoomViewModel = | ||
GetRoomsViewModel.RoomViewModel( | ||
id = roomId!!.value, | ||
name = name, | ||
game = game.toGetRoomsView(), | ||
host = host.toGetRoomsView(), | ||
minPlayers = minPlayers, | ||
maxPlayers = maxPlayers, | ||
currentPlayers = players.size, | ||
isLocked = isLocked, | ||
) | ||
|
||
private fun GameRegistration.toGetRoomsView(): GetRoomsViewModel.RoomViewModel.Game = | ||
GetRoomsViewModel.RoomViewModel.Game(id!!.value, displayName) | ||
|
||
private fun Room.Player.toGetRoomsView(): GetRoomsViewModel.RoomViewModel.Player = | ||
GetRoomsViewModel.RoomViewModel.Player(id.value, nickname) | ||
|
||
private fun Pagination<Room>.toPage(size: Int): GetRoomsViewModel.Page = | ||
GetRoomsViewModel.Page( | ||
page = page, | ||
offset = offset, | ||
total = size | ||
) |
26 changes: 26 additions & 0 deletions
26
spring/src/main/kotlin/tw/waterballsa/gaas/spring/controllers/viewmodel/GetRoomsViewModel.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,26 @@ | ||
package tw.waterballsa.gaas.spring.controllers.viewmodel | ||
|
||
data class GetRoomsViewModel( | ||
val rooms: List<RoomViewModel>, | ||
val page: Page | ||
) { | ||
data class RoomViewModel( | ||
val id: String, | ||
val name: String, | ||
val game: Game, | ||
val host: Player, | ||
val maxPlayers: Int, | ||
val minPlayers: Int, | ||
val currentPlayers: Int, | ||
val isLocked: Boolean, | ||
) { | ||
data class Game(val id: String, val name: String) | ||
data class Player(val id: String, val nickname: String) | ||
} | ||
|
||
data class Page( | ||
val total: Int, | ||
val page: Int, | ||
val offset: Int | ||
) | ||
} |
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
4 changes: 4 additions & 0 deletions
4
spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/dao/RoomDAO.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,11 +1,15 @@ | ||
package tw.waterballsa.gaas.spring.repositories.dao | ||
|
||
import org.springframework.data.domain.Page | ||
import org.springframework.data.domain.Pageable | ||
import org.springframework.data.mongodb.repository.MongoRepository | ||
import org.springframework.stereotype.Repository | ||
import tw.waterballsa.gaas.domain.Room | ||
import tw.waterballsa.gaas.spring.repositories.data.RoomData | ||
|
||
@Repository | ||
interface RoomDAO : MongoRepository<RoomData, String> { | ||
fun existsByHostId(hostId: String): Boolean | ||
fun findByStatus(status: Room.Status, pageable: Pageable): Page<RoomData> | ||
} | ||
|
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 |
---|---|---|
|
@@ -9,9 +9,11 @@ import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser | |
import org.springframework.security.oauth2.core.oidc.user.OidcUser | ||
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oidcLogin | ||
import org.springframework.test.web.servlet.ResultActions | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
import tw.waterballsa.gaas.application.model.Pagination | ||
import tw.waterballsa.gaas.application.repositories.GameRegistrationRepository | ||
import tw.waterballsa.gaas.application.repositories.RoomRepository | ||
import tw.waterballsa.gaas.application.repositories.UserRepository | ||
|
@@ -21,6 +23,7 @@ import tw.waterballsa.gaas.domain.Room.Player | |
import tw.waterballsa.gaas.domain.User | ||
import tw.waterballsa.gaas.spring.it.AbstractSpringBootTest | ||
import tw.waterballsa.gaas.spring.models.TestCreateRoomRequest | ||
import tw.waterballsa.gaas.spring.models.TestGetRoomsRequest | ||
import tw.waterballsa.gaas.spring.models.TestJoinRoomRequest | ||
import java.time.Instant.now | ||
|
||
|
@@ -81,7 +84,6 @@ class RoomControllerTest @Autowired constructor( | |
val request = createRoomRequest("1234") | ||
createRoom(request) | ||
.thenCreateRoomSuccessfully(request) | ||
|
||
createRoom(request) | ||
.andExpect(status().isBadRequest) | ||
.andExpect(jsonPath("$.message").value("A user can only create one room at a time.")) | ||
|
@@ -117,6 +119,51 @@ class RoomControllerTest @Autowired constructor( | |
.thenJoinRoomSuccessfully() | ||
} | ||
|
||
@Test | ||
fun givenWaitingRoomBAndWaitingRoomC_WhenUserAVisitLobby_ThenShouldHaveRoomBAndRoomC() { | ||
val userA = testUser | ||
val userB = createUser("2", "[email protected]", "winner1122") | ||
val userC = createUser("3", "[email protected]", "winner1234") | ||
val request = TestGetRoomsRequest("WAITING", 0, 10) | ||
|
||
givenWaitingRooms(userB, userC) | ||
request.whenUserAVisitLobby(userA) | ||
.thenShouldHaveRooms(request) | ||
} | ||
|
||
private fun TestGetRoomsRequest.whenUserAVisitLobby(joinUser: User): ResultActions = | ||
mockMvc.perform( | ||
get("/rooms") | ||
.with(oidcLogin().oidcUser(mockOidcUser(joinUser))) | ||
.param("status", status) | ||
.param("page", page.toString()) | ||
.param("offset", offset.toString()) | ||
) | ||
|
||
private fun givenWaitingRooms(vararg users: User) = | ||
users.forEach { givenTheHostCreatePublicRoom(it) } | ||
|
||
private fun ResultActions.thenShouldHaveRooms(request: TestGetRoomsRequest) { | ||
val rooms = roomRepository.findByStatus(request.toStatus(), request.toPagination()) | ||
andExpect(status().isOk) | ||
.andExpect(jsonPath("$.rooms").isArray) | ||
.andExpect(jsonPath("$.rooms.length()").value(rooms.data.size)) | ||
.roomExcept(rooms) | ||
} | ||
|
||
private fun ResultActions.roomExcept(rooms: Pagination<Room>) { | ||
rooms.data.forEachIndexed() { index, room -> | ||
andExpect(jsonPath("$.rooms[$index].id").value(room.roomId!!.value)) | ||
.andExpect(jsonPath("$.rooms[$index].name").value(room.name)) | ||
.andExpect(jsonPath("$.rooms[$index].game.id").value(room.game.id!!.value)) | ||
.andExpect(jsonPath("$.rooms[$index].host.id").value(room.host.id.value)) | ||
.andExpect(jsonPath("$.rooms[$index].isLocked").value(room.isLocked)) | ||
.andExpect(jsonPath("$.rooms[$index].currentPlayers").value(room.players.size)) | ||
.andExpect(jsonPath("$.rooms[$index].maxPlayers").value(room.maxPlayers)) | ||
.andExpect(jsonPath("$.rooms[$index].minPlayers").value(room.minPlayers)) | ||
} | ||
} | ||
|
||
private fun createRoom(request: TestCreateRoomRequest): ResultActions = | ||
mockMvc.perform( | ||
post("/rooms") | ||
|
@@ -133,17 +180,16 @@ class RoomControllerTest @Autowired constructor( | |
|
||
private fun givenTheHostCreatePublicRoom(host: User): Room { | ||
testRoom = createRoom(host) | ||
return testRoom | ||
return testRoom | ||
} | ||
|
||
private fun givenTheHostCreateRoomWithPassword(host: User, password: String): Room { | ||
testRoom = createRoom(host, password) | ||
return testRoom | ||
|
||
} | ||
|
||
private fun Room.whenUserJoinTheRoom(user: User, password: String? = null):ResultActions{ | ||
val request = joinRoomRequest(password); | ||
private fun Room.whenUserJoinTheRoom(user: User, password: String? = null): ResultActions { | ||
val request = joinRoomRequest(password) | ||
val joinUser = mockOidcUser(user) | ||
return joinRoom(request, joinUser) | ||
} | ||
|
@@ -191,7 +237,7 @@ class RoomControllerTest @Autowired constructor( | |
) | ||
) | ||
|
||
private fun createRoom(host : User, password: String? = null): Room = roomRepository.createRoom( | ||
private fun createRoom(host: User, password: String? = null): Room = roomRepository.createRoom( | ||
Room( | ||
game = testGame, | ||
host = Player(Player.Id(host.id!!.value), host.nickname), | ||
|
@@ -229,5 +275,4 @@ class RoomControllerTest @Autowired constructor( | |
TestJoinRoomRequest( | ||
password = password | ||
) | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
spring/src/test/kotlin/tw/waterballsa/gaas/spring/models/TestGetRoomsRequest.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,13 @@ | ||
package tw.waterballsa.gaas.spring.models | ||
|
||
import tw.waterballsa.gaas.application.model.Pagination | ||
import tw.waterballsa.gaas.domain.Room | ||
|
||
class TestGetRoomsRequest( | ||
val status: String, | ||
val page: Int, | ||
val offset: Int | ||
) { | ||
fun toStatus() : Room.Status = Room.Status.valueOf(status) | ||
fun toPagination(): Pagination<Any> = Pagination(page, offset) | ||
} |