aboutsummaryrefslogtreecommitdiff
path: root/src/db/events.rs
blob: 86206bb99edd9d0efcd823072e1ca181729456c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use anyhow::Result;
use chrono::{NaiveDate, NaiveTime};
use rusqlite::{params, Connection};
use uuid::Uuid;

use crate::model::event::Event;

pub fn insert(conn: &Connection, event: &Event) -> Result<()> {
    let repetition = match &event.repetition {
        Some(r) => Some(serde_json::to_string(&r)?),
        None => None,
    };

    let category = event.category.map(|id| id.to_hyphenated().to_string());

    conn.execute(
        "INSERT INTO events (id, date, start, end, name, repetition, category, created, updated) VALUES (?, ?, ?, ?, ?, ?, ?, datetime(), datetime())",
        params![event.id.to_hyphenated().to_string(), event.date, event.start, event.end, event.name, repetition, category]
    )?;

    Ok(())
}

pub fn update(conn: &Connection, event: &Event) -> Result<()> {
    let repetition = match &event.repetition {
        Some(r) => Some(serde_json::to_string(&r)?),
        None => None,
    };

    let category = event.category.map(|id| id.to_hyphenated().to_string());

    conn.execute(
        "UPDATE events SET date = ?, start = ?, end = ?, name = ?, repetition = ?, category = ?, updated = datetime() WHERE id = ?",
        params![event.date, event.start, event.end, event.name, repetition, category, 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(())
}

pub fn list_recurring(conn: &Connection) -> Result<Vec<Event>> {
    let mut stmt = conn.prepare(
        "
        SELECT id, date, start, end, name, repetition, category
        FROM events
        WHERE repetition IS NOT NULL",
    )?;

    let iter = stmt.query_map([], |row| {
        Ok(read_event(
            row.get(0)?,
            row.get(1)?,
            row.get(2)?,
            row.get(3)?,
            row.get(4)?,
            row.get(5)?,
            row.get(6)?,
        ))
    })?;

    let mut res = vec![];
    for event in iter {
        res.push(event??)
    }
    Ok(res)
}

pub fn list_non_recurring_between(
    conn: &Connection,
    start: NaiveDate,
    end: NaiveDate,
) -> Result<Vec<Event>> {
    let mut stmt = conn.prepare(
        "
        SELECT id, date, start, end, name, category
        FROM events
        WHERE 
            repetition IS NULL 
            AND date >= ?
            AND date <= ?
    ",
    )?;

    let iter = stmt.query_map([start, end], |row| {
        Ok(read_event(
            row.get(0)?,
            row.get(1)?,
            row.get(2)?,
            row.get(3)?,
            row.get(4)?,
            None,
            row.get(5)?,
        ))
    })?;

    let mut res = vec![];
    for event in iter {
        res.push(event??)
    }
    Ok(res)
}

fn read_event(
    id: String,
    date: NaiveDate,
    start: Option<NaiveTime>,
    end: Option<NaiveTime>,
    name: String,
    repetition: Option<String>,
    category: Option<String>,
) -> Result<Event> {
    let id = Uuid::parse_str(&id)?;
    let repetition = match repetition {
        Some(r) => Some(serde_json::from_str(&r)?),
        None => None,
    };
    let category = match category {
        Some(c) => Some(Uuid::parse_str(&c)?),
        None => None,
    };

    Ok(Event {
        id,
        date,
        start,
        end,
        name,
        repetition,
        category,
    })
}