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
use crate::prelude::*;

pub async fn insert(
    conn: &mut PgConnection,
    name: &str,
    path: &str,
    mime: &str,
    uploader: Option<Uuid>,
) -> ModelResult<Uuid> {
    let res = sqlx::query!(
        r#"
INSERT INTO file_uploads(path, name, mime, uploaded_by_user)
VALUES ($1, $2, $3, $4)
RETURNING id
"#,
        path,
        name,
        mime,
        uploader
    )
    .fetch_one(conn)
    .await?;
    Ok(res.id)
}

pub async fn get_filename(conn: &mut PgConnection, path: &str) -> ModelResult<String> {
    let res = sqlx::query!(
        r#"
SELECT name
FROM file_uploads
WHERE path = $1
"#,
        path,
    )
    .fetch_one(conn)
    .await?;
    Ok(res.name)
}

pub async fn delete_and_fetch_path(conn: &mut PgConnection, id: Uuid) -> ModelResult<String> {
    let res = sqlx::query!(
        "
UPDATE file_uploads
SET deleted_at = now()
WHERE id = $1
RETURNING path
",
        id
    )
    .fetch_one(conn)
    .await?;
    Ok(res.path)
}