Compare commits
1 Commits
46c9b89ccc
...
85a9e9d440
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
85a9e9d440 |
@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -41,24 +41,26 @@
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<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>
|
||||
{% 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={% 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 %}
|
||||
<li class="page-item {% if page == current_page %}active{% endif %}">
|
||||
<a class="page-link" href="/?page={{ page }}">{{ page }}</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% 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 %}">
|
||||
<a class="page-link" href="/?page={{ current_page + 1 }}">Next</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
<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>
|
||||
{% endif %}
|
||||
{% endcall %}
|
||||
{% endblock %}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user