add pagination

pull/1/head
kirbylife 2019-11-16 23:10:06 -06:00
parent 482b70e292
commit ad4cd6b028
3 changed files with 40 additions and 8 deletions

View File

@ -24,6 +24,7 @@ use std::vec::Vec;
use tera::Context; use tera::Context;
const TITLES: [&str; 4] = ["/* {} */", "# {}", "// {}", "<!-- {} -->"]; const TITLES: [&str; 4] = ["/* {} */", "# {}", "// {}", "<!-- {} -->"];
const MAX_POSTS_PER_PAGE: i64 = 20;
fn establish_connection() -> PgConnection { fn establish_connection() -> PgConnection {
dotenv().ok(); dotenv().ok();
@ -32,21 +33,29 @@ fn establish_connection() -> PgConnection {
PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url)) PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url))
} }
pub fn get_posts() -> Vec<Post> { fn get_posts(page: i64) -> (Vec<Post>, i64) {
use schema::posts::dsl::*; use schema::posts::dsl::*;
let connection = establish_connection(); let connection = establish_connection();
posts let visible_posts = posts
.filter(published.eq(true)) .filter(published.eq(true))
.limit(20) .limit(MAX_POSTS_PER_PAGE)
.offset(MAX_POSTS_PER_PAGE * (page - 1))
.load::<Post>(&connection) .load::<Post>(&connection)
.expect("Error loading posts") .expect("Error loading posts");
let number_of_posts: i64 = posts
.filter(published.eq(true))
.count()
.get_result(&connection)
.expect("Error counting the posts");
(visible_posts, number_of_posts)
} }
#[get("/?<page>")] #[get("/?<page>")]
fn index(page: Option<i8>) -> Template { fn index(page: Option<i8>) -> Template {
let page = page.unwrap_or_else(|| 0); let page = page.unwrap_or_else(|| 1);
println!("{}", page);
let mut context = Context::new(); let mut context = Context::new();
@ -54,8 +63,14 @@ fn index(page: Option<i8>) -> Template {
let title_fmt = TITLES[rng.gen_range(0, TITLES.len())]; let title_fmt = TITLES[rng.gen_range(0, TITLES.len())];
let title = str::replace(title_fmt, "{}", "CódigoComentado"); let title = str::replace(title_fmt, "{}", "CódigoComentado");
let (posts, n_posts) = get_posts(page as i64);
let total_pages = (n_posts as f64 / MAX_POSTS_PER_PAGE as f64).ceil() as i64;
context.insert("title", &title); context.insert("title", &title);
context.insert("posts", &get_posts()); context.insert("posts", &posts);
context.insert("total_pages", &total_pages);
context.insert("actual_page", &page);
Template::render("index", context) Template::render("index", context)
} }

View File

@ -45,7 +45,14 @@ article figure img {
grid-area: info; grid-area: info;
} }
article strong { strong {
font-family: monospace; font-family: monospace;
font-size: 10px; font-size: 10px;
} }
div.pagination {
margin-top: 50px;
width: auto;
height: 3em;
text-align: center;
}

View File

@ -20,4 +20,14 @@
</article> </article>
</a> </a>
{% endfor %} {% endfor %}
{# Pages #}
<div class="pagination">
<strong>Pages:</strong>
[
{% for n in range(start=1, end=total_pages+1) %}
<a {% if actual_page != n %} href="?page={{ n }}"{% endif %}>página {{ n }}</a>{% if total_pages != n %}, {% endif %}
{% endfor %}
]
</div>
{% endblock content %} {% endblock content %}