1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use crate::user::UserID;
use crate::DatabaseError;
use std::path::PathBuf;
#[derive(Debug, Clone, Default)]
pub struct Asset {
pub id: i64,
pub remote_url: Option<String>,
pub local_path: String,
pub file_ext: String,
}
impl Asset {
pub async fn get_by_id(
conn: &mut crate::Transaction<'_>,
id: i64,
) -> Result<Self, DatabaseError> {
Ok(
sqlx::query_as!(Asset, "SELECT * FROM assets WHERE id = ?", id)
.fetch_one(&mut *conn)
.await?,
)
}
pub async fn get_of_user(
conn: &mut crate::Transaction<'_>,
uid: UserID,
) -> Result<Self, DatabaseError> {
Ok(sqlx::query_as!(
Asset,
r#"SELECT assets.* FROM assets
INNER JOIN users ON users.picture = assets.id
WHERE users.id = ?"#,
uid
)
.fetch_one(&mut *conn)
.await?)
}
pub async fn into_media_poster(
&self,
conn: &mut crate::Transaction<'_>,
media_id: i64,
) -> Result<i64, DatabaseError> {
Ok(sqlx::query!(
r#"INSERT INTO media_posters (media_id, asset_id)
VALUES ($1, $2) RETURNING id as "id!: i64""#,
media_id,
self.id
)
.fetch_one(&mut *conn)
.await?
.id)
}
pub async fn into_media_backdrop(
&self,
conn: &mut crate::Transaction<'_>,
media_id: i64,
) -> Result<i64, DatabaseError> {
Ok(sqlx::query!(
r#"INSERT INTO media_backdrops (media_id, asset_id)
VALUES ($1, $2) RETURNING id as "id!: i64""#,
media_id,
self.id
)
.fetch_one(&mut *conn)
.await?
.id)
}
pub async fn get_url_by_file(
conn: &mut crate::Transaction<'_>,
path: &PathBuf,
) -> Result<String, DatabaseError> {
let cleaned_path: &str = &path.to_string_lossy();
Ok(sqlx::query!(
r#"SELECT remote_url as "remote_url!" FROM assets WHERE Local_path = ?"#,
cleaned_path
)
.fetch_one(&mut *conn)
.await?
.remote_url)
}
}
#[derive(Debug, Clone, Default)]
pub struct InsertableAsset {
pub remote_url: Option<String>,
pub local_path: String,
pub file_ext: String,
}
impl InsertableAsset {
pub async fn insert_many(
tx: &mut crate::Transaction<'_>,
assets: Vec<Self>,
) -> Result<Vec<Asset>, DatabaseError> {
let mut results = vec![];
for asset in assets {
results.push(asset.insert(tx).await?);
}
Ok(results)
}
pub async fn insert(self, conn: &mut crate::Transaction<'_>) -> Result<Asset, DatabaseError> {
let url = self.remote_url.clone().unwrap_or_default();
if let Ok(x) =
sqlx::query_as_unchecked!(Asset, "SELECT * FROM assets WHERE remote_url = ?", url)
.fetch_one(&mut *conn)
.await
{
return Ok(x);
}
sqlx::query!(
"INSERT INTO assets (remote_url, local_path, file_ext)
VALUES ($1, $2, $3)",
self.remote_url,
self.local_path,
self.file_ext
)
.execute(&mut *conn)
.await?;
let result = sqlx::query_as!(
Asset,
r#"SELECT id as "id!", remote_url, local_path, file_ext FROM assets WHERE remote_url = ?"#,
url
)
.fetch_one(&mut *conn)
.await?;
Ok(result)
}
}