headless_lms_models/
page_language_groups.rs

1use std::collections::HashMap;
2
3use crate::prelude::*;
4
5#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
6pub struct PageLanguageGroup {
7    pub id: Uuid,
8    pub created_at: DateTime<Utc>,
9    pub updated_at: DateTime<Utc>,
10    pub deleted_at: Option<DateTime<Utc>>,
11    pub course_language_group_id: Uuid,
12}
13
14pub async fn insert(
15    conn: &mut PgConnection,
16    pkey_policy: PKeyPolicy<Uuid>,
17    course_language_group_id: Uuid,
18) -> ModelResult<Uuid> {
19    let res = sqlx::query!(
20        "
21INSERT INTO page_language_groups (
22  id,
23  course_language_group_id
24  )
25VALUES ($1, $2)
26RETURNING id
27        ",
28        pkey_policy.into_uuid(),
29        course_language_group_id
30    )
31    .fetch_one(conn)
32    .await?;
33    Ok(res.id)
34}
35
36pub struct PageLanguageGroupNavigationInfo {
37    pub page_language_group_id: Uuid,
38    pub page_id: Uuid,
39    pub course_id: Option<Uuid>,
40    pub exam_id: Option<Uuid>,
41    pub page_path: String,
42}
43
44/** Can be used to find the same page in all the different language versions of the course. Returns a mappgig from course_id or exam_id to the page language group navigation info. */
45pub async fn get_all_pages_in_page_language_group_mapping(
46    conn: &mut PgConnection,
47    page_id: Uuid,
48) -> ModelResult<HashMap<CourseOrExamId, PageLanguageGroupNavigationInfo>> {
49    let res = sqlx::query_as!(
50        PageLanguageGroupNavigationInfo,
51        "
52SELECT plg.id AS page_language_group_id,
53  p.id AS page_id,
54  p.url_path AS page_path,
55  p.course_id AS course_id,
56  p.exam_id AS exam_id
57FROM page_language_groups plg
58  JOIN pages p ON plg.id = p.page_language_group_id
59WHERE plg.id = (SELECT page_language_group_id FROM pages WHERE id = $1)
60  AND plg.deleted_at IS NULL
61  AND p.deleted_at IS NULL
62        ",
63        page_id,
64    )
65    .fetch_all(conn)
66    .await?;
67    let mut result = HashMap::new();
68    for x in res {
69        let key = CourseOrExamId::from_course_and_exam_ids(x.course_id, x.exam_id)?;
70        result.insert(key, x);
71    }
72    Ok(result)
73}