headless_lms_models/
privacy_link.rs

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

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct PrivacyLink {
    pub id: Uuid,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub deleted_at: Option<DateTime<Utc>>,
    pub title: String,
    pub url: String,
    pub course_id: Uuid,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct PrivacyLinkNew {
    pub course_id: Uuid,
    pub title: String,
    pub url: String,
}

pub async fn insert(
    conn: &mut PgConnection,
    course_id: Uuid,
    title: String,
    url: String,
) -> ModelResult<PrivacyLink> {
    let res = sqlx::query_as!(
        PrivacyLink,
        r#"
INSERT INTO privacy_links (course_id, title, url)
VALUES ($1, $2, $3)
RETURNING *
"#,
        course_id,
        title,
        url,
    )
    .fetch_one(conn)
    .await?;
    Ok(res)
}

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

pub async fn delete_privacy_link(
    conn: &mut PgConnection,
    course_id: Uuid,
) -> ModelResult<PrivacyLink> {
    let deleted = sqlx::query_as!(
        PrivacyLink,
        r#"
UPDATE privacy_links
SET deleted_at = now()
WHERE course_id = $1
RETURNING *
  "#,
        course_id
    )
    .fetch_one(conn)
    .await?;
    Ok(deleted)
}