Compare commits

..

1 Commits

Author SHA1 Message Date
gabatxo1312
46c9b89ccc
Add paginate 2026-01-28 23:26:37 +01:00
3 changed files with 28 additions and 32 deletions

View File

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

View File

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

View File

@ -41,26 +41,24 @@
</tbody> </tbody>
</table> </table>
{% if total_page > 1 %} <nav aria-label="Page navigation">
<nav aria-label="Page navigation"> <ul class="pagination">
<ul class="pagination"> <li class="page-item {% if current_page == 1 %}disabled{% endif %}">
<li class="page-item {% if current_page <= 1 %}disabled{% endif %}"> <a class="page-link" href="/?page={{ current_page - 1 }}">Prev</a>
<a class="page-link" href="/?page={% if current_page > 1 %}{{ current_page - 1 }}{% else %}1{% endif %}">Prev</a> </li>
</li>
{% 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>
{% endif %}
{% endfor %}
<li class="page-item {% if current_page == total_page %}disabled{% endif %}"> {% for page in (current_page - 1)..(current_page + 1) %}
<a class="page-link" href="/?page={{ current_page + 1 }}">Next</a> {% if page >= 1 && page <= total_page %}
</li> <li class="page-item {% if page == current_page %}active{% endif %}">
</ul> <a class="page-link" href="/?page={{ page }}">{{ page }}</a>
</nav> </li>
{% endif %} {% endif %}
{% endfor %}
<li class="page-item {% if current_page == total_page %}disabled{% endif %}">
<a class="page-link" href="/?page={{ current_page + 1 }}">Next</a>
</li>
</ul>
</nav>
{% endcall %} {% endcall %}
{% endblock %} {% endblock %}