headless_lms_models/
page_audio_files.rs1use 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 *
47 "#,
48 id
49 )
50 .fetch_one(conn)
51 .await?;
52 Ok(response.path)
53}
54
55pub async fn path_is_referenced(conn: &mut PgConnection, path: &str) -> ModelResult<bool> {
57 let referenced = sqlx::query_scalar!(
58 r#"
59SELECT EXISTS(
60 SELECT 1 FROM page_audio_files WHERE path = $1 AND deleted_at IS NULL
61) AS "exists!"
62 "#,
63 path
64 )
65 .fetch_one(conn)
66 .await?;
67 Ok(referenced)
68}
69
70pub async fn get_page_audio_files(
71 conn: &mut PgConnection,
72 page_id: Uuid,
73) -> ModelResult<Vec<PageAudioFile>> {
74 let audio_files = sqlx::query_as!(
75 PageAudioFile,
76 "
77SELECT *
78FROM page_audio_files
79WHERE page_id = $1
80AND deleted_at IS NULL;
81",
82 page_id
83 )
84 .fetch_all(conn)
85 .await?;
86 Ok(audio_files)
87}
88
89pub async fn get_page_audio_files_by_id(
90 conn: &mut PgConnection,
91 id: Uuid,
92) -> ModelResult<PageAudioFile> {
93 let audio_files = sqlx::query_as!(
94 PageAudioFile,
95 "
96SELECT *
97FROM page_audio_files
98WHERE id = $1
99AND deleted_at IS NULL;
100",
101 id
102 )
103 .fetch_one(conn)
104 .await?;
105 Ok(audio_files)
106}