headless_lms_models/
glossary.rs1use crate::prelude::*;
2use utoipa::ToSchema;
3
4#[derive(Debug, Serialize, ToSchema)]
5
6pub struct Term {
7 pub id: Uuid,
8 pub term: String,
9 pub definition: String,
10}
11
12#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, ToSchema)]
13pub struct GlossaryTerm {
14 pub id: Uuid,
15 pub created_at: DateTime<Utc>,
16 pub updated_at: DateTime<Utc>,
17 pub deleted_at: Option<DateTime<Utc>>,
18 pub term: String,
19 pub definition: String,
20 pub course_id: Uuid,
21}
22
23#[derive(Debug, Deserialize, ToSchema)]
24
25pub struct TermUpdate {
26 pub term: String,
27 pub definition: String,
28}
29
30pub async fn insert(
31 conn: &mut PgConnection,
32 term: &str,
33 definition: &str,
34 course_id: Uuid,
35) -> ModelResult<Uuid> {
36 let res = sqlx::query!(
37 "
38INSERT INTO glossary (term, definition, course_id)
39SELECT $1, $2, $3
40RETURNING id
41",
42 term,
43 definition,
44 course_id
45 )
46 .fetch_one(conn)
47 .await?;
48 Ok(res.id)
49}
50
51pub async fn update(
52 conn: &mut PgConnection,
53 id: Uuid,
54 term: &str,
55 definition: &str,
56) -> ModelResult<()> {
57 sqlx::query!(
58 "
59UPDATE glossary
60SET term = $1,
61 definition = $2
62WHERE id = $3
63",
64 term,
65 definition,
66 id
67 )
68 .execute(conn)
69 .await?;
70 Ok(())
71}
72
73pub async fn get_term_by_id(conn: &mut PgConnection, id: Uuid) -> ModelResult<GlossaryTerm> {
74 let res = sqlx::query_as!(
75 GlossaryTerm,
76 "
77SELECT *
78FROM glossary
79WHERE id = $1
80 AND deleted_at IS NULL
81 ",
82 id
83 )
84 .fetch_one(conn)
85 .await?;
86 Ok(res)
87}
88
89pub async fn update_term_by_id_and_course_id(
90 conn: &mut PgConnection,
91 id: Uuid,
92 course_id: Uuid,
93 term: &str,
94 definition: &str,
95) -> ModelResult<GlossaryTerm> {
96 let res = sqlx::query_as!(
97 GlossaryTerm,
98 "
99UPDATE glossary
100SET term = $1,
101 definition = $2
102WHERE id = $3
103 AND course_id = $4
104 AND deleted_at IS NULL
105RETURNING *
106 ",
107 term,
108 definition,
109 id,
110 course_id
111 )
112 .fetch_one(conn)
113 .await?;
114 Ok(res)
115}
116
117pub async fn delete(conn: &mut PgConnection, id: Uuid) -> ModelResult<()> {
118 sqlx::query!(
119 "
120UPDATE glossary
121SET deleted_at = now()
122WHERE id = $1
123AND deleted_at IS NULL
124",
125 id
126 )
127 .execute(conn)
128 .await?;
129 Ok(())
130}
131
132pub async fn delete_term_by_id_and_course_id(
133 conn: &mut PgConnection,
134 id: Uuid,
135 course_id: Uuid,
136) -> ModelResult<GlossaryTerm> {
137 let res = sqlx::query_as!(
138 GlossaryTerm,
139 "
140UPDATE glossary
141SET deleted_at = now()
142WHERE id = $1
143 AND course_id = $2
144 AND deleted_at IS NULL
145RETURNING *
146 ",
147 id,
148 course_id
149 )
150 .fetch_one(conn)
151 .await?;
152 Ok(res)
153}
154
155pub async fn fetch_for_course(conn: &mut PgConnection, course_id: Uuid) -> ModelResult<Vec<Term>> {
156 let res = sqlx::query_as!(
157 Term,
158 "
159SELECT glossary.id,
160 glossary.term,
161 glossary.definition
162FROM glossary
163WHERE glossary.course_id = $1
164AND deleted_at IS NULL
165",
166 course_id
167 )
168 .fetch_all(conn)
169 .await?;
170 Ok(res)
171}