aboutsummaryrefslogtreecommitdiff
path: root/src/db
diff options
context:
space:
mode:
Diffstat (limited to 'src/db')
-rw-r--r--src/db/migrations/1-init.sql16
-rw-r--r--src/db/mod.rs42
2 files changed, 48 insertions, 10 deletions
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<Vec<Event>> {
- let mut stmt = conn.prepare("SELECT id, date, start, end, name, repeated FROM events")?;
+pub fn list_repeated(conn: &Connection) -> Result<Vec<Event>> {
+ 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<Vec<Event>> {
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<Vec<Event>> {
+ 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())
+}