1use std::collections::HashMap;
2
3use crate::media::{Media, MediaId};
4use sqlx::{AssertSqlSafe, Sqlite};
5use time::PrimitiveDateTime;
6
7pub async fn insert<'e, E>(
8 ex: E,
9 path: &str,
10 content_type: &str,
11 byte_size: i64,
12 width: i64,
13 height: i64,
14) -> Result<MediaId, sqlx::Error>
15where
16 E: sqlx::Executor<'e, Database = Sqlite>,
17{
18 sqlx::query_scalar!(
19 r#"INSERT INTO media (path, content_type, byte_size, width, height)
20 VALUES (?1, ?2, ?3, ?4, ?5)
21 RETURNING id AS "id!: MediaId""#,
22 path,
23 content_type,
24 byte_size,
25 width,
26 height,
27 )
28 .fetch_one(ex)
29 .await
30}
31
32pub async fn update_bytes<'e, E>(
33 ex: E,
34 id: MediaId,
35 byte_size: i64,
36 width: i64,
37 height: i64,
38) -> Result<(), sqlx::Error>
39where
40 E: sqlx::Executor<'e, Database = Sqlite>,
41{
42 sqlx::query!(
43 "UPDATE media SET byte_size = ?2, width = ?3, height = ?4 WHERE id = ?1",
44 id,
45 byte_size,
46 width,
47 height,
48 )
49 .execute(ex)
50 .await
51 .map(|_| ())
52}
53
54pub async fn get<'e, E>(ex: E, id: MediaId) -> Result<Option<Media>, sqlx::Error>
55where
56 E: sqlx::Executor<'e, Database = Sqlite>,
57{
58 sqlx::query_as!(
59 Media,
60 r#"SELECT id AS "id!: MediaId",
61 path AS "path!: String",
62 content_type AS "content_type!: String",
63 byte_size AS "byte_size!: i64",
64 width AS "width!: i64",
65 height AS "height!: i64",
66 created_at AS "created_at!: PrimitiveDateTime",
67 updated_at AS "updated_at!: PrimitiveDateTime"
68 FROM media WHERE id = ?1"#,
69 id,
70 )
71 .fetch_optional(ex)
72 .await
73}
74
75pub async fn get_by_path<'e, E>(ex: E, path: &str) -> Result<Option<Media>, sqlx::Error>
77where
78 E: sqlx::Executor<'e, Database = Sqlite>,
79{
80 sqlx::query_as!(
81 Media,
82 r#"SELECT id AS "id!: MediaId",
83 path AS "path!: String",
84 content_type AS "content_type!: String",
85 byte_size AS "byte_size!: i64",
86 width AS "width!: i64",
87 height AS "height!: i64",
88 created_at AS "created_at!: PrimitiveDateTime",
89 updated_at AS "updated_at!: PrimitiveDateTime"
90 FROM media WHERE path = ?1"#,
91 path,
92 )
93 .fetch_optional(ex)
94 .await
95}
96
97#[derive(Debug, sqlx::FromRow)]
99struct KeyedMedia {
100 key: String,
101 #[sqlx(flatten)]
102 media: Media,
103}
104
105pub async fn fetch_keyed<'e, E>(
108 ex: E,
109 sql: &str,
110 keys: &[&str],
111) -> Result<HashMap<String, Media>, sqlx::Error>
112where
113 E: sqlx::Executor<'e, Database = Sqlite>,
114{
115 let sql = AssertSqlSafe(sql);
116 let mut query = sqlx::query_as::<_, KeyedMedia>(sql);
117 for key in keys {
118 query = query.bind(key);
119 }
120 let rows = query.fetch_all(ex).await?;
121 Ok(rows.into_iter().map(|row| (row.key, row.media)).collect())
122}
123
124pub(crate) struct LogoColumns {
126 pub id: Option<MediaId>,
127 pub path: Option<String>,
128 pub content_type: Option<String>,
129 pub byte_size: Option<i64>,
130 pub width: Option<i64>,
131 pub height: Option<i64>,
132 pub created_at: Option<PrimitiveDateTime>,
133 pub updated_at: Option<PrimitiveDateTime>,
134}
135
136pub(crate) fn build_logo(cols: LogoColumns) -> Option<Media> {
139 cols.id.zip(cols.path).map(|(id, path)| Media {
140 id,
141 path,
142 content_type: cols.content_type.expect("media.content_type NOT NULL"),
143 byte_size: cols.byte_size.expect("media.byte_size NOT NULL"),
144 width: cols.width.expect("media.width NOT NULL"),
145 height: cols.height.expect("media.height NOT NULL"),
146 created_at: cols.created_at.expect("media.created_at NOT NULL"),
147 updated_at: cols.updated_at.expect("media.updated_at NOT NULL"),
148 })
149}
150
151pub async fn delete<'e, E>(ex: E, id: MediaId) -> Result<(), sqlx::Error>
152where
153 E: sqlx::Executor<'e, Database = Sqlite>,
154{
155 sqlx::query!("DELETE FROM media WHERE id = ?1", id)
156 .execute(ex)
157 .await
158 .map(|_| ())
159}