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

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct OpenUniversityRegistrationLink {
    pub uh_course_code: String,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub deleted_at: Option<DateTime<Utc>>,
    pub registration_link: String,
}

pub async fn upsert(
    conn: &mut PgConnection,
    uh_course_code: &str,
    registration_link: &str,
) -> ModelResult<OpenUniversityRegistrationLink> {
    let res = sqlx::query_as!(
        OpenUniversityRegistrationLink,
        "
INSERT INTO open_university_registration_links (uh_course_code, registration_link)
VALUES ($1, $2) ON CONFLICT (uh_course_code) DO
UPDATE
SET registration_link = $2,
  deleted_at = NULL
RETURNING *
        ",
        uh_course_code,
        registration_link,
    )
    .fetch_one(conn)
    .await?;
    Ok(res)
}

pub async fn get_link_by_course_code(
    conn: &mut PgConnection,
    uh_course_code: &str,
) -> ModelResult<String> {
    let res = sqlx::query!(
        "
SELECT registration_link
FROM open_university_registration_links
WHERE uh_course_code = $1
  AND deleted_at IS NULL
        ",
        uh_course_code
    )
    .map(|record| record.registration_link)
    .fetch_one(conn)
    .await?;
    Ok(res)
}