Merge pull request #144 from SeaQL/ss/rocket-pagination
Add pagination to the rocket example
This commit is contained in:
commit
56c8f2ea69
@ -11,7 +11,6 @@ use rocket_db_pools::{sqlx, Connection, Database};
|
|||||||
use rocket_dyn_templates::{context, Template};
|
use rocket_dyn_templates::{context, Template};
|
||||||
|
|
||||||
use sea_orm::entity::*;
|
use sea_orm::entity::*;
|
||||||
use sea_orm::query::*;
|
|
||||||
|
|
||||||
mod pool;
|
mod pool;
|
||||||
use pool::RocketDbPool;
|
use pool::RocketDbPool;
|
||||||
@ -27,6 +26,8 @@ type Result<T, E = rocket::response::Debug<sqlx::Error>> = std::result::Result<T
|
|||||||
mod post;
|
mod post;
|
||||||
pub use post::Entity as Post;
|
pub use post::Entity as Post;
|
||||||
|
|
||||||
|
const DEFAULT_POSTS_PER_PAGE: usize = 5;
|
||||||
|
|
||||||
#[get("/new")]
|
#[get("/new")]
|
||||||
fn new() -> Template {
|
fn new() -> Template {
|
||||||
Template::render("new", &Context::default())
|
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.")
|
Flash::success(Redirect::to("/"), "Post successfully edited.")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/?<page>&<posts_per_page>")]
|
||||||
async fn list(conn: Connection<Db>, flash: Option<FlashMessage<'_>>) -> Template {
|
async fn list(
|
||||||
let posts = Post::find()
|
conn: Connection<Db>,
|
||||||
.order_by_asc(post::Column::Id)
|
posts_per_page: Option<usize>,
|
||||||
.all(&conn)
|
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
|
.await
|
||||||
.expect("could not retrieve posts");
|
.expect("could not retrieve posts");
|
||||||
|
|
||||||
@ -86,6 +96,9 @@ async fn list(conn: Connection<Db>, flash: Option<FlashMessage<'_>>) -> Template
|
|||||||
context! {
|
context! {
|
||||||
posts: posts,
|
posts: posts,
|
||||||
flash: flash,
|
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 {
|
async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
|
||||||
let db_url = Db::fetch(&rocket).unwrap().db_url.clone();
|
let db_url = Db::fetch(&rocket).unwrap().db_url.clone();
|
||||||
let conn = sea_orm::Database::connect(&db_url).await.unwrap();
|
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)
|
Ok(rocket)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,19 @@
|
|||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</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>
|
</table>
|
||||||
|
|
||||||
<div class="twelve columns">
|
<div class="twelve columns">
|
||||||
|
@ -3,7 +3,9 @@ use crate::{
|
|||||||
PrimaryKeyToColumn, RelationDef,
|
PrimaryKeyToColumn, RelationDef,
|
||||||
};
|
};
|
||||||
pub use sea_query::{Condition, ConditionalStatement, DynIden, JoinType, Order, OrderedStatement};
|
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 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
|
// LINT: when there is a group by clause, but some columns don't have aggregate functions
|
||||||
|
Loading…
x
Reference in New Issue
Block a user