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 | |
| parent | 3417d6323598bca299a3d4c6dea21e013e6681be (diff) | |
Implement initial pagination.
| -rw-r--r-- | src/database.rs | 43 | ||||
| -rw-r--r-- | src/main.rs | 37 | ||||
| -rw-r--r-- | templates/index.hbs | 3 |
3 files changed, 48 insertions, 35 deletions
diff --git a/src/database.rs b/src/database.rs index bd195f2..61a51cb 100644 --- a/src/database.rs +++ b/src/database.rs @@ -198,29 +198,26 @@ impl TagDatabase { ) }; - let query = if let (Some(id), Some(n)) = (q.last_id, q.limit) { - format!( - "SELECT id, blake2, filename, orig_dir - FROM images - {union} - WHERE id > {last_id} - GROUP BY id - ORDER BY id DESC - LIMIT {limit}", - last_id = id, - union = union.or(Some("".into())).unwrap(), - limit = n - ) - } else { - format!( - "SELECT id, blake2, filename, orig_dir - FROM images - {union} - GROUP BY id - ORDER BY id DESC", - union = union.or(Some("".into())).unwrap(), - ) - }; + let query = format!( + "SELECT id, blake2, filename, orig_dir + FROM images + {union} + {since} + GROUP BY id + ORDER BY id DESC + {limit}", + since = if let Some(n) = q.last_id { + format!("WHERE id < {}", n) + } else { + String::from("") + }, + limit = if let Some(n) = q.limit { + format!("LIMIT {}", n) + } else { + String::from("") + }, + union = union.or(Some("".into())).unwrap(), + ); Ok(self .0 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(), diff --git a/templates/index.hbs b/templates/index.hbs index f0d6d91..18245a0 100644 --- a/templates/index.hbs +++ b/templates/index.hbs @@ -13,6 +13,9 @@ {{#each images}} <img src="/image/{{thumb_filename}}"> {{/each}} + {{#if next_page}} + <p><a href="{{next_page}}">next</a></p> + {{/if}} </main> <aside id="tag-view"> <section id="search"> |