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
67AND deleted_at IS NULL
68",
69 id
70 )
71 .execute(conn)
72 .await?;
73 Ok(())
74}
75
76pub async fn fetch_for_course(conn: &mut PgConnection, course_id: Uuid) -> ModelResult<Vec<Term>> {
77 let res = sqlx::query_as!(
78 Term,
79 "
80SELECT glossary.id,
81 glossary.term,
82 glossary.definition
83FROM glossary
84WHERE glossary.course_id = $1
85AND deleted_at IS NULL
86",
87 course_id
88 )
89 .fetch_all(conn)
90 .await?;
91 Ok(res)
92}