#[derive(Debug, Default, serde::Deserialize, serde::Serialize)] pub struct CoverArtQueue { pub id: uuid::Uuid, pub song_queue_id: uuid::Uuid, } pub mod request { pub mod link { #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] pub struct Request { pub coverart_id: uuid::Uuid, pub song_queue_id: uuid::Uuid, } } pub mod fetch_coverart_no_data { #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] pub struct Params { pub id: Option, pub song_queue_id: Option, } } } pub mod response { #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] pub struct Response { pub message: String, pub data: Vec, } pub mod link { #[derive(Debug, serde::Deserialize, serde::Serialize)] pub struct Id { pub coverart_id: uuid::Uuid, pub song_queue_id: uuid::Uuid, } #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] pub struct Response { pub message: String, pub data: Vec, } } pub mod fetch_coverart_no_data { #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] pub struct Response { pub message: String, pub data: Vec, } } } pub mod db { use sqlx::Row; pub async fn insert(pool: &sqlx::PgPool, data: &Vec) -> Result { let result = sqlx::query( r#" INSERT INTO "coverartQueue" (data) VALUES($1) RETURNING id; "#, ) .bind(data) .fetch_one(pool) .await .map_err(|e| { eprintln!("Error inserting: {}", e); }); match result { Ok(row) => { let id: uuid::Uuid = row .try_get("id") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(); Ok(id) } Err(_err) => Err(sqlx::Error::RowNotFound), } } pub async fn update( pool: &sqlx::PgPool, coverart_id: &uuid::Uuid, song_queue_id: &uuid::Uuid, ) -> Result { let result = sqlx::query( r#" UPDATE "coverartQueue" SET song_queue_id = $1 WHERE id = $2; "#, ) .bind(song_queue_id) .bind(coverart_id) .execute(pool) .await; match result { Ok(_) => Ok(0), Err(_err) => Err(sqlx::Error::RowNotFound), } } pub async fn get_coverart_queue_with_id( pool: &sqlx::PgPool, id: &uuid::Uuid, ) -> Result { let result = sqlx::query( r#" SELECT id, song_queue_id FROM "coverartQueue" WHERE id = $1; "#, ) .bind(id) .fetch_one(pool) .await .map_err(|e| { eprintln!("Error querying data: {:?}", e); }); match result { Ok(row) => Ok(super::CoverArtQueue { id: row .try_get("id") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), song_queue_id: row .try_get("song_queue_id") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), }), Err(_) => Err(sqlx::Error::RowNotFound), } } pub async fn get_coverart_queue_with_song_queue_id( pool: &sqlx::PgPool, song_queue_id: &uuid::Uuid, ) -> Result { let result = sqlx::query( r#" SELECT id, song_queue_id FROM "coverartQueue" WHERE song_queue_id = $1; "#, ) .bind(song_queue_id) .fetch_one(pool) .await .map_err(|e| { eprintln!("Error querying data: {:?}", e); }); match result { Ok(row) => Ok(super::CoverArtQueue { id: row .try_get("id") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), song_queue_id: row .try_get("song_queue_id") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), }), Err(_) => Err(sqlx::Error::RowNotFound), } } } pub mod endpoint { pub async fn queue( axum::Extension(pool): axum::Extension, mut multipart: axum::extract::Multipart, ) -> ( axum::http::StatusCode, axum::Json, ) { let mut response = super::response::Response::default(); match multipart.next_field().await { Ok(Some(field)) => { let name = field.name().unwrap().to_string(); let file_name = field.file_name().unwrap().to_string(); let content_type = field.content_type().unwrap().to_string(); let data = field.bytes().await.unwrap(); let raw_data = data.to_vec(); println!( "Received file '{}' (name = '{}', content-type = '{}', size = {})", file_name, name, content_type, data.len() ); match super::db::insert(&pool, &raw_data).await { Ok(id) => { response.message = String::from("Successful"); response.data.push(id); (axum::http::StatusCode::OK, axum::Json(response)) } Err(err) => { response.message = err.to_string(); (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } } Ok(None) => (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)), Err(err) => { response.message = err.to_string(); (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } } pub async fn link( axum::Extension(pool): axum::Extension, axum::Json(payload): axum::Json, ) -> ( axum::http::StatusCode, axum::Json, ) { let mut response = super::response::link::Response::default(); let id = payload.coverart_id; let song_id = payload.song_queue_id; match super::db::update(&pool, &id, &song_id).await { Ok(_o) => { response.data.push(super::response::link::Id { song_queue_id: song_id, coverart_id: id, }); (axum::http::StatusCode::OK, axum::Json(response)) } Err(err) => { response.message = err.to_string(); (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } } pub async fn fetch_coverart_no_data( axum::Extension(pool): axum::Extension, axum::extract::Query(params): axum::extract::Query< super::request::fetch_coverart_no_data::Params, >, ) -> ( axum::http::StatusCode, axum::Json, ) { let mut response = super::response::fetch_coverart_no_data::Response::default(); match params.id { Some(id) => match super::db::get_coverart_queue_with_id(&pool, &id).await { Ok(cover_art_queue) => { response.message = String::from("Successful"); response.data.push(cover_art_queue); (axum::http::StatusCode::OK, axum::Json(response)) } Err(err) => { response.message = err.to_string(); (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } }, _ => match params.song_queue_id { Some(song_queue_id) => { match super::db::get_coverart_queue_with_song_queue_id(&pool, &song_queue_id) .await { Ok(cover_art_queue) => { response.message = String::from("Successful"); response.data.push(cover_art_queue); (axum::http::StatusCode::OK, axum::Json(response)) } Err(err) => { response.message = err.to_string(); (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } } None => { response.message = String::from("No valid id provided"); (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } }, } } }