Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: find game sort by #218

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading