From b1f5c5f604568c321702da9dafc2cb97ce66ab43 Mon Sep 17 00:00:00 2001 From: KD Date: Mon, 19 May 2025 21:54:50 -0400 Subject: [PATCH] Link coverart with song queue (#128) * Added initial code to link coverart id and song queue id * Added more functionality * Preparing test * Finished test * Cleanup * Code formatting * Clippy warning fix --- src/callers/coverart.rs | 73 ++++++++++++++++++++++ src/callers/mod.rs | 1 + src/main.rs | 135 ++++++++++++++++++++++++++++++++++------ 3 files changed, 190 insertions(+), 19 deletions(-) diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index 15de43a..b0d760d 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -1,9 +1,34 @@ +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 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, + } + } } mod db { @@ -33,6 +58,27 @@ mod db { 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 mod endpoint { @@ -80,4 +126,31 @@ pub mod endpoint { } } } + + 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)) + } + } + } } diff --git a/src/callers/mod.rs b/src/callers/mod.rs index 55d2907..de9b756 100644 --- a/src/callers/mod.rs +++ b/src/callers/mod.rs @@ -8,4 +8,5 @@ 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 QUEUECOVERARTLINK: &str = "/api/v2/coverart/queue/link"; } diff --git a/src/main.rs b/src/main.rs index d7ff00c..f2a6e14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,7 +58,7 @@ async fn main() { } pub mod init { - use axum::routing::{get, post}; + use axum::routing::{get, patch, post}; use std::time::Duration; use tower_http::timeout::TimeoutLayer; @@ -89,6 +89,10 @@ pub mod init { crate::callers::endpoints::QUEUECOVERART, post(crate::callers::coverart::endpoint::queue), ) + .route( + crate::callers::endpoints::QUEUECOVERARTLINK, + patch(crate::callers::coverart::endpoint::link), + ) } pub async fn app() -> axum::Router { @@ -269,6 +273,27 @@ mod tests { app.clone().oneshot(req).await } + async fn upload_coverart_queue_req( + app: &axum::Router, + ) -> Result { + let mut form = MultipartForm::default(); + let _ = form.add_file("jpg", "tests/Machine_gun/160809_machinegun.jpg"); + + // Create request + let content_type = form.content_type(); + let body = MultipartBody::from(form); + + let req = axum::http::Request::builder() + .method(axum::http::Method::POST) + .uri(crate::callers::endpoints::QUEUECOVERART) + .header(axum::http::header::CONTENT_TYPE, content_type) + .body(axum::body::Body::from_stream(body)) + .unwrap(); + + // Send request + app.clone().oneshot(req).await + } + pub async fn resp_to_bytes( response: axum::response::Response, ) -> Result { @@ -614,26 +639,9 @@ mod tests { db::migrations(&pool).await; let app = init::app(pool).await; - let mut form = MultipartForm::default(); - let _ = form.add_file("jpg", "tests/Machine_gun/160809_machinegun.jpg"); - - // Create request - let content_type = form.content_type(); - let body = MultipartBody::from(form); // Send request - match app - .clone() - .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(crate::callers::endpoints::QUEUECOVERART) - .header(axum::http::header::CONTENT_TYPE, content_type) - .body(axum::body::Body::from_stream(body)) - .unwrap(), - ) - .await - { + match upload_coverart_queue_req(&app).await { Ok(response) => { let resp = get_resp_data::(response).await; @@ -648,4 +656,93 @@ mod tests { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } + + #[tokio::test] + async fn test_song_coverart_queue_link() { + let tm_pool = db_mgr::get_pool().await.unwrap(); + let db_name = db_mgr::generate_db_name().await; + + match db_mgr::create_database(&tm_pool, &db_name).await { + Ok(_) => { + println!("Success"); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + + let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); + db::migrations(&pool).await; + + let app = init::app(pool).await; + + match song_queue_req(&app).await { + Ok(response) => { + let resp = + get_resp_data::(response).await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + let song_queue_id = resp.data[0]; + assert_eq!(false, song_queue_id.is_nil(), "Should not be empty"); + + // Send request + match upload_coverart_queue_req(&app).await { + Ok(response) => { + let resp = + get_resp_data::(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + let coverart_id = resp.data[0]; + assert_eq!(false, coverart_id.is_nil(), "Should not be empty"); + + let payload = serde_json::json!( + { + "song_queue_id": song_queue_id, + "coverart_id" : coverart_id, + }); + + match app + .clone() + .oneshot( + axum::http::Request::builder() + .method(axum::http::Method::PATCH) + .uri(crate::callers::endpoints::QUEUECOVERARTLINK) + .header(axum::http::header::CONTENT_TYPE, "application/json") + .body(axum::body::Body::from(payload.to_string())) + .unwrap(), + ) + .await + { + Ok(response) => { + let resp = get_resp_data::< + crate::callers::coverart::response::link::Response, + >(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + let resp_coverart_id = resp.data[0].coverart_id; + let resp_song_queue_id = resp.data[0].song_queue_id; + + assert_eq!(false, resp_coverart_id.is_nil(), "Should not be empty"); + assert_eq!( + false, + resp_song_queue_id.is_nil(), + "Should not be empty" + ); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + + let _ = db_mgr::drop_database(&tm_pool, &db_name).await; + } }