headless_lms_models/
page_audio_files.rs

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