Skip to main content

headless_lms_models/
page_audio_files.rs

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