From e00e9cf18556643c8208b86c807578d07cd8849a Mon Sep 17 00:00:00 2001 From: KD Date: Mon, 26 May 2025 17:12:11 -0400 Subject: [PATCH] Wipe data from coverart queue (#136) * Changes to coverartQueue migrations * Added endpoint to wipe data from coverart queue * Added constant for endpoint * Endpoint is now available * Added test Need to complete * Test is working * Code formatting * Removed TODO --- migrations/20250420185217_init_migration.sql | 2 +- src/callers/coverart.rs | 69 +++++ src/callers/mod.rs | 1 + src/main.rs | 278 ++++++++++++++++++- 4 files changed, 335 insertions(+), 15 deletions(-) diff --git a/migrations/20250420185217_init_migration.sql b/migrations/20250420185217_init_migration.sql index aafe6e0..e5a6d6a 100644 --- a/migrations/20250420185217_init_migration.sql +++ b/migrations/20250420185217_init_migration.sql @@ -20,7 +20,7 @@ CREATE TABLE IF NOT EXISTS "metadataQueue" ( -- Table to store queued coverart CREATE TABLE IF NOT EXISTS "coverartQueue" ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), - data BYTEA NOT NULL, + data BYTEA NULL, song_queue_id UUID NULL ); diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index 988d0ce..6336d4d 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -38,6 +38,13 @@ pub mod request { pub coverart_queue_id: uuid::Uuid, } } + + pub mod wipe_data_from_coverart_queue { + #[derive(Debug, serde::Deserialize, serde::Serialize)] + pub struct Request { + pub coverart_queue_id: uuid::Uuid, + } + } } pub mod response { @@ -84,6 +91,14 @@ pub mod response { pub data: Vec, } } + + pub mod wipe_data_from_coverart_queue { + #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } } pub mod db { @@ -246,6 +261,31 @@ pub mod db { Err(_) => Err(sqlx::Error::RowNotFound), } } + + pub async fn wipe_data( + pool: &sqlx::PgPool, + coverart_queue_id: &uuid::Uuid, + ) -> Result { + let result = sqlx::query( + r#" + UPDATE "coverartQueue" SET data = NULL WHERE id = $1 RETURNING id; + "#, + ) + .bind(coverart_queue_id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error updating query: {:?}", e); + }); + + match result { + Ok(row) => Ok(row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap()), + Err(_) => Err(sqlx::Error::RowNotFound), + } + } } pub mod cov_db { @@ -508,4 +548,33 @@ pub mod endpoint { } } } + + pub async fn wipe_data_from_coverart_queue( + axum::Extension(pool): axum::Extension, + axum::Json(payload): axum::Json, + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { + let mut response = super::response::wipe_data_from_coverart_queue::Response::default(); + let coverart_queue_id = payload.coverart_queue_id; + + match super::db::get_coverart_queue_with_id(&pool, &coverart_queue_id).await { + Ok(coverart_queue) => match super::db::wipe_data(&pool, &coverart_queue.id).await { + Ok(id) => { + response.message = String::from("Success"); + 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)) + } + }, + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::NOT_FOUND, axum::Json(response)) + } + } + } } diff --git a/src/callers/mod.rs b/src/callers/mod.rs index d5e85a5..e15f55d 100644 --- a/src/callers/mod.rs +++ b/src/callers/mod.rs @@ -12,6 +12,7 @@ pub mod endpoints { pub const QUEUECOVERARTDATA: &str = "/api/v2/coverart/queue/data"; pub const QUEUECOVERARTLINK: &str = "/api/v2/coverart/queue/link"; pub const QUEUESONGDATAWIPE: &str = "/api/v2/song/queue/data/wipe"; + pub const QUEUECOVERARTDATAWIPE: &str = "/api/v2/coverart/queue/data/wipe"; pub const CREATESONG: &str = "/api/v2/song"; pub const CREATECOVERART: &str = "/api/v2/coverart"; diff --git a/src/main.rs b/src/main.rs index 785abb3..6c87a1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -101,6 +101,10 @@ pub mod init { crate::callers::endpoints::QUEUECOVERARTLINK, patch(crate::callers::coverart::endpoint::link), ) + .route( + crate::callers::endpoints::QUEUECOVERARTDATAWIPE, + patch(crate::callers::coverart::endpoint::wipe_data_from_coverart_queue), + ) .route( crate::callers::endpoints::CREATESONG, post(crate::callers::song::endpoint::create_metadata), @@ -368,6 +372,24 @@ mod tests { app.clone().oneshot(req).await } + async fn create_coverart_req( + app: &axum::Router, + song_id: &uuid::Uuid, + coverart_id: &uuid::Uuid, + ) -> Result { + let payload = serde_json::json!({ + "song_id": song_id, + "coverart_queue_id": coverart_id + }); + let req = axum::http::Request::builder() + .method(axum::http::Method::POST) + .uri(crate::callers::endpoints::CREATECOVERART) + .header(axum::http::header::CONTENT_TYPE, "application/json") + .body(axum::body::Body::from(payload.to_string())) + .unwrap(); + app.clone().oneshot(req).await + } + async fn create_song_req( app: &axum::Router, song_queue_id: &uuid::Uuid, @@ -1454,19 +1476,13 @@ mod tests { "Should not be empty" ); - let payload = serde_json::json!({ - "song_id": song_id, - "coverart_queue_id": resp_coverart_id, - }); - - match app.clone().oneshot( - axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(crate::callers::endpoints::CREATECOVERART) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .body(axum::body::Body::from(payload.to_string())) - .unwrap() - ).await { + match create_coverart_req( + &app, + &song_id, + &resp_coverart_id, + ) + .await + { Ok(response) => { let resp = get_resp_data::< crate::callers::coverart::response::create_coverart::Response, @@ -1479,7 +1495,11 @@ mod tests { ); } Err(err) => { - assert!(false, "Error: {:?}", err); + assert!( + false, + "Error: {:?}", + err + ); } } } @@ -1636,4 +1656,234 @@ mod tests { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } + + #[tokio::test] + async fn test_wipe_data_from_coverart_queue() { + 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; + + // Send request + 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"); + assert_eq!(false, resp.data[0].is_nil(), "Should not be empty"); + let song_queue_id = resp.data[0]; + assert_eq!(false, song_queue_id.is_nil(), "Should not be empty"); + + match queue_metadata_req(&app, &resp.data[0]).await { + Ok(response) => { + let resp = + get_resp_data::(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + + let id = resp.data[0]; + + match fetch_metadata_queue_req(&app, &id).await { + Ok(response) => { + let resp = get_resp_data::< + crate::callers::metadata::response::fetch_metadata::Response, + >(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Data should not be empty"); + let song_q_id = resp.data[0].song_queue_id; + + match create_song_req(&app, &song_q_id).await { + Ok(response) => { + let resp = get_resp_data::(response).await; + assert_eq!( + false, + resp.data.is_empty(), + "No songs found, Response {:?}", + resp + ); + let song = &resp.data[0]; + let song_id = song.id; + assert_eq!( + false, + song_id.is_nil(), + "Song id should not be nil {:?}", + song + ); + + eprintln!("Song: {:?}", song); + + match upload_coverart_queue_req(&app).await { + Ok(response) => { + let resp = get_resp_data::< + crate::callers::coverart::response::Response, + >( + 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" + ); + + match coverart_queue_song_queue_link_req( + &app, + &coverart_id, + &song_queue_id, + ) + .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" + ); + + match get_queued_coverart( + &app, + &resp_coverart_id, + ) + .await + { + Ok(response) => { + let resp = get_resp_data::< + crate::callers::coverart::response::fetch_coverart_no_data::Response, + >(response) + .await; + assert_eq!( + false, + resp.data.is_empty(), + "Should not be empty" + ); + + match create_coverart_req( + &app, + &song_id, + &resp_coverart_id, + ) + .await + { + Ok(response) => { + let resp = get_resp_data::< + crate::callers::coverart::response::create_coverart::Response, + >(response) + .await; + assert_eq!( + false, + resp.data.is_empty(), + "Should not be empty" + ); + + let payload = serde_json::json!({ + "coverart_queue_id": resp_coverart_id + }); + + match app.clone().oneshot( + axum::http::Request::builder() + .method(axum::http::Method::PATCH) + .uri(crate::callers::endpoints::QUEUECOVERARTDATAWIPE) + .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::wipe_data_from_coverart_queue::Response, + >(response) + .await; + assert_eq!( + false, + resp.data.is_empty(), + "Should not be empty" + ); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!( + false, + "Error: {:?}", + err + ); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + 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; + } }