Add pagination to the blog page

No UI to control it yet
This commit is contained in:
Bat
2018-07-20 18:42:35 +02:00
parent 3764e295b8
commit 67eb41add1
5 changed files with 62 additions and 8 deletions
+40 -2
View File
@@ -1,5 +1,11 @@
use rocket::response::NamedFile;
use std::path::{Path, PathBuf};
use rocket::{
http::uri::{FromUriParam, UriDisplay},
response::NamedFile
};
use std::{
fmt,
path::{Path, PathBuf}
};
macro_rules! may_fail {
($account:expr, $expr:expr, $template:expr, $msg:expr, | $res:ident | $block:block) => {
@@ -28,6 +34,38 @@ macro_rules! may_fail {
};
}
const ITEMS_PER_PAGE: i32 = 10;
#[derive(FromForm)]
pub struct Page {
page: i32
}
impl UriDisplay for Page {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "page={}", &self.page as &UriDisplay)
}
}
impl FromUriParam<i32> for Page {
type Target = Page;
fn from_uri_param(num: i32) -> Page {
Page { page: num }
}
}
impl Page {
pub fn first() -> Page {
Page {
page: 1
}
}
pub fn limits(&self) -> (i32, i32) {
((self.page - 1) * ITEMS_PER_PAGE, self.page * ITEMS_PER_PAGE)
}
}
pub mod blogs;
pub mod comments;
pub mod errors;