updating users with pagination

This commit is contained in:
2025-01-26 14:24:10 +01:00
parent c40f1a0036
commit 34349653db
3 changed files with 59 additions and 11 deletions

View File

@@ -1,11 +1,12 @@
package ltd.hlaeja.controller package ltd.hlaeja.controller
import ltd.hlaeja.dto.Pagination
import ltd.hlaeja.service.AccountRegistryService import ltd.hlaeja.service.AccountRegistryService
import org.springframework.stereotype.Controller import org.springframework.stereotype.Controller
import org.springframework.ui.Model import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping 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.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
@Controller @Controller
@@ -13,18 +14,39 @@ import reactor.core.publisher.Mono
class AccountController( class AccountController(
private val accountRegistryService: AccountRegistryService, private val accountRegistryService: AccountRegistryService,
) { ) {
companion object {
const val DEFAULT_PAGE: Int = 1
const val DEFAULT_SIZE: Int = 25
}
@GetMapping @GetMapping
fun getAccounts( fun getDefaultAccounts(
@RequestParam(defaultValue = "1") page: Int,
@RequestParam(defaultValue = "2") size: Int,
model: Model, 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() .collectList()
.doOnNext { items -> .doOnNext { items ->
model.addAttribute("items", items) model.addAttribute("items", items)
model.addAttribute("page", page) model.addAttribute("pagination", Pagination(page, size, items.size, DEFAULT_SIZE))
model.addAttribute("size", size)
} }
.then(Mono.just("account/users")) .then(Mono.just("account/users"))
} }

View 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
}

View File

@@ -8,6 +8,7 @@
<main> <main>
<h1>Test</h1> <h1>Test</h1>
<hr> <hr>
<div>Show page <span th:text="${pagination.page}"/> items <span th:text="${pagination.start}"/> - <span th:text="${pagination.end}"/></div>
<table> <table>
<tr> <tr>
<th>Id</th> <th>Id</th>
@@ -20,10 +21,18 @@
<td th:text="${item.username}">username</td> <td th:text="${item.username}">username</td>
</tr> </tr>
</table> </table>
<div th:if="${pagination.showSize}">
<a th:href="@{/account(page=${page}+1, size=${size})}">Next</a> <span th:if="${pagination.first}">Previous</span>
<a th:href="@{/account(page=${page}-1, size=${size})}" th:unless="${page == 1}">Previous</a> <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> <br>
<a href="/logout">Logout</a> <a href="/logout">Logout</a>
</main> </main>