From 5166efe517291f5c9fc6326f30651ef799d6db65 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 9 Jan 2022 13:39:10 +0100 Subject: Add id in event model --- src/db/migrations/1-init.sql | 2 +- src/db/mod.rs | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) (limited to 'src/db') 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 { 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> { - 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)?, }) })?; -- cgit v1.2.3