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

#[derive(Debug, Serialize)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct Term {
    pub id: Uuid,
    pub term: String,
    pub definition: String,
}

#[derive(Debug, Deserialize)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct TermUpdate {
    pub term: String,
    pub definition: String,
}

pub async fn insert(
    conn: &mut PgConnection,
    term: &str,
    definition: &str,
    course_id: Uuid,
) -> ModelResult<Uuid> {
    let res = sqlx::query!(
        "
INSERT INTO glossary (term, definition, course_id)
SELECT $1, $2, $3
RETURNING id
",
        term,
        definition,
        course_id
    )
    .fetch_one(conn)
    .await?;
    Ok(res.id)
}

pub async fn update(
    conn: &mut PgConnection,
    id: Uuid,
    term: &str,
    definition: &str,
) -> ModelResult<()> {
    sqlx::query!(
        "
UPDATE glossary
SET term = $1,
  definition = $2
WHERE id = $3
",
        term,
        definition,
        id
    )
    .execute(conn)
    .await?;
    Ok(())
}

pub async fn delete(conn: &mut PgConnection, id: Uuid) -> ModelResult<()> {
    sqlx::query!(
        "
UPDATE glossary
SET deleted_at = now()
WHERE id = $1
",
        id
    )
    .execute(conn)
    .await?;
    Ok(())
}

pub async fn fetch_for_course(conn: &mut PgConnection, course_id: Uuid) -> ModelResult<Vec<Term>> {
    let res = sqlx::query_as!(
        Term,
        "
SELECT glossary.id,
  glossary.term,
  glossary.definition
FROM glossary
WHERE glossary.course_id = $1
AND deleted_at IS NULL
",
        course_id
    )
    .fetch_all(conn)
    .await?;
    Ok(res)
}