From c622eab39ac7dd9f794b5d60eb937e29c9b3bd6e Mon Sep 17 00:00:00 2001 From: niliara-edu Date: Sun, 26 Jan 2025 19:11:24 +0100 Subject: add api documentation --- src/database/album.rs | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'src/database/album.rs') 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, + #[schema(example = "1")] id: Option, - cover: Option, + #[schema(example = "just ignore this one")] artist_name: Option, + #[schema(example = "1")] artist_id: Option, } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, ToSchema)] pub struct AlbumPost { + #[schema(example = "album name")] name: Option, - cover: Option, + #[schema(example = "just ignore this one")] artist_id: Option, } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, ToSchema)] pub struct AlbumPut { id: Option, + #[schema(example = "album name")] name: Option, - cover: Option, + #[schema(example = "just ignore this one")] artist_id: Option, } @@ -30,7 +36,7 @@ impl DatabaseWrapper { pub async fn select_albums(&self) -> Result, 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, 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, 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) -- cgit v1.2.3