diff options
author | Joris | 2022-04-24 16:31:49 +0200 |
---|---|---|
committer | Joris | 2022-04-24 16:31:49 +0200 |
commit | 47fe90ee23d8ab04645ef3c7a17459ed40c5b765 (patch) | |
tree | d1aa7d3840a3bd825dcae0a0398fec7ca310f45c /src/db/categories.rs | |
parent | 6d271b09303d924381cc65e7c0b5eb56833780ed (diff) |
Allow to attach categories to events
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 }) +} |