From 499ea4312f30312236e03046c90f8a2372bff471 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 25 May 2025 19:20:33 -0400 Subject: [PATCH 1/8] Added TODOs for later --- src/callers/coverart.rs | 1 + src/callers/metadata.rs | 1 + src/callers/song.rs | 1 + 3 files changed, 3 insertions(+) diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index 03f9102..988d0ce 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -1,3 +1,4 @@ +// TODO: Separate queue and coverart endpoints #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] pub struct CoverArtQueue { pub id: uuid::Uuid, diff --git a/src/callers/metadata.rs b/src/callers/metadata.rs index 253023f..3f8c8e9 100644 --- a/src/callers/metadata.rs +++ b/src/callers/metadata.rs @@ -1,3 +1,4 @@ +// TODO: Explicitly make this module target queueing a song's metadata pub mod request { use serde::{Deserialize, Serialize}; diff --git a/src/callers/song.rs b/src/callers/song.rs index 93313f8..fdf78e0 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -1,3 +1,4 @@ +// TODO: Separate queue and song endpoints pub mod request { use serde::{Deserialize, Serialize}; -- 2.47.3 From d49f8c033101dc279da0550cb0095b8812fce143 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 25 May 2025 19:47:05 -0400 Subject: [PATCH 2/8] Added endpoint to wipe data from song queue --- src/callers/song.rs | 97 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/src/callers/song.rs b/src/callers/song.rs index fdf78e0..ee52b08 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -77,6 +77,13 @@ pub mod request { } } } + + pub mod wipe_data_from_song_queue { + #[derive(Debug, serde::Deserialize, serde::Serialize)] + pub struct Request { + pub song_queue_id: uuid::Uuid + } + } } pub mod response { @@ -127,6 +134,14 @@ pub mod response { pub data: Vec, } } + + pub mod wipe_data_from_song_queue { + #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec + } + } } // TODO: Might make a distinction between year and date in a song's tag at some point @@ -459,6 +474,59 @@ mod song_queue { } } + pub async fn get_song_queue(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result { + let result = sqlx::query( + r#" + SELECT id, filename, status FROM "songQueue" WHERE id = $1 + "# + ) + .bind(&id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error querying data: {:?}", e); + }); + + match result { + Ok(row) => Ok(SongQueue { + id: row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + filename: row + .try_get("filename") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + status: row + .try_get("status") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + }), + Err(_err) => Err(sqlx::Error::RowNotFound), + } + } + + pub async fn wipe_data(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result { + let result = sqlx::query( + r#" + UPDATE "songQueue" SET data = NULL WHERE id = $1 RETURNING id; + "# + ) + .bind(&id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error updating record: {:?}", e); + }); + + match result { + Ok(row) => { + Ok(row.try_get("id").map_err(|_e| sqlx::Error::RowNotFound).unwrap()) + } + Err(_) => Err(sqlx::Error::RowNotFound) + } + } + pub async fn get_data(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result, sqlx::Error> { let result = sqlx::query( r#" @@ -736,4 +804,33 @@ pub mod endpoint { (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } + + pub async fn wipe_data_from_song_queue( + axum::Extension(pool): axum::Extension, + axum::Json(payload): axum::Json, + ) -> (axum::http::StatusCode, axum::Json,) { + let mut response = super::response::wipe_data_from_song_queue::Response::default(); + let id = payload.song_queue_id; + + match super::song_queue::get_song_queue(&pool, &id).await { + Ok(song_queue) => { + match super::song_queue::wipe_data(&pool, &song_queue.id).await { + Ok(wiped_id) => { + response.message = "Success"; + response.data.push(wiped_id); + + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::NOT_FOUND, axum::Json(response)) + } + } + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::NOT_FOUND, axum::Json(response)) + } + } + } } -- 2.47.3 From 2a9d2b08b8d12e9c6903324b0eb60f5437e4a333 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 25 May 2025 19:47:14 -0400 Subject: [PATCH 3/8] Migration changes --- migrations/20250420185217_init_migration.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrations/20250420185217_init_migration.sql b/migrations/20250420185217_init_migration.sql index 6415f2a..aafe6e0 100644 --- a/migrations/20250420185217_init_migration.sql +++ b/migrations/20250420185217_init_migration.sql @@ -6,7 +6,7 @@ CREATE TABLE IF NOT EXISTS "songQueue" ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), filename TEXT NOT NULL, status TEXT CHECK (status IN ('pending', 'processing', 'done')), - data BYTEA NOT NULL + data BYTEA NULL ); -- Table to store queued metadata -- 2.47.3 From 145247c9ffd656dd9c8ffd0d44d351f3ee13b9e4 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 25 May 2025 19:51:07 -0400 Subject: [PATCH 4/8] Syntax error fix --- src/callers/song.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/callers/song.rs b/src/callers/song.rs index ee52b08..580f78b 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -816,7 +816,7 @@ pub mod endpoint { Ok(song_queue) => { match super::song_queue::wipe_data(&pool, &song_queue.id).await { Ok(wiped_id) => { - response.message = "Success"; + response.message = String::from("Success"); response.data.push(wiped_id); (axum::http::StatusCode::OK, axum::Json(response)) -- 2.47.3 From c8b5db48b598a5bef93e192daf4b58b3eee1ce11 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 25 May 2025 19:51:34 -0400 Subject: [PATCH 5/8] Added and linked endpoint --- src/callers/mod.rs | 1 + src/main.rs | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/callers/mod.rs b/src/callers/mod.rs index 5427afe..d5e85a5 100644 --- a/src/callers/mod.rs +++ b/src/callers/mod.rs @@ -11,6 +11,7 @@ pub mod endpoints { 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"; + pub const QUEUESONGDATAWIPE: &str = "/api/v2/song/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 bc1e04b..dc4ce22 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,6 +73,10 @@ pub mod init { crate::callers::endpoints::QUEUESONGUPDATE, patch(crate::callers::song::endpoint::update_song_queue), ) + .route( + crate::callers::endpoints::QUEUESONGDATAWIPE, + patch(crate::callers::song::endpoint::wipe_data_from_song_queue) + ) .route( crate::callers::endpoints::QUEUEMETADATA, post(crate::callers::metadata::endpoint::queue_metadata), -- 2.47.3 From 034cc49d97bc8c7df957096142899946e221c42c Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 25 May 2025 20:01:35 -0400 Subject: [PATCH 6/8] Added test --- src/main.rs | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/src/main.rs b/src/main.rs index dc4ce22..be6b14f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1520,4 +1520,120 @@ mod tests { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } + + #[tokio::test] + async fn test_wipe_data_from_song_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"); + + 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 + ); + + let payload = serde_json::json!({ + "song_queue_id": song_q_id + }); + + match app.clone().oneshot( + axum::http::Request::builder() + .method(axum::http::Method::PATCH) + .uri(crate::callers::endpoints::QUEUESONGDATAWIPE) + .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::(response).await; + assert_eq!( + false, + resp.data.is_empty(), + "Failure in wiping data from song queue {:?}", + resp + ); + + let returned_id = &resp.data[0]; + assert_eq!(false, returned_id.is_nil(), "Returned id should not be nil {:?}", returned_id); + assert_eq!(*returned_id, song_q_id, "Returned id does not match sent id {:?} {:?}", returned_id, song_q_id); + } + 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; + } } -- 2.47.3 From 77ed332fef22765a6384e6afd857b89d28472da8 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 25 May 2025 20:01:47 -0400 Subject: [PATCH 7/8] Warning fixes --- src/callers/song.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/callers/song.rs b/src/callers/song.rs index 580f78b..fdd8ef0 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -480,7 +480,7 @@ mod song_queue { SELECT id, filename, status FROM "songQueue" WHERE id = $1 "# ) - .bind(&id) + .bind(id) .fetch_one(pool) .await .map_err(|e| { @@ -512,7 +512,7 @@ mod song_queue { UPDATE "songQueue" SET data = NULL WHERE id = $1 RETURNING id; "# ) - .bind(&id) + .bind(id) .fetch_one(pool) .await .map_err(|e| { -- 2.47.3 From d32a11b1787bda9adff2c5f8db935a48c42590a8 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 25 May 2025 20:02:10 -0400 Subject: [PATCH 8/8] Code formatting --- src/callers/song.rs | 82 +++++++++++++++++++++++++-------------------- src/main.rs | 4 +-- 2 files changed, 47 insertions(+), 39 deletions(-) diff --git a/src/callers/song.rs b/src/callers/song.rs index fdd8ef0..690ffab 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -81,7 +81,7 @@ pub mod request { pub mod wipe_data_from_song_queue { #[derive(Debug, serde::Deserialize, serde::Serialize)] pub struct Request { - pub song_queue_id: uuid::Uuid + pub song_queue_id: uuid::Uuid, } } } @@ -139,7 +139,7 @@ pub mod response { #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] pub struct Response { pub message: String, - pub data: Vec + pub data: Vec, } } } @@ -474,18 +474,21 @@ mod song_queue { } } - pub async fn get_song_queue(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result { + pub async fn get_song_queue( + pool: &sqlx::PgPool, + id: &uuid::Uuid, + ) -> Result { let result = sqlx::query( r#" SELECT id, filename, status FROM "songQueue" WHERE id = $1 - "# - ) - .bind(id) - .fetch_one(pool) - .await - .map_err(|e| { - eprintln!("Error querying data: {:?}", e); - }); + "#, + ) + .bind(id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error querying data: {:?}", e); + }); match result { Ok(row) => Ok(SongQueue { @@ -506,24 +509,28 @@ mod song_queue { } } - pub async fn wipe_data(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result { + pub async fn wipe_data( + pool: &sqlx::PgPool, + id: &uuid::Uuid, + ) -> Result { let result = sqlx::query( r#" UPDATE "songQueue" SET data = NULL WHERE id = $1 RETURNING id; - "# - ) - .bind(id) - .fetch_one(pool) - .await - .map_err(|e| { - eprintln!("Error updating record: {:?}", e); - }); + "#, + ) + .bind(id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error updating record: {:?}", e); + }); match result { - Ok(row) => { - Ok(row.try_get("id").map_err(|_e| sqlx::Error::RowNotFound).unwrap()) - } - Err(_) => Err(sqlx::Error::RowNotFound) + Ok(row) => Ok(row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap()), + Err(_) => Err(sqlx::Error::RowNotFound), } } @@ -808,25 +815,26 @@ pub mod endpoint { pub async fn wipe_data_from_song_queue( axum::Extension(pool): axum::Extension, axum::Json(payload): axum::Json, - ) -> (axum::http::StatusCode, axum::Json,) { + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { let mut response = super::response::wipe_data_from_song_queue::Response::default(); let id = payload.song_queue_id; match super::song_queue::get_song_queue(&pool, &id).await { - Ok(song_queue) => { - match super::song_queue::wipe_data(&pool, &song_queue.id).await { - Ok(wiped_id) => { - response.message = String::from("Success"); - response.data.push(wiped_id); + Ok(song_queue) => match super::song_queue::wipe_data(&pool, &song_queue.id).await { + Ok(wiped_id) => { + response.message = String::from("Success"); + response.data.push(wiped_id); - (axum::http::StatusCode::OK, axum::Json(response)) - } - Err(err) => { - response.message = err.to_string(); - (axum::http::StatusCode::NOT_FOUND, axum::Json(response)) - } + (axum::http::StatusCode::OK, axum::Json(response)) } - } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::NOT_FOUND, axum::Json(response)) + } + }, Err(err) => { response.message = err.to_string(); (axum::http::StatusCode::NOT_FOUND, axum::Json(response)) diff --git a/src/main.rs b/src/main.rs index be6b14f..785abb3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,8 +75,8 @@ pub mod init { ) .route( crate::callers::endpoints::QUEUESONGDATAWIPE, - patch(crate::callers::song::endpoint::wipe_data_from_song_queue) - ) + patch(crate::callers::song::endpoint::wipe_data_from_song_queue), + ) .route( crate::callers::endpoints::QUEUEMETADATA, post(crate::callers::metadata::endpoint::queue_metadata), -- 2.47.3