diff options
| author | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2020-05-25 18:32:15 -0400 |
|---|---|---|
| committer | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2020-05-25 18:32:15 -0400 |
| commit | 899f517859fe2d2981ec5214892c1af966358c6a (patch) | |
| tree | 2bc5e8e07cd3afc349f4310106ea779a89a233e1 /src/database.rs | |
| parent | 30a4fc7365ffa4842275bc9513e3e458308b596d (diff) | |
Implement posts endpoint.
Diffstat (limited to 'src/database.rs')
| -rw-r--r-- | src/database.rs | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/src/database.rs b/src/database.rs index d27646b..7b69a21 100644 --- a/src/database.rs +++ b/src/database.rs @@ -26,9 +26,9 @@ pub struct TagDatabase(Connection); /// Tag query with a sense of "state" for pagination. #[derive(Debug)] pub struct Query<'a> { - tags: &'a [&'a str], - last_id: Option<i64>, - limit: Option<i64>, + pub tags: &'a [&'a str], + pub last_id: Option<i64>, + pub limit: Option<i64>, } impl TagDatabase { @@ -144,6 +144,22 @@ impl TagDatabase { Ok(()) } + /// Return the tags for the image specified by `image_id`. + pub fn tags_for_image(&self, image_id: i64) -> Result<Vec<String>> { + Ok(self + .0 + .prepare( + "SELECT tags.name, COUNT(mapping.tag) as tag_count + FROM mapping + INNER JOIN images ON images.id = ? + INNER JOIN tags + GROUP BY tags.name;", + )? + .query_map(params![image_id], |row| row.get(0))? + .filter_map(|tag| tag.ok()) + .collect()) + } + /// Return the intersection of the sets of images matching each of `tags`. pub fn query(&self, q: Query) -> Result<Vec<Image>> { let ids: Vec<i64> = q @@ -257,22 +273,6 @@ mod tests { tb.initialize_tables()?; Ok(tb) } - - /// Return the tags for the image specified by `image_id`. - pub fn tags_for_image(&self, image_id: i64) -> Result<Vec<String>> { - Ok(self - .0 - .prepare( - "SELECT tags.name, COUNT(mapping.tag) as tag_count - FROM mapping - INNER JOIN images ON images.id = ? - INNER JOIN tags - GROUP BY tags.name;", - )? - .query_map(params![image_id], |row| row.get(0))? - .filter_map(|tag| tag.ok()) - .collect()) - } } #[test] |