From 8de57fec67898ceeebd7620b1b5ba6bd423fb2fd Mon Sep 17 00:00:00 2001 From: "Jakob L. Kreuze" Date: Tue, 19 May 2020 21:37:02 -0400 Subject: Factor out `tag_id`. --- src/main.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 91458e0..0809159 100644 --- a/src/main.rs +++ b/src/main.rs @@ -124,19 +124,25 @@ impl TagBase { Ok(()) } - /// Associate `tag` with the image specified by `image_id`. - fn tag_image(&self, image_id: i64, tag: &str) -> Result<()> { - let tag_id: i64 = self + /// Return the internal id for `tag`. + fn tag_id(&self, tag: &str) -> Result { + Ok(self .conn .prepare("SELECT * FROM tags WHERE name = ?")? - .query_row(params![tag], |row| Ok(row.get(0)))??; + .query_row(params![tag], |row| Ok(row.get::<_, String>(0)))?? + .parse::()?) + } + + /// Associate `tag` with the image specified by `image_id`. + fn tag_image(&self, image_id: i64, tag: &str) -> Result<()> { self.conn.execute( "INSERT INTO mapping (image, tag) VALUES(?, ?)", - params![image_id, tag_id], + params![image_id, self.tag_id(tag)?], )?; Ok(()) } + /// Return the tags for the image specified by `image_id`. fn tags_for_image(&self, image_id: i64) -> Result> { Ok(self .conn -- cgit v1.3