use anyhow::Result; use rusqlite::{params, Connection}; use rusqlite_migration::{Migrations, M}; use uuid::Uuid; use crate::model::event::Event; pub fn init() -> Result { let mut conn = Connection::open("database.db")?; let migrations = Migrations::new(vec![M::up(include_str!("migrations/1-init.sql"))]); migrations.to_latest(&mut conn)?; Ok(conn) } pub fn insert(conn: &Connection, event: &Event) -> Result<()> { 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] )?; Ok(()) } pub fn update(conn: &Connection, event: &Event) -> Result<()> { 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()] )?; Ok(()) } pub fn delete(conn: &Connection, id: &Uuid) -> Result<()> { conn.execute( "DELETE FROM events WHERE id = ?", params![id.to_hyphenated().to_string()], )?; Ok(()) } // TODO: Don’t use unwrap pub fn list(conn: &Connection) -> Result> { 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 { id: Uuid::parse_str(&uuid).unwrap(), date: row.get(1)?, start: row.get(2)?, end: row.get(3)?, name: row.get(4)?, }) })?; Ok(iter.map(|r| r.unwrap()).collect()) }