aboutsummaryrefslogtreecommitdiff
path: root/src/db
diff options
context:
space:
mode:
Diffstat (limited to 'src/db')
-rw-r--r--src/db/migrations/1-init.sql3
-rw-r--r--src/db/mod.rs82
2 files changed, 60 insertions, 25 deletions
diff --git a/src/db/migrations/1-init.sql b/src/db/migrations/1-init.sql
index 7e49764..467e481 100644
--- a/src/db/migrations/1-init.sql
+++ b/src/db/migrations/1-init.sql
@@ -8,3 +8,6 @@ CREATE TABLE IF NOT EXISTS "events" (
"created" TEXT NOT NULL, /* DATETIME */
"updated" TEXT NOT NULL /* DATETIME */
);
+
+CREATE INDEX events_date_index on events (date);
+CREATE INDEX events_repetition_index on events (repetition);
diff --git a/src/db/mod.rs b/src/db/mod.rs
index 1bbf21e..3d498a8 100644
--- a/src/db/mod.rs
+++ b/src/db/mod.rs
@@ -1,5 +1,5 @@
use anyhow::Result;
-use chrono::NaiveDate;
+use chrono::{NaiveDate, NaiveTime};
use rusqlite::{params, Connection};
use rusqlite_migration::{Migrations, M};
use uuid::Uuid;
@@ -34,7 +34,7 @@ pub fn update(conn: &Connection, event: &Event) -> Result<()> {
};
conn.execute(
- "UPDATE events SET date = ?, start = ?, end = ?, name = ?, repetition = ?, updated = datetime() where id = ?",
+ "UPDATE events SET date = ?, start = ?, end = ?, name = ?, repetition = ?, updated = datetime() WHERE id = ?",
params![event.date, event.start, event.end, event.name, repetition, event.id.to_hyphenated().to_string()]
)?;
@@ -50,8 +50,7 @@ pub fn delete(conn: &Connection, id: &Uuid) -> Result<()> {
Ok(())
}
-// TODO: Don’t use unwrap
-pub fn list_repeated(conn: &Connection) -> Result<Vec<Event>> {
+pub fn list_recurring(conn: &Connection) -> Result<Vec<Event>> {
let mut stmt = conn.prepare(
"
SELECT id, date, start, end, name, repetition
@@ -60,23 +59,48 @@ pub fn list_repeated(conn: &Connection) -> Result<Vec<Event>> {
)?;
let iter = stmt.query_map([], |row| {
- let uuid: String = row.get(0)?;
- let repetition: Option<String> = row.get(5)?;
- 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: repetition.and_then(|r: String| serde_json::from_str(&r).ok()),
- })
+ Ok(read_recurring_event(
+ row.get(0)?,
+ row.get(1)?,
+ row.get(2)?,
+ row.get(3)?,
+ row.get(4)?,
+ row.get(5)?,
+ ))
})?;
- Ok(iter.map(|r| r.unwrap()).collect())
+ let mut res = vec![];
+ for event in iter {
+ res.push(event??)
+ }
+ Ok(res)
+}
+
+fn read_recurring_event(
+ uuid: String,
+ date: NaiveDate,
+ start: Option<NaiveTime>,
+ end: Option<NaiveTime>,
+ name: String,
+ repetition: Option<String>,
+) -> Result<Event> {
+ let id = Uuid::parse_str(&uuid)?;
+ let repetition = match repetition {
+ Some(r) => Some(serde_json::from_str(&r)?),
+ None => None,
+ };
+
+ Ok(Event {
+ id,
+ date,
+ start,
+ end,
+ name,
+ repetition,
+ })
}
-// TODO: Don’t use unwrap
-pub fn list_non_repeated_between(
+pub fn list_non_recurring_between(
conn: &Connection,
start: NaiveDate,
end: NaiveDate,
@@ -94,15 +118,23 @@ pub fn list_non_repeated_between(
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)?,
+ let date = row.get(1)?;
+ let start = row.get(2)?;
+ let end = row.get(3)?;
+ let name = row.get(4)?;
+ Ok(Uuid::parse_str(&uuid).map(|id| Event {
+ id,
+ date,
+ start,
+ end,
+ name,
repetition: None,
- })
+ }))
})?;
- Ok(iter.map(|r| r.unwrap()).collect())
+ let mut res = vec![];
+ for event in iter {
+ res.push(event??)
+ }
+ Ok(res)
}