diff --git a/src/main/kotlin/ltd/hlaeja/controller/AccountController.kt b/src/main/kotlin/ltd/hlaeja/controller/AccountController.kt index f698201..0998c02 100644 --- a/src/main/kotlin/ltd/hlaeja/controller/AccountController.kt +++ b/src/main/kotlin/ltd/hlaeja/controller/AccountController.kt @@ -1,11 +1,12 @@ package ltd.hlaeja.controller +import ltd.hlaeja.dto.Pagination import ltd.hlaeja.service.AccountRegistryService import org.springframework.stereotype.Controller import org.springframework.ui.Model import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RequestParam import reactor.core.publisher.Mono @Controller @@ -13,18 +14,39 @@ import reactor.core.publisher.Mono class AccountController( private val accountRegistryService: AccountRegistryService, ) { + companion object { + const val DEFAULT_PAGE: Int = 1 + const val DEFAULT_SIZE: Int = 25 + } @GetMapping - fun getAccounts( - @RequestParam(defaultValue = "1") page: Int, - @RequestParam(defaultValue = "2") size: Int, + fun getDefaultAccounts( model: Model, - ): Mono = accountRegistryService.getAccounts(page, size) + ): Mono = getAccounts(DEFAULT_PAGE, DEFAULT_SIZE, model) + + @GetMapping("/page-{page}") + fun getAccountsPage( + @PathVariable page: Int, + model: Model, + ): Mono = getAccounts(page, DEFAULT_SIZE, model) + + @GetMapping("/page-{page}/show-{size}") + fun getAccountsPageSize( + @PathVariable page: Int, + @PathVariable size: Int, + model: Model, + ): Mono = getAccounts(page, size, model) + + private fun getAccounts( + page: Int, + size: Int, + model: Model, + ) = accountRegistryService.getAccounts(page, size) .collectList() .doOnNext { items -> model.addAttribute("items", items) - model.addAttribute("page", page) - model.addAttribute("size", size) + model.addAttribute("pagination", Pagination(page, size, items.size, DEFAULT_SIZE)) } .then(Mono.just("account/users")) } + diff --git a/src/main/kotlin/ltd/hlaeja/dto/Pagination.kt b/src/main/kotlin/ltd/hlaeja/dto/Pagination.kt new file mode 100644 index 0000000..2e625a1 --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/dto/Pagination.kt @@ -0,0 +1,17 @@ +package ltd.hlaeja.dto + +@Suppress("unused") +data class Pagination( + val page: Int, + val size: Int, + val items: Int, + val defaultSize: Int, +) { + val hasMore: Boolean = size == items + val showSize: Boolean = size != defaultSize + val first: Boolean = page <= 1 + val previous: Int = page - 1 + val next: Int = page + 1 + val start: Int = (page - 1) * size + 1 + val end: Int = page * size +} diff --git a/src/main/resources/templates/account/users.html b/src/main/resources/templates/account/users.html index a52cc4c..f861dec 100644 --- a/src/main/resources/templates/account/users.html +++ b/src/main/resources/templates/account/users.html @@ -8,6 +8,7 @@

Test


+
Show page items -
@@ -20,10 +21,18 @@
Idusername
- - Next - Previous - +
+ Previous + Previous + Next + Next +
+
+ Previous + Previous + Next + Next +

Logout