Add pagination to the blog page
No UI to control it yet
This commit is contained in:
+40
-2
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user