headless_lms_models/
student_countries.rs1use crate::prelude::*;
2use utoipa::ToSchema;
3
4#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, ToSchema)]
5
6pub struct StudentCountry {
7 pub id: Uuid,
8 pub user_id: Uuid,
9 pub course_id: Uuid,
10 pub course_instance_id: Uuid,
11 pub country_code: String,
12 pub created_at: DateTime<Utc>,
13 pub deleted_at: Option<DateTime<Utc>>,
14}
15
16pub async fn insert(
17 conn: &mut PgConnection,
18 user_id: Uuid,
19 course_id: Uuid,
20 course_instance_id: Uuid,
21 country_code: &str,
22) -> ModelResult<()> {
23 sqlx::query!(
24 "
25INSERT INTO student_countries (
26 user_id,
27 course_id,
28 course_instance_id,
29 country_code
30)
31VALUES($1, $2, $3, $4)
32 ",
33 user_id,
34 course_id,
35 course_instance_id,
36 country_code,
37 )
38 .execute(conn)
39 .await?;
40 Ok(())
41}
42
43pub async fn delete_student_country(conn: &mut PgConnection, id: Uuid) -> ModelResult<()> {
44 sqlx::query!(
45 r#"
46UPDATE student_countries
47SET deleted_at = now()
48WHERE id = $1
49AND deleted_at IS NULL
50 "#,
51 id
52 )
53 .execute(conn)
54 .await?;
55 Ok(())
56}
57
58pub async fn get_countries(
59 conn: &mut PgConnection,
60 course_id: Uuid,
61 course_instance_id: Uuid,
62) -> ModelResult<Vec<StudentCountry>> {
63 let student_countries = sqlx::query_as!(
64 StudentCountry,
65 "
66SELECT *
67FROM student_countries
68WHERE course_id = $1
69AND course_instance_id = $2
70AND deleted_at IS NULL;
71",
72 course_id,
73 course_instance_id,
74 )
75 .fetch_all(conn)
76 .await?;
77 Ok(student_countries)
78}
79
80pub async fn get_selected_country_by_user_id(
81 conn: &mut PgConnection,
82 user_id: Uuid,
83 course_instance_id: Uuid,
84) -> ModelResult<StudentCountry> {
85 let country = sqlx::query_as!(
86 StudentCountry,
87 "
88SELECT *
89FROM student_countries
90WHERE user_id = $1
91AND course_instance_id = $2
92AND deleted_at IS NULL;
93",
94 user_id,
95 course_instance_id,
96 )
97 .fetch_one(conn)
98 .await?;
99 Ok(country)
100}