updating users with pagination
This commit is contained in:
@@ -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<String> = accountRegistryService.getAccounts(page, size)
|
||||
): Mono<String> = getAccounts(DEFAULT_PAGE, DEFAULT_SIZE, model)
|
||||
|
||||
@GetMapping("/page-{page}")
|
||||
fun getAccountsPage(
|
||||
@PathVariable page: Int,
|
||||
model: Model,
|
||||
): Mono<String> = getAccounts(page, DEFAULT_SIZE, model)
|
||||
|
||||
@GetMapping("/page-{page}/show-{size}")
|
||||
fun getAccountsPageSize(
|
||||
@PathVariable page: Int,
|
||||
@PathVariable size: Int,
|
||||
model: Model,
|
||||
): Mono<String> = 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"))
|
||||
}
|
||||
|
||||
|
||||
17
src/main/kotlin/ltd/hlaeja/dto/Pagination.kt
Normal file
17
src/main/kotlin/ltd/hlaeja/dto/Pagination.kt
Normal file
@@ -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
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
<main>
|
||||
<h1>Test</h1>
|
||||
<hr>
|
||||
<div>Show page <span th:text="${pagination.page}"/> items <span th:text="${pagination.start}"/> - <span th:text="${pagination.end}"/></div>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Id</th>
|
||||
@@ -20,10 +21,18 @@
|
||||
<td th:text="${item.username}">username</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<a th:href="@{/account(page=${page}+1, size=${size})}">Next</a>
|
||||
<a th:href="@{/account(page=${page}-1, size=${size})}" th:unless="${page == 1}">Previous</a>
|
||||
|
||||
<div th:if="${pagination.showSize}">
|
||||
<span th:if="${pagination.first}">Previous</span>
|
||||
<a th:unless="${pagination.first}" th:href="@{'/account/page-' + ${pagination.previous} + '/show-' + ${size}}">Previous</a>
|
||||
<a th:if="${pagination.hasMore}" th:href="@{'/account/page-' + ${pagination.next} + '/show-' + ${size}}">Next</a>
|
||||
<span th:unless="${pagination.hasMore}">Next</span>
|
||||
</div>
|
||||
<div th:unless="${pagination.showSize}">
|
||||
<span th:if="${pagination.first}">Previous</span>
|
||||
<a th:unless="${pagination.first}" th:href="@{'/account/page-' + ${pagination.previous}}">Previous</a>
|
||||
<a th:if="${pagination.hasMore}" th:href="@{'/account/page-' + ${pagination.next}}">Next</a>
|
||||
<span th:unless="${pagination.hasMore}">Next</span>
|
||||
</div>
|
||||
<br>
|
||||
<a href="/logout">Logout</a>
|
||||
</main>
|
||||
|
||||
Reference in New Issue
Block a user