diff --git a/examples/rocket_example/src/main.rs b/examples/rocket_example/src/main.rs index 07ec3939..1a275200 100644 --- a/examples/rocket_example/src/main.rs +++ b/examples/rocket_example/src/main.rs @@ -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> = std::result::Result Template { Template::render("new", &Context::default()) @@ -71,11 +72,20 @@ async fn update(conn: Connection, id: i32, post_form: Form) -> Flash::success(Redirect::to("/"), "Post successfully edited.") } -#[get("/")] -async fn list(conn: Connection, flash: Option>) -> Template { - let posts = Post::find() - .order_by_asc(post::Column::Id) - .all(&conn) +#[get("/?&")] +async fn list( + conn: Connection, + posts_per_page: Option, + page: Option, + flash: Option>, +) -> 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, flash: Option>) -> 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) -> 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) } diff --git a/examples/rocket_example/templates/index.html.tera b/examples/rocket_example/templates/index.html.tera index 621f18b8..0cba1b7d 100644 --- a/examples/rocket_example/templates/index.html.tera +++ b/examples/rocket_example/templates/index.html.tera @@ -22,6 +22,19 @@ {% endfor %} + + + + + {% if page == 0 %} Previous {% else %} + Previous + {% endif %} | {% if page == num_pages - 1 %} Next {% else %} + Next + {% endif %} + + + +
diff --git a/src/query/helper.rs b/src/query/helper.rs index 7aeb9f0b..d93595f1 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -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