From 2d80413609130f1c121dcae39a150a27dd9f02ea Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 26 Feb 2022 18:35:24 +0100 Subject: Show repeated events --- src/db/migrations/1-init.sql | 16 ++++++++-------- src/db/mod.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 10 deletions(-) (limited to 'src/db') diff --git a/src/db/migrations/1-init.sql b/src/db/migrations/1-init.sql index a7db8b8..7e49764 100644 --- a/src/db/migrations/1-init.sql +++ b/src/db/migrations/1-init.sql @@ -1,10 +1,10 @@ CREATE TABLE IF NOT EXISTS "events" ( - "id" VARCHAR PRIMARY KEY, - "date" VARCHAR NOT NULL, - "start" VARCHAR NULL, - "end" VARCHAR NULL, - "name" VARCHAR NOT NULL, - "repetition" VARCHAR NULL, - "created" TIMESTAMP NOT NULL, - "updated" TIMESTAMP NOT NULL + "id" TEXT PRIMARY KEY, /* UUID */ + "date" TEXT NOT NULL, /* DATE */ + "start" TEXT NULL, /* TIME */ + "end" TEXT NULL, /* TIME */ + "name" TEXT NOT NULL, + "repetition" TEXT NULL, /* JSON */ + "created" TEXT NOT NULL, /* DATETIME */ + "updated" TEXT NOT NULL /* DATETIME */ ); diff --git a/src/db/mod.rs b/src/db/mod.rs index 2cac0d2..b27226b 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use chrono::NaiveDate; use rusqlite::{params, Connection}; use rusqlite_migration::{Migrations, M}; use uuid::Uuid; @@ -50,8 +51,13 @@ pub fn delete(conn: &Connection, id: &Uuid) -> Result<()> { } // TODO: Don’t use unwrap -pub fn list(conn: &Connection) -> Result> { - let mut stmt = conn.prepare("SELECT id, date, start, end, name, repeated FROM events")?; +pub fn list_repeated(conn: &Connection) -> Result> { + let mut stmt = conn.prepare( + " + SELECT id, date, start, end, name, repetition + FROM events + WHERE repetition IS NOT NULL", + )?; let iter = stmt.query_map([], |row| { let uuid: String = row.get(0)?; @@ -68,3 +74,35 @@ pub fn list(conn: &Connection) -> Result> { Ok(iter.map(|r| r.unwrap()).collect()) } + +// TODO: Don’t use unwrap +pub fn list_non_repeated_between( + conn: &Connection, + start: NaiveDate, + end: NaiveDate, +) -> Result> { + let mut stmt = conn.prepare( + " + SELECT id, date, start, end, name + FROM events + WHERE + repetition IS NULL + AND date >= ? + AND date <= ? + ", + )?; + + let iter = stmt.query_map([start, end], |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)?, + repetition: None, + }) + })?; + + Ok(iter.map(|r| r.unwrap()).collect()) +} -- cgit v1.2.3