Paginate the homepage

This commit is contained in:
Bat 2018-07-25 15:20:09 +02:00
parent 740393bc18
commit 18125ab398
9 changed files with 50 additions and 5 deletions

View File

@ -65,6 +65,10 @@ impl Post {
.len()
}
pub fn count(conn: &PgConnection) -> i64 {
posts::table.count().get_result(conn).expect("Couldn't count posts")
}
pub fn get_recents(conn: &PgConnection, limit: i64) -> Vec<Post> {
posts::table.order(posts::creation_date.desc())
.limit(limit)
@ -106,6 +110,14 @@ impl Post {
.expect("Error loading a page of posts for blog")
}
pub fn get_recents_page(conn: &PgConnection, (min, max): (i32, i32)) -> Vec<Post> {
posts::table.order(posts::creation_date.desc())
.offset(min.into())
.limit((max - min).into())
.load::<Post>(conn)
.expect("Error loading recent posts page")
}
pub fn get_authors(&self, conn: &PgConnection) -> Vec<User> {
use schema::users;
use schema::post_authors;

View File

@ -345,3 +345,9 @@ msgid "One article in this blog"
msgid_plural "{{ count }} articles in this blog"
msgstr[0] "Du bist kein Autor in diesem Blog."
msgstr[1] "Du bist kein Autor in diesem Blog."
msgid "Previous page"
msgstr ""
msgid "Next page"
msgstr ""

View File

@ -337,3 +337,9 @@ msgid "One article in this blog"
msgid_plural "{{ count }} articles in this blog"
msgstr[0] ""
msgstr[1] ""
msgid "Previous page"
msgstr ""
msgid "Next page"
msgstr ""

View File

@ -341,3 +341,9 @@ msgid "One article in this blog"
msgid_plural "{{ count }} articles in this blog"
msgstr[0] "{{ count }} aut⋅eur⋅rice dans ce blog : "
msgstr[1] "{{ count }} aut⋅eur⋅rice⋅s dans ce blog : "
msgid "Previous page"
msgstr ""
msgid "Next page"
msgstr ""

View File

@ -350,5 +350,11 @@ msgstr[0] "Ten blog ma jednego autora: "
msgstr[1] "Ten blog ma {{ count }} autorów: "
msgstr[2] "Ten blog ma {{ count }} autorów: "
msgid "Previous page"
msgstr ""
msgid "Next page"
msgstr ""
#~ msgid "Logowanie"
#~ msgstr "Zaloguj się"

View File

@ -46,6 +46,7 @@ fn main() {
routes::comments::create,
routes::instance::paginated_index,
routes::instance::index,
routes::instance::shared_inbox,
routes::instance::nodeinfo,

View File

@ -10,17 +10,20 @@ use plume_models::{
instance::*
};
use inbox::Inbox;
use routes::Page;
#[get("/")]
fn index(conn: DbConn, user: Option<User>) -> Template {
#[get("/?<page>")]
fn paginated_index(conn: DbConn, user: Option<User>, page: Page) -> Template {
match Instance::get_local(&*conn) {
Some(inst) => {
let recents = Post::get_recents(&*conn, 6);
let recents = Post::get_recents_page(&*conn, page.limits());
Template::render("instance/index", json!({
"instance": inst,
"account": user,
"recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::<Vec<serde_json::Value>>()
"recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
"page": page.page,
"n_pages": Page::total(Post::count(&*conn) as i32)
}))
}
None => {
@ -31,6 +34,11 @@ fn index(conn: DbConn, user: Option<User>) -> Template {
}
}
#[get("/")]
fn index(conn: DbConn, user: Option<User>) -> Template {
paginated_index(conn, user, Page::first())
}
#[post("/inbox", data = "<data>")]
fn shared_inbox(conn: DbConn, data: String) -> String {
let act: serde_json::Value = serde_json::from_str(&data[..]).unwrap();

View File

@ -544,7 +544,6 @@ form.new-post input[type="submit"]:hover { background: #DADADA; }
/*== Pagination ==*/
.pagination {
display: flex;
width: 100%;
justify-content: space-evenly;
}

View File

@ -14,4 +14,5 @@
{{ macros::post_card(article=article) }}
{% endfor %}
</div>
{{ macros::paginate(page=page, total=n_pages) }}
{% endblock content %}