aboutsummaryrefslogtreecommitdiff
path: root/src/db/categories.rs
blob: ebefb6d965f75ff5ec21ce9e44dfe0aac1484201 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 })
}