diff options
author | Joris | 2022-02-20 09:33:55 +0100 |
---|---|---|
committer | Joris | 2022-02-20 09:33:55 +0100 |
commit | 1445e23a26c6581ad0c3f5b5016e47e95d224e9f (patch) | |
tree | 18bd4288fbcf52279a69de50be5bad6cc7db3c75 /src/db | |
parent | 6c47403b11e7aaf1a22778bdc7615051779cb7bd (diff) |
Save repetition in events
But don’t show repetead events for now.
Diffstat (limited to 'src/db')
-rw-r--r-- | src/db/migrations/1-init.sql | 1 | ||||
-rw-r--r-- | src/db/mod.rs | 22 |
2 files changed, 18 insertions, 5 deletions
diff --git a/src/db/migrations/1-init.sql b/src/db/migrations/1-init.sql index 39b845b..a7db8b8 100644 --- a/src/db/migrations/1-init.sql +++ b/src/db/migrations/1-init.sql @@ -4,6 +4,7 @@ CREATE TABLE IF NOT EXISTS "events" ( "start" VARCHAR NULL, "end" VARCHAR NULL, "name" VARCHAR NOT NULL, + "repetition" VARCHAR NULL, "created" TIMESTAMP NOT NULL, "updated" TIMESTAMP NOT NULL ); 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()), }) })?; |