diff options
Diffstat (limited to 'src/db/mod.rs')
-rw-r--r-- | src/db/mod.rs | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/src/db/mod.rs b/src/db/mod.rs index 0dd4ddf..2cac0d2 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -13,18 +13,28 @@ pub fn init() -> Result<Connection> { } pub fn insert(conn: &Connection, event: &Event) -> Result<()> { + let repetition = match &event.repetition { + Some(r) => Some(serde_json::to_string(&r)?), + None => None, + }; + conn.execute( - "INSERT INTO events (id, date, start, end, name, created, updated) VALUES (?, ?, ?, ?, ?, datetime(), datetime())", - params![event.id.to_hyphenated().to_string(), event.date, event.start, event.end, event.name] + "INSERT INTO events (id, date, start, end, name, repetition, created, updated) VALUES (?, ?, ?, ?, ?, ?, datetime(), datetime())", + params![event.id.to_hyphenated().to_string(), event.date, event.start, event.end, event.name, repetition] )?; Ok(()) } pub fn update(conn: &Connection, event: &Event) -> Result<()> { + let repetition = match &event.repetition { + Some(r) => Some(serde_json::to_string(&r)?), + None => None, + }; + conn.execute( - "UPDATE events SET date = ?, start = ?, end = ?, name = ?, updated = datetime() where id = ?", - params![event.date, event.start, event.end, event.name, event.id.to_hyphenated().to_string()] + "UPDATE events SET date = ?, start = ?, end = ?, name = ?, repetition = ?, updated = datetime() where id = ?", + params![event.date, event.start, event.end, event.name, repetition, event.id.to_hyphenated().to_string()] )?; Ok(()) @@ -41,16 +51,18 @@ pub fn delete(conn: &Connection, id: &Uuid) -> Result<()> { // TODO: Don’t use unwrap pub fn list(conn: &Connection) -> Result<Vec<Event>> { - let mut stmt = conn.prepare("SELECT id, date, start, end, name FROM events")?; + let mut stmt = conn.prepare("SELECT id, date, start, end, name, repeated FROM events")?; let iter = stmt.query_map([], |row| { let uuid: String = row.get(0)?; + let repetition: Option<String> = row.get(5)?; Ok(Event { id: Uuid::parse_str(&uuid).unwrap(), date: row.get(1)?, start: row.get(2)?, end: row.get(3)?, name: row.get(4)?, + repetition: repetition.and_then(|r: String| serde_json::from_str(&r).ok()), }) })?; |