Merge pull request #144 from SeaQL/ss/rocket-pagination

Add pagination to the rocket example
This commit is contained in:
Chris Tsang 2021-09-11 15:51:43 +08:00 committed by GitHub
commit 56c8f2ea69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 8 deletions

View File

@ -11,7 +11,6 @@ use rocket_db_pools::{sqlx, Connection, Database};
use rocket_dyn_templates::{context, Template};
use sea_orm::entity::*;
use sea_orm::query::*;
mod pool;
use pool::RocketDbPool;
@ -27,6 +26,8 @@ type Result<T, E = rocket::response::Debug<sqlx::Error>> = std::result::Result<T
mod post;
pub use post::Entity as Post;
const DEFAULT_POSTS_PER_PAGE: usize = 5;
#[get("/new")]
fn new() -> Template {
Template::render("new", &Context::default())
@ -71,11 +72,20 @@ async fn update(conn: Connection<Db>, id: i32, post_form: Form<post::Model>) ->
Flash::success(Redirect::to("/"), "Post successfully edited.")
}
#[get("/")]
async fn list(conn: Connection<Db>, flash: Option<FlashMessage<'_>>) -> Template {
let posts = Post::find()
.order_by_asc(post::Column::Id)
.all(&conn)
#[get("/?<page>&<posts_per_page>")]
async fn list(
conn: Connection<Db>,
posts_per_page: Option<usize>,
page: Option<usize>,
flash: Option<FlashMessage<'_>>,
) -> Template {
let page = page.unwrap_or(0);
let posts_per_page = posts_per_page.unwrap_or(DEFAULT_POSTS_PER_PAGE);
let paginator = Post::find().paginate(&conn, posts_per_page);
let num_pages = paginator.num_pages().await.ok().unwrap();
let posts = paginator
.fetch_page(page)
.await
.expect("could not retrieve posts");
@ -86,6 +96,9 @@ async fn list(conn: Connection<Db>, flash: Option<FlashMessage<'_>>) -> Template
context! {
posts: posts,
flash: flash,
page: page,
posts_per_page: posts_per_page,
num_pages: num_pages,
},
)
}
@ -138,7 +151,7 @@ pub fn not_found(req: &Request<'_>) -> Template {
async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
let db_url = Db::fetch(&rocket).unwrap().db_url.clone();
let conn = sea_orm::Database::connect(&db_url).await.unwrap();
setup::create_post_table(&conn).await;
let _ = setup::create_post_table(&conn).await;
Ok(rocket)
}

View File

@ -22,6 +22,19 @@
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<td></td>
<td>
{% if page == 0 %} Previous {% else %}
<a href="/?page={{ page - 1 }}&posts_per_page={{ posts_per_page }}">Previous</a>
{% endif %} | {% if page == num_pages - 1 %} Next {% else %}
<a href="/?page={{ page + 1 }}&posts_per_page={{ posts_per_page }}">Next</a>
{% endif %}
</td>
<td></td>
</tr>
</tfoot>
</table>
<div class="twelve columns">

View File

@ -3,7 +3,9 @@ use crate::{
PrimaryKeyToColumn, RelationDef,
};
pub use sea_query::{Condition, ConditionalStatement, DynIden, JoinType, Order, OrderedStatement};
use sea_query::{Expr, IntoCondition, LockType, SeaRc, SelectExpr, SelectStatement, SimpleExpr, TableRef};
use sea_query::{
Expr, IntoCondition, LockType, SeaRc, SelectExpr, SelectStatement, SimpleExpr, TableRef,
};
// LINT: when the column does not appear in tables selected from
// LINT: when there is a group by clause, but some columns don't have aggregate functions