diff options
Diffstat (limited to 'src/database/album.rs')
-rw-r--r-- | src/database/album.rs | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/src/database/album.rs b/src/database/album.rs index d7ffae0..6b2b46d 100644 --- a/src/database/album.rs +++ b/src/database/album.rs @@ -1,28 +1,34 @@ use crate::database::DatabaseWrapper; use serde::{Deserialize, Serialize}; use sqlx::mysql::MySqlQueryResult; +use utoipa::ToSchema; -#[derive(Serialize)] +#[derive(Serialize, ToSchema)] pub struct Album { + #[schema(example = "album name")] name: Option<String>, + #[schema(example = "1")] id: Option<i32>, - cover: Option<String>, + #[schema(example = "just ignore this one")] artist_name: Option<String>, + #[schema(example = "1")] artist_id: Option<i32>, } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, ToSchema)] pub struct AlbumPost { + #[schema(example = "album name")] name: Option<String>, - cover: Option<String>, + #[schema(example = "just ignore this one")] artist_id: Option<String>, } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, ToSchema)] pub struct AlbumPut { id: Option<String>, + #[schema(example = "album name")] name: Option<String>, - cover: Option<String>, + #[schema(example = "just ignore this one")] artist_id: Option<String>, } @@ -30,7 +36,7 @@ impl DatabaseWrapper { pub async fn select_albums(&self) -> Result<Vec<Album>, sqlx::Error> { sqlx::query_as!( Album, - "SELECT album.name, album.id, album.cover, + "SELECT album.name, album.id, artist.name as artist_name, artist.id as artist_id FROM album INNER JOIN artist ON album.artist_id = artist.id @@ -43,7 +49,7 @@ impl DatabaseWrapper { 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, + "SELECT album.name, album.id, artist.name as artist_name, artist.id as artist_id FROM album INNER JOIN artist ON album.artist_id = artist.id @@ -58,7 +64,7 @@ impl DatabaseWrapper { let name: String = format!("{}{}{}", "%", name_raw, "%"); sqlx::query_as!( Album, - "SELECT album.name, album.id, album.cover, + "SELECT album.name, album.id, artist.name as artist_name, artist.id as artist_id FROM album INNER JOIN artist ON album.artist_id = artist.id @@ -76,7 +82,7 @@ impl DatabaseWrapper { ) -> Result<Vec<Album>, sqlx::Error> { sqlx::query_as!( Album, - "SELECT album.name, album.id, album.cover, + "SELECT album.name, album.id, artist.name as artist_name, artist.id as artist_id FROM album INNER JOIN artist ON album.artist_id = artist.id @@ -103,10 +109,9 @@ impl DatabaseWrapper { } sqlx::query!( - "INSERT INTO album (name, cover, artist_id) - VALUE (?, ?, ?)", + "INSERT INTO album (name, artist_id) + VALUE (?, ?)", data.name, - data.cover.unwrap_or(String::default()), data.artist_id, ) .execute(&self.db_pool) @@ -125,11 +130,9 @@ impl DatabaseWrapper { Err(_) => return Err(sqlx::Error::RowNotFound), }; sqlx::query!( - "UPDATE album SET name=?, cover=? WHERE id=?", + "UPDATE album SET name=? 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) |