Compare commits

..

1 Commits

Author SHA1 Message Date
gabatxo1312
85a9e9d440
Add paginate 2026-01-29 17:06:50 +01:00
3 changed files with 32 additions and 28 deletions

View File

@ -72,19 +72,22 @@ impl BookOperator {
}
pub async fn list_paginate(&self, page: u64) -> Result<BooksPaginate, BookError> {
let page = if page > 0 { page - 1 } else { 0 };
let page = if page > 0 { page } else { 1 }; // keep 1-indexed
let page_0indexed = page - 1; // convert for SeaORM (0-based index)
let book_pages = Entity::find()
.order_by_desc(Column::Id)
.paginate(&self.state.db, 1);
.paginate(&self.state.db, 100);
let books = book_pages.fetch_page(page).await.context(DBSnafu)?;
let current_page = book_pages.cur_page();
let books = book_pages
.fetch_page(page_0indexed)
.await
.context(DBSnafu)?;
let total_page = book_pages.num_pages().await.context(DBSnafu)?;
Ok(BooksPaginate {
books,
current_page,
current_page: page,
total_page,
})
}

View File

@ -46,11 +46,10 @@ pub async fn index(
State(state): State<AppState>,
Query(pagination): Query<Pagination>,
) -> Result<impl axum::response::IntoResponse, AppStateError> {
let page: u64 = if let Some(page) = pagination.page {
page.try_into().unwrap()
} else {
1
};
let page: u64 = pagination
.page
.map(|p| p.max(1) as u64) // Minimum 1
.unwrap_or(1);
let users = UserOperator::new(state.clone())
.list()

View File

@ -41,14 +41,15 @@
</tbody>
</table>
{% if total_page > 1 %}
<nav aria-label="Page navigation">
<ul class="pagination">
<li class="page-item {% if current_page == 1 %}disabled{% endif %}">
<a class="page-link" href="/?page={{ current_page - 1 }}">Prev</a>
<li class="page-item {% if current_page <= 1 %}disabled{% endif %}">
<a class="page-link" href="/?page={% if current_page > 1 %}{{ current_page - 1 }}{% else %}1{% endif %}">Prev</a>
</li>
{% for page in (current_page - 1)..(current_page + 1) %}
{% if page >= 1 && page <= total_page %}
{% for page in 1..(total_page + 1) %}
{% if page >= current_page - 1 && page <= current_page + 1 %}
<li class="page-item {% if page == current_page %}active{% endif %}">
<a class="page-link" href="/?page={{ page }}">{{ page }}</a>
</li>
@ -60,5 +61,6 @@
</li>
</ul>
</nav>
{% endif %}
{% endcall %}
{% endblock %}