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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
use crate::database::DatabaseWrapper;
use serde::{Deserialize, Serialize};
use sqlx::mysql::MySqlQueryResult;
#[derive(Serialize)]
pub struct Album {
name: Option<String>,
id: Option<i32>,
cover: Option<String>,
artist_name: Option<String>,
artist_id: Option<i32>,
}
#[derive(Serialize, Deserialize)]
pub struct AlbumPost {
name: Option<String>,
cover: Option<String>,
artist_id: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct AlbumPut {
id: Option<String>,
name: Option<String>,
cover: Option<String>,
artist_id: Option<String>,
}
impl DatabaseWrapper {
pub async fn select_albums(&self) -> Result<Vec<Album>, sqlx::Error> {
sqlx::query_as!(
Album,
"SELECT album.name, album.id, album.cover,
artist.name as artist_name, artist.id as artist_id
FROM album
INNER JOIN artist ON album.artist_id = artist.id
",
)
.fetch_all(&self.db_pool)
.await
}
pub async fn select_album_by_id(&self, id: &str) -> Result<Option<Album>, sqlx::Error> {
sqlx::query_as!(
Album,
"SELECT album.name, album.id, album.cover,
artist.name as artist_name, artist.id as artist_id
FROM album
INNER JOIN artist ON album.artist_id = artist.id
WHERE album.id=?",
id,
)
.fetch_optional(&self.db_pool)
.await
}
pub async fn select_albums_by_name(&self, name_raw: &str) -> Result<Vec<Album>, sqlx::Error> {
let name: String = format!("{}{}{}", "%", name_raw, "%");
sqlx::query_as!(
Album,
"SELECT album.name, album.id, album.cover,
artist.name as artist_name, artist.id as artist_id
FROM album
INNER JOIN artist ON album.artist_id = artist.id
WHERE LOWER(album.name) LIKE LOWER(?)
",
name,
)
.fetch_all(&self.db_pool)
.await
}
pub async fn select_albums_by_artist(
&self,
artist_id: &str,
) -> Result<Vec<Album>, sqlx::Error> {
sqlx::query_as!(
Album,
"SELECT album.name, album.id, album.cover,
artist.name as artist_name, artist.id as artist_id
FROM album
INNER JOIN artist ON album.artist_id = artist.id
WHERE artist.id=?",
artist_id,
)
.fetch_all(&self.db_pool)
.await
}
pub async fn create_album(&self, data: AlbumPost) -> Result<MySqlQueryResult, sqlx::Error> {
if data.name.is_none() || data.artist_id.is_none() {
return Err(sqlx::Error::RowNotFound);
}
if match self
.select_artist_by_id(data.artist_id.as_ref().unwrap())
.await
{
Ok(res) => res.is_none(),
Err(_) => true,
} {
return Err(sqlx::Error::RowNotFound);
}
sqlx::query!(
"INSERT INTO album (name, cover, artist_id)
VALUE (?, ?, ?)",
data.name,
data.cover.unwrap_or(String::default()),
data.artist_id,
)
.execute(&self.db_pool)
.await
}
pub async fn edit_album(&self, data: AlbumPut) -> Result<MySqlQueryResult, sqlx::Error> {
if data.id.is_none() {
return Err(sqlx::Error::RowNotFound);
}
let og_album: Album = match self.select_album_by_id(data.id.as_ref().unwrap()).await {
Ok(res) => match res.is_some() {
true => res.unwrap(),
false => return Err(sqlx::Error::RowNotFound),
},
Err(_) => return Err(sqlx::Error::RowNotFound),
};
sqlx::query!(
"UPDATE album SET name=?, cover=? WHERE id=?",
data.name
.unwrap_or(og_album.name.unwrap_or(String::default())),
data.cover
.unwrap_or(og_album.cover.unwrap_or(String::default())),
data.id,
)
.execute(&self.db_pool)
.await
}
pub async fn delete_album(&self, id: i32) -> Result<MySqlQueryResult, sqlx::Error> {
let _ = sqlx::query!(
"DELETE FROM song
WHERE album_id = ?",
id
)
.execute(&self.db_pool)
.await;
sqlx::query!(
"DELETE FROM album
WHERE id = ?",
id
)
.execute(&self.db_pool)
.await
}
}
|