headless_lms_models/
glossary.rs1use crate::prelude::*;
2
3#[derive(Debug, Serialize)]
4#[cfg_attr(feature = "ts_rs", derive(TS))]
5pub struct Term {
6 pub id: Uuid,
7 pub term: String,
8 pub definition: String,
9}
10
11#[derive(Debug, Deserialize)]
12#[cfg_attr(feature = "ts_rs", derive(TS))]
13pub struct TermUpdate {
14 pub term: String,
15 pub definition: String,
16}
17
18pub async fn insert(
19 conn: &mut PgConnection,
20 term: &str,
21 definition: &str,
22 course_id: Uuid,
23) -> ModelResult<Uuid> {
24 let res = sqlx::query!(
25 "
26INSERT INTO glossary (term, definition, course_id)
27SELECT $1, $2, $3
28RETURNING id
29",
30 term,
31 definition,
32 course_id
33 )
34 .fetch_one(conn)
35 .await?;
36 Ok(res.id)
37}
38
39pub async fn update(
40 conn: &mut PgConnection,
41 id: Uuid,
42 term: &str,
43 definition: &str,
44) -> ModelResult<()> {
45 sqlx::query!(
46 "
47UPDATE glossary
48SET term = $1,
49 definition = $2
50WHERE id = $3
51",
52 term,
53 definition,
54 id
55 )
56 .execute(conn)
57 .await?;
58 Ok(())
59}
60
61pub async fn delete(conn: &mut PgConnection, id: Uuid) -> ModelResult<()> {
62 sqlx::query!(
63 "
64UPDATE glossary
65SET deleted_at = now()
66WHERE id = $1
67",
68 id
69 )
70 .execute(conn)
71 .await?;
72 Ok(())
73}
74
75pub async fn fetch_for_course(conn: &mut PgConnection, course_id: Uuid) -> ModelResult<Vec<Term>> {
76 let res = sqlx::query_as!(
77 Term,
78 "
79SELECT glossary.id,
80 glossary.term,
81 glossary.definition
82FROM glossary
83WHERE glossary.course_id = $1
84AND deleted_at IS NULL
85",
86 course_id
87 )
88 .fetch_all(conn)
89 .await?;
90 Ok(res)
91}