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> {
|
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()
|
let book_pages = Entity::find()
|
||||||
.order_by_desc(Column::Id)
|
.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 books = book_pages
|
||||||
let current_page = book_pages.cur_page();
|
.fetch_page(page_0indexed)
|
||||||
|
.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,
|
current_page: page,
|
||||||
total_page,
|
total_page,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -46,11 +46,10 @@ 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 = if let Some(page) = pagination.page {
|
let page: u64 = pagination
|
||||||
page.try_into().unwrap()
|
.page
|
||||||
} else {
|
.map(|p| p.max(1) as u64) // Minimum 1
|
||||||
1
|
.unwrap_or(1);
|
||||||
};
|
|
||||||
|
|
||||||
let users = UserOperator::new(state.clone())
|
let users = UserOperator::new(state.clone())
|
||||||
.list()
|
.list()
|
||||||
|
|||||||
@ -41,24 +41,26 @@
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<nav aria-label="Page navigation">
|
{% if total_page > 1 %}
|
||||||
<ul class="pagination">
|
<nav aria-label="Page navigation">
|
||||||
<li class="page-item {% if current_page == 1 %}disabled{% endif %}">
|
<ul class="pagination">
|
||||||
<a class="page-link" href="/?page={{ current_page - 1 }}">Prev</a>
|
<li class="page-item {% if current_page <= 1 %}disabled{% endif %}">
|
||||||
</li>
|
<a class="page-link" href="/?page={% if current_page > 1 %}{{ current_page - 1 }}{% else %}1{% endif %}">Prev</a>
|
||||||
|
</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 %}
|
||||||
|
|
||||||
{% for page in (current_page - 1)..(current_page + 1) %}
|
<li class="page-item {% if current_page == total_page %}disabled{% endif %}">
|
||||||
{% if page >= 1 && page <= total_page %}
|
<a class="page-link" href="/?page={{ current_page + 1 }}">Next</a>
|
||||||
<li class="page-item {% if page == current_page %}active{% endif %}">
|
</li>
|
||||||
<a class="page-link" href="/?page={{ page }}">{{ page }}</a>
|
</ul>
|
||||||
</li>
|
</nav>
|
||||||
{% 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 %}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user