diff options
Diffstat (limited to 'src/db/categories.rs')
-rw-r--r-- | src/db/categories.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/db/categories.rs b/src/db/categories.rs new file mode 100644 index 0000000..ebefb6d --- /dev/null +++ b/src/db/categories.rs @@ -0,0 +1,25 @@ +use anyhow::Result; +use rusqlite::Connection; +use uuid::Uuid; + +use crate::model::category::Category; + +pub fn list(conn: &Connection) -> Result<Vec<Category>> { + let mut stmt = conn.prepare("SELECT id, name, color FROM categories")?; + + let iter = stmt.query_map([], |row| { + Ok(read_category(row.get(0)?, row.get(1)?, row.get(2)?)) + })?; + + let mut res = vec![]; + for category in iter { + res.push(category??) + } + Ok(res) +} + +fn read_category(id: String, name: String, color: String) -> Result<Category> { + let id = Uuid::parse_str(&id)?; + + Ok(Category { id, name, color }) +} |