diff options
author | Joris | 2022-01-09 13:39:10 +0100 |
---|---|---|
committer | Joris | 2022-01-09 13:39:10 +0100 |
commit | 5166efe517291f5c9fc6326f30651ef799d6db65 (patch) | |
tree | 36df3d65c9e7218bff2264391032ecd28001a688 /src/db | |
parent | 99af88a840bef534540a4b273d24a8a17e7fc9b9 (diff) |
Add id in event model
Diffstat (limited to 'src/db')
-rw-r--r-- | src/db/migrations/1-init.sql | 2 | ||||
-rw-r--r-- | src/db/mod.rs | 17 |
2 files changed, 11 insertions, 8 deletions
diff --git a/src/db/migrations/1-init.sql b/src/db/migrations/1-init.sql index 72fab80..39b845b 100644 --- a/src/db/migrations/1-init.sql +++ b/src/db/migrations/1-init.sql @@ -1,5 +1,5 @@ CREATE TABLE IF NOT EXISTS "events" ( - "id" INTEGER PRIMARY KEY, + "id" VARCHAR PRIMARY KEY, "date" VARCHAR NOT NULL, "start" VARCHAR NULL, "end" VARCHAR NULL, diff --git a/src/db/mod.rs b/src/db/mod.rs index 6cfa2eb..23cec7e 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,6 +1,7 @@ use anyhow::Result; use rusqlite::{params, Connection}; use rusqlite_migration::{Migrations, M}; +use uuid::Uuid; use crate::model::event::Event; @@ -13,22 +14,24 @@ pub fn init() -> Result<Connection> { pub fn insert(conn: &Connection, event: &Event) -> Result<()> { conn.execute( - "INSERT INTO events (date, start, end, name, created, updated) VALUES (?, ?, ?, ?, datetime(), datetime())", - params![event.date, event.start, event.end, event.name] + "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] )?; Ok(()) } pub fn list(conn: &Connection) -> Result<Vec<Event>> { - let mut stmt = conn.prepare("SELECT date, start, end, name FROM events")?; + let mut stmt = conn.prepare("SELECT id, date, start, end, name FROM events")?; let iter = stmt.query_map([], |row| { + let uuid: String = row.get(0)?; Ok(Event { - date: row.get(0)?, - start: row.get(1)?, - end: row.get(2)?, - name: row.get(3)?, + id: Uuid::parse_str(&uuid).unwrap(), + date: row.get(1)?, + start: row.get(2)?, + end: row.get(3)?, + name: row.get(4)?, }) })?; |