aboutsummaryrefslogtreecommitdiff
path: root/src/db.rs
blob: ab699c66ca1c1c2a39da2e5df54c824239c90901 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use chrono::{DateTime, Local};
use tokio_rusqlite::{params, Connection, Result};

use crate::model::{decode_datetime, encode_datetime, File};

pub async fn insert_file(conn: &Connection, file: File) -> Result<()> {
    conn.call(move |conn| {
        conn.execute(
            r#"
                INSERT INTO
                    files(id, created_at, expires_at, filename, content_length)
                VALUES
                    (?1, datetime(), ?2, ?3, ?4)
                "#,
            params![
                file.id,
                encode_datetime(file.expires_at),
                file.name,
                file.content_length
            ],
        )
        .map_err(tokio_rusqlite::Error::Rusqlite)
    })
    .await
    .map(|_| ())
}

pub async fn get_file(conn: &Connection, file_id: String) -> Result<Option<File>> {
    conn.call(move |conn| {
        let mut stmt = conn.prepare(
            r#"
            SELECT
                filename, expires_at, content_length
            FROM
                files
            WHERE
                id = ?
                AND expires_at > datetime()
            "#,
        )?;

        let mut iter = stmt.query_map([file_id.clone()], |row| {
            let res: (String, String, usize) = (row.get(0)?, row.get(1)?, row.get(2)?);
            Ok(res)
        })?;

        match iter.next() {
            Some(Ok((filename, expires_at, content_length))) => {
                match decode_datetime(&expires_at) {
                    Some(expires_at) => Ok(Some(File {
                        id: file_id.clone(),
                        name: filename,
                        expires_at,
                        content_length,
                    })),
                    _ => Err(rusqlite_other_error(&format!(
                        "Error decoding datetime: {expires_at}"
                    ))),
                }
            }
            Some(_) => Err(rusqlite_other_error("Error reading file in DB")),
            None => Ok(None),
        }
    })
    .await
}

pub async fn list_expire_after(conn: &Connection, time: DateTime<Local>) -> Result<Vec<String>> {
    conn.call(move |conn| {
        let mut stmt = conn.prepare(
            r#"
            SELECT
                id
            FROM
                files
            WHERE
                expires_at > ?
            "#,
        )?;

        let iter = stmt.query_map([encode_datetime(time)], |row| row.get(0))?;

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

pub async fn remove_expire_before(conn: &Connection, time: DateTime<Local>) -> Result<()> {
    conn.call(move |conn| {
        conn.execute(
            &format!(
                r#"
            DELETE FROM
                files
            WHERE
                expires_at <= ?
            "#
            ),
            [encode_datetime(time)]
        )?;

        Ok(())
    })
    .await
}

fn rusqlite_other_error(msg: &str) -> tokio_rusqlite::Error {
    tokio_rusqlite::Error::Other(msg.into())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{generate_file_id, local_time};
    use chrono::Duration;
    use std::collections::HashSet;
    use std::ops::Add;

    #[tokio::test]
    async fn test_insert_and_get_file() {
        let conn = get_connection().await;
        let file = dummy_file(Duration::minutes(1));
        assert!(insert_file(&conn, file.clone()).await.is_ok());
        let file_res = get_file(&conn, file.id.clone()).await;
        assert!(file_res.is_ok());
        assert_eq!(file_res.unwrap(), Some(file));
    }

    #[tokio::test]
    async fn test_expired_file_err() {
        let conn = get_connection().await;
        let file = dummy_file(Duration::zero());
        assert!(insert_file(&conn, file.clone()).await.is_ok());
        let file_res = get_file(&conn, file.id.clone()).await;
        assert!(file_res.is_ok());
        assert!(file_res.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_wrong_file_err() {
        let conn = get_connection().await;
        let file = dummy_file(Duration::minutes(1));
        assert!(insert_file(&conn, file.clone()).await.is_ok());
        let file_res = get_file(&conn, "wrong-id".to_string()).await;
        assert!(file_res.is_ok());
        assert!(file_res.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_list_non_expirable() {
        let conn = get_connection().await;
        let file_expire = dummy_file(Duration::zero());
        let file_no_expire_1 = dummy_file(Duration::minutes(1));
        let file_no_expire_2 = dummy_file(Duration::minutes(1));
        assert!(insert_file(&conn, file_expire.clone()).await.is_ok());
        assert!(insert_file(&conn, file_no_expire_1.clone()).await.is_ok());
        assert!(insert_file(&conn, file_no_expire_2.clone()).await.is_ok());
        let list = list_expire_after(&conn, Local::now()).await;
        assert!(list.is_ok());
        assert_eq!(
            HashSet::from_iter(list.unwrap().iter()),
            HashSet::from([&file_no_expire_1.id, &file_no_expire_2.id])
        )
    }

    #[tokio::test]
    async fn test_remove_expire_before() {
        let conn = get_connection().await;
        let file_1 = dummy_file(Duration::zero());
        let file_2 = dummy_file(Duration::zero());
        let file_3 = dummy_file(Duration::minutes(1));
        let file_4 = dummy_file(Duration::minutes(1));
        assert!(insert_file(&conn, file_1.clone()).await.is_ok());
        assert!(insert_file(&conn, file_2.clone()).await.is_ok());
        assert!(insert_file(&conn, file_3.clone()).await.is_ok());
        assert!(insert_file(&conn, file_4.clone()).await.is_ok());
        assert!(remove_expire_before(&conn, Local::now()).await.is_ok());
        assert!(get_file(&conn, file_1.id).await.unwrap().is_none());
        assert!(get_file(&conn, file_2.id).await.unwrap().is_none());
        assert!(get_file(&conn, file_3.id).await.unwrap().is_some());
        assert!(get_file(&conn, file_4.id).await.unwrap().is_some());
    }

    fn dummy_file(td: Duration) -> File {
        File {
            id: generate_file_id(),
            name: "foo".to_string(),
            expires_at: local_time().add(td),
            content_length: 100,
        }
    }

    async fn get_connection() -> Connection {
        let conn = Connection::open_in_memory().await.unwrap();
        let init_db = tokio::fs::read_to_string("init-db.sql").await.unwrap();

        let res = conn.call(move |conn| Ok(conn.execute(&init_db, []))).await;
        assert!(res.is_ok());
        conn
    }
}