From 0a2b9f985b67a6c21f5c5ab8468aae2048f00484 Mon Sep 17 00:00:00 2001 From: KuoChe Date: Sat, 1 Jun 2024 17:05:24 +0800 Subject: [PATCH] feature: find game sort by --- .../GameRegistrationRepository.kt | 2 +- .../usecases/GetGameRegistrationsUsecase.kt | 8 ++++++-- .../controllers/GameRegistrationController.kt | 6 ++++-- .../SpringGameRegistrationRepository.kt | 19 +++++++++++++++++-- .../GameRegistrationControllerTest.kt | 16 ++++++++++++++++ 5 files changed, 44 insertions(+), 7 deletions(-) diff --git a/application/src/main/kotlin/tw/waterballsa/gaas/application/repositories/GameRegistrationRepository.kt b/application/src/main/kotlin/tw/waterballsa/gaas/application/repositories/GameRegistrationRepository.kt index 451c2397..d06699a8 100644 --- a/application/src/main/kotlin/tw/waterballsa/gaas/application/repositories/GameRegistrationRepository.kt +++ b/application/src/main/kotlin/tw/waterballsa/gaas/application/repositories/GameRegistrationRepository.kt @@ -9,7 +9,7 @@ interface GameRegistrationRepository { fun deleteAll() fun getNumberOfTotalGameRegistrations(): Long fun existsByUniqueName(uniqueName: String): Boolean - fun findGameRegistrations(): List + fun findGameRegistrations(sortBy: String? = null): List fun findById(id: Id): GameRegistration? fun updateGame(gameRegistration: GameRegistration): GameRegistration } diff --git a/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/GetGameRegistrationsUsecase.kt b/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/GetGameRegistrationsUsecase.kt index 80e107dc..4b61afd7 100644 --- a/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/GetGameRegistrationsUsecase.kt +++ b/application/src/main/kotlin/tw/waterballsa/gaas/application/usecases/GetGameRegistrationsUsecase.kt @@ -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) } + + data class Request( + val sortBy: String?, + ) } 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 eb189dc7..b5317cca 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 @@ -35,9 +35,11 @@ class GameRegistrationController( } @GetMapping - fun findGameRegistrations(): List { + fun findGameRegistrations( + @RequestParam(value = "sort_by", required = false) sortBy: String?, + ): List { val presenter = GetGameRegistrationPresenter() - getGameRegistrationsUsecase.execute(presenter) + getGameRegistrationsUsecase.execute(GetGameRegistrationsUsecase.Request(sortBy), presenter) return presenter.viewModel } diff --git a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/SpringGameRegistrationRepository.kt b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/SpringGameRegistrationRepository.kt index 3e0e0776..3d3372ac 100644 --- a/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/SpringGameRegistrationRepository.kt +++ b/spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/SpringGameRegistrationRepository.kt @@ -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 @@ -27,12 +29,25 @@ class SpringGameRegistrationRepository( override fun existsByUniqueName(uniqueName: String): Boolean = gameRegistrationDAO.existsByUniqueName(uniqueName) - override fun findGameRegistrations(): List = - gameRegistrationDAO.findAll().map(GameRegistrationData::toDomain) + override fun findGameRegistrations(sortBy: String?): List { + 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) { + 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] } + } + } } 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 59948f78..7bf580ca 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 @@ -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>() {}) + } + @Test fun givenBig2HasRegistered_whenUpdateGameRegistrationWithWrongId_thenShouldReturnGameRegistrationNotFound() { givenGameHasRegistered("big2", "Big2")