diff options
| author | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2020-05-28 18:12:46 -0400 |
|---|---|---|
| committer | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2020-05-28 18:12:46 -0400 |
| commit | 4dbb8998f840a771949ed30f9f2b652713edd0f9 (patch) | |
| tree | 938d79fef0b4c6ca8ccde0bd46a0a4f727e9d032 /src/main.rs | |
| parent | 3417d6323598bca299a3d4c6dea21e013e6681be (diff) | |
Implement initial pagination.
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/src/main.rs b/src/main.rs index fab288c..e2290b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,13 +61,12 @@ impl DatabaseConnection { } #[get("/")] -fn index() -> Template { - let context = HashMap::<&str, &str>::new(); - Template::render("index", &context) +fn index(conn: SiteState) -> Template { + display_posts(conn, String::from(""), None) } -#[get("/posts?<tags>")] -fn display_posts(conn: SiteState, tags: String) -> Template { +#[get("/posts?<tags>&<last>")] +fn display_posts(conn: SiteState, tags: String, last: Option<i64>) -> Template { #[derive(Serialize)] struct TagResult { name: String, @@ -75,11 +74,13 @@ fn display_posts(conn: SiteState, tags: String) -> Template { } #[derive(Serialize)] - struct Asdf { + struct Context { tags: Vec<TagResult>, images: Vec<ImageResult>, + next_page: String, } + let tagstr = &tags.clone(); let tag_results = { let tb = &conn.inner().lock().unwrap().tb; tb.top_tags(20) @@ -91,9 +92,15 @@ fn display_posts(conn: SiteState, tags: String) -> Template { }) .collect() }; - let images = get_posts(conn, tags).into_inner(); - let context = Asdf { + let images = get_posts(conn, tags, last).into_inner(); + let context = Context { tags: tag_results, + // FIXME: Magic number for results per request. + next_page: if images.len() == 50 { + format!("/posts?tags={}&last={}", tagstr, images[49].id) + } else { + String::from("") + }, images, }; Template::render("index", context) @@ -101,20 +108,25 @@ fn display_posts(conn: SiteState, tags: String) -> Template { #[derive(Serialize)] struct ImageResult { + id: i64, filename: String, thumb_filename: String, tags: Vec<String>, } -#[get("/posts?<tags>")] -fn get_posts(conn: SiteState, tags: String) -> Json<Vec<ImageResult>> { +#[get("/posts?<tags>&<last>")] +fn get_posts(conn: SiteState, tags: String, last: Option<i64>) -> Json<Vec<ImageResult>> { let tb = &conn.inner().lock().unwrap().tb; - let tags = tags.split(",").collect::<Vec<&str>>(); + let tags = if tags != "" { + tags.split(",").collect::<Vec<&str>>() + } else { + vec![] + }; let results = tb .query(Query { tags: &tags[..], - last_id: Some(0), + last_id: last, limit: Some(50), }) .unwrap(); @@ -123,6 +135,7 @@ fn get_posts(conn: SiteState, tags: String) -> Json<Vec<ImageResult>> { results .iter() .map(|image| ImageResult { + id: image.id, filename: image.filename.clone(), thumb_filename: thumb_filename(&image.filename), tags: tb.tags_for_image(image.id).unwrap(), |