diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index e2c918c..3ce316b 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -21,6 +21,14 @@ pub mod request { pub song_queue_id: Option, } } + + pub mod fetch_coverart_with_data { + #[derive(Debug, serde::Deserialize, serde::Serialize)] + pub struct Params { + pub id: Option, + pub song_queue_id: Option, + } + } } pub mod response { @@ -51,6 +59,14 @@ pub mod response { pub data: Vec, } } + + pub mod fetch_coverart_with_data { + #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec>, + } + } } pub mod db { @@ -163,6 +179,60 @@ pub mod db { Err(_) => Err(sqlx::Error::RowNotFound), } } + + pub async fn get_coverart_queue_data_with_id( + pool: &sqlx::PgPool, + id: &uuid::Uuid, + ) -> Result, sqlx::Error> { + let result = sqlx::query( + r#" + SELECT data FROM "coverartQueue" WHERE id = $1; + "#, + ) + .bind(id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error querying data: {:?}", e); + }); + + match result { + Ok(row) => Ok( + row + .try_get("data") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + ), + Err(_) => Err(sqlx::Error::RowNotFound), + } + } + + pub async fn get_coverart_queue_data_with_song_queue_id( + pool: &sqlx::PgPool, + song_queue_id: &uuid::Uuid, + ) -> Result, sqlx::Error> { + let result = sqlx::query( + r#" + SELECT data 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( + row + .try_get("data") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + ), + Err(_) => Err(sqlx::Error::RowNotFound), + } + } } pub mod endpoint { @@ -284,4 +354,47 @@ pub mod endpoint { }, } } + + pub async fn fetch_coverart_with_data( + axum::Extension(pool): axum::Extension, + axum::extract::Query(params): axum::extract::Query< + super::request::fetch_coverart_with_data::Params>, + ) -> (axum::http::StatusCode, axum::Json) { + let mut response = super::response::fetch_coverart_with_data::Response::default(); + + match params.id { + Some(id) => match super::db::get_coverart_queue_data_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_data_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)) + } + }, + } + } } diff --git a/src/callers/mod.rs b/src/callers/mod.rs index de9b756..e089375 100644 --- a/src/callers/mod.rs +++ b/src/callers/mod.rs @@ -8,5 +8,6 @@ pub mod endpoints { pub const NEXTQUEUESONG: &str = "/api/v2/song/queue/next"; pub const QUEUEMETADATA: &str = "/api/v2/song/metadata/queue"; pub const QUEUECOVERART: &str = "/api/v2/coverart/queue"; + pub const QUEUECOVERARTDATA: &str = "/api/v2/coverart/queue/data"; pub const QUEUECOVERARTLINK: &str = "/api/v2/coverart/queue/link"; }