Skip to content

Commit

Permalink
feature: find game sort by
Browse files Browse the repository at this point in the history
  • Loading branch information
kuoche1712003 committed Jun 1, 2024
1 parent 129350a commit a81493b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface GameRegistrationRepository {
fun deleteAll()
fun getNumberOfTotalGameRegistrations(): Long
fun existsByUniqueName(uniqueName: String): Boolean
fun findGameRegistrations(): List<GameRegistration>
fun findGameRegistrations(sortBy: String? = null): List<GameRegistration>
fun findById(id: Id): GameRegistration?
fun updateGame(gameRegistration: GameRegistration): GameRegistration
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import javax.inject.Named
class GetGameRegistrationsUsecase(
private val gameRegistrationRepository: GameRegistrationRepository,
) {
fun execute(presenter: Presenter) {
presenter.renderGameRegistrations(gameRegistrationRepository.findGameRegistrations())
fun execute(request: Request, presenter: Presenter) {
presenter.renderGameRegistrations(gameRegistrationRepository.findGameRegistrations(request.sortBy))
}

interface Presenter {
fun renderGameRegistrations(gameRegistrations: Collection<GameRegistration>)
}

data class Request(
val sortBy: String?,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ class GameRegistrationController(
}

@GetMapping
fun findGameRegistrations(): List<GetGamesViewModel> {
fun findGameRegistrations(
@RequestParam(value = "sort_by", required = false) sortBy: String?,
): List<GetGamesViewModel> {
val presenter = GetGameRegistrationPresenter()
getGameRegistrationsUsecase.execute(presenter)
getGameRegistrationsUsecase.execute(GetGameRegistrationsUsecase.Request(sortBy), presenter)
return presenter.viewModel
}

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

import org.springframework.data.domain.Sort
import org.springframework.data.domain.Sort.Order
import org.springframework.stereotype.Component
import tw.waterballsa.gaas.application.repositories.GameRegistrationRepository
import tw.waterballsa.gaas.domain.GameRegistration
Expand Down Expand Up @@ -27,12 +29,25 @@ class SpringGameRegistrationRepository(

override fun existsByUniqueName(uniqueName: String): Boolean = gameRegistrationDAO.existsByUniqueName(uniqueName)

override fun findGameRegistrations(): List<GameRegistration> =
gameRegistrationDAO.findAll().map(GameRegistrationData::toDomain)
override fun findGameRegistrations(sortBy: String?): List<GameRegistration> {
return SortBy.from(sortBy)
?.let { Sort.by(it.orders) }
?.run { gameRegistrationDAO.findAll(this).map { it.toDomain() } }
?: gameRegistrationDAO.findAll().map { it.toDomain() }
}

override fun findById(id: Id): GameRegistration? =
gameRegistrationDAO.findById(id.value).mapOrNull(GameRegistrationData::toDomain)

override fun updateGame(gameRegistration: GameRegistration): GameRegistration =
gameRegistrationDAO.save(gameRegistration.toData()).toDomain()

enum class SortBy(val value: String, val orders: List<Order>) {
CREATED_ON("createdOn", listOf(Order.desc("createdOn"), Order.desc("_id")));

companion object {
private val map = SortBy.values().associateBy { it.value }
infix fun from(value: String?): SortBy? = value?.let { map[value] }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ class GameRegistrationControllerTest @Autowired constructor(
getGamesViewModels.forEachIndexed { i, model -> model.validateWithGameRegistration(gameRegistrations[i]) }
}

@Test
fun givenBig2IsNewerThanUno_WhenThenUserViewsGameListByCreateOn_ThenBig2RankInFront() {
val unoRequest = createGameRegistrationRequest("uno-java", "UNO Java")
val big2Request = createGameRegistrationRequest("big2-Java", "Big2 Java")
registerGameSuccessfully(unoRequest)
registerGameSuccessfully(big2Request)

mockMvc.perform(get("/games?sort_by=createdOn"))
.andExpect(status().isOk)
.andExpect(jsonPath("$").isArray)
.andExpect(jsonPath("$.size()").value(2))
.validateSpecificElement(0, big2Request)
.validateSpecificElement(1, unoRequest)
.getBody(object : TypeReference<List<GetGamesViewModel>>() {})
}

@Test
fun givenBig2HasRegistered_whenUpdateGameRegistrationWithWrongId_thenShouldReturnGameRegistrationNotFound() {
givenGameHasRegistered("big2", "Big2")
Expand Down

0 comments on commit a81493b

Please sign in to comment.