From b8c0585f7bb7aa32e995b7bb6cd91f5da3e30238 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 22 May 2025 15:15:43 -0400 Subject: [PATCH 1/9] Added endpoint - no code - for updating the song from the queue --- src/callers/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/callers/mod.rs b/src/callers/mod.rs index e089375..5b3beaf 100644 --- a/src/callers/mod.rs +++ b/src/callers/mod.rs @@ -5,6 +5,7 @@ pub mod song; pub mod endpoints { pub const QUEUESONG: &str = "/api/v2/song/queue"; pub const QUEUESONGDATA: &str = "/api/v2/song/queue/{id}"; + pub const QUEUESONGUPDATE: &str = "/api/v2/song/queue/:id"; 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"; -- 2.47.3 From 0ce0c549d6fb8e1944bc6212f5fcbdc27cef1dde Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 22 May 2025 15:41:56 -0400 Subject: [PATCH 2/9] Added endpoint to update song from queue --- src/callers/song.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/src/callers/song.rs b/src/callers/song.rs index b87834b..d29cf3f 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -47,6 +47,14 @@ pub mod response { pub data: Vec, } } + + pub mod update_song_queue { + #[derive(Default, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec>, + } + } } pub mod status { @@ -106,6 +114,32 @@ mod song_queue { } } + pub async fn update( + pool: &sqlx::PgPool, + data: &Vec, + id: &uuid::Uuid, + ) -> Result, sqlx::Error> { + let result = sqlx::query( + r#" + UPDATE "songQueue" SET data = $1 WHERE id = $2 RETURNING data; + "# + ) + .bind(data) + .bind(id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error inserting: {:?}", 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_most_recent_and_update(pool: &sqlx::PgPool) -> Result { let result = sqlx::query( r#" @@ -380,4 +414,51 @@ pub mod endpoint { (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } + + pub async fn update_song_queue( + axum::extract::Path(id): axum::extract::Path, + axum::Extension(pool): axum::Extension, + mut multipart: axum::extract::Multipart, + ) -> (axum::http::StatusCode, axum::Json) { + + let mut response = super::response::update_song_queue::Response::default(); + + if let Some(field) = multipart.next_field().await.unwrap() { + 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(); + + println!( + "Received file '{}' (name = '{}', content-type = '{}', size = {})", + file_name, + name, + content_type, + data.len() + ); + + // Save the file to disk + // let mut file = std::fs::File::create(&file_name).unwrap(); + // file.write_all(&data).unwrap(); + + let raw_data: Vec = data.to_vec(); + match song_queue::update( + &pool, + &raw_data, + &id, + ).await { + Ok(queued_data) => { + response.data.push(queued_data); + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + } + } else { + response.message = String::from("No data provided"); + (axum::http::StatusCode::NOT_FOUND, axum::Json(response)) + } + } } -- 2.47.3 From 5b70003f546c61cae627357640aec4925c4af79e Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 22 May 2025 15:43:54 -0400 Subject: [PATCH 3/9] Endpoint is now available --- src/main.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.rs b/src/main.rs index a155648..291f4aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,6 +81,10 @@ pub mod init { crate::callers::endpoints::NEXTQUEUESONG, get(crate::callers::song::endpoint::fetch_queue_song), ) + .route( + crate::callers::endpoints::QUEUESONGUPDATE, + patch(crate::callers::song::endpoint::update_song_queue) + ) .route( crate::callers::endpoints::QUEUEMETADATA, post(crate::callers::metadata::endpoint::queue_metadata), -- 2.47.3 From 4222ebc52d9fbe33b7003a4431ae284cb7542e66 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 22 May 2025 15:53:46 -0400 Subject: [PATCH 4/9] Fixed endoint uri --- src/callers/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/callers/mod.rs b/src/callers/mod.rs index 5b3beaf..1dbc6c4 100644 --- a/src/callers/mod.rs +++ b/src/callers/mod.rs @@ -5,7 +5,7 @@ pub mod song; pub mod endpoints { pub const QUEUESONG: &str = "/api/v2/song/queue"; pub const QUEUESONGDATA: &str = "/api/v2/song/queue/{id}"; - pub const QUEUESONGUPDATE: &str = "/api/v2/song/queue/:id"; + pub const QUEUESONGUPDATE: &str = "/api/v2/song/queue/{id}"; 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"; -- 2.47.3 From e184efd2607ee03d1e8088bea13850e0c94ae212 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 22 May 2025 16:06:29 -0400 Subject: [PATCH 5/9] Added tempfile crate for tests --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index b891c3d..035b860 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,3 +25,4 @@ icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", ta [dev-dependencies] common-multipart-rfc7578 = { version = "0.7.0" } url = { version = "2.5.4" } +tempfile = { version = "3.19.1" } -- 2.47.3 From d984cee163b3bf795aa2f00515f46a5cd1a66d89 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 22 May 2025 16:09:21 -0400 Subject: [PATCH 6/9] Test is working --- src/main.rs | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 291f4aa..8e9a2bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -146,10 +146,12 @@ pub async fn root() -> &'static str { mod tests { use crate::db; + use std::io::Write; + use std::usize; + use common_multipart_rfc7578::client::multipart::{ Body as MultipartBody, Form as MultipartForm, }; - use std::usize; use tower::ServiceExt; mod db_mgr { @@ -1074,4 +1076,115 @@ mod tests { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } + + #[tokio::test] + async fn test_update_song_from_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 fetch_queue_req(&app).await { + Ok(response) => { + let resp = get_resp_data::< + crate::callers::song::response::fetch_queue_song::Response, + >(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + let id = resp.data[0].id; + + match fetch_queue_data_req(&app, &id).await { + Ok(response) => match resp_to_bytes(response).await { + Ok(bytes) => { + assert_eq!( + false, + bytes.is_empty(), + "Queued data should not be empty" + ); + + // let new_file = String::from("tests/Machine_gun/new_file.flac"); + + let temp_file = tempfile::tempdir().expect("Could not create test directory"); + let test_dir = String::from(temp_file.path().to_str().unwrap()); + let new_file = format!("{}/new_file.flac", test_dir); + + let mut file = std::fs::File::create(&new_file).unwrap(); + file.write_all(&bytes).unwrap(); + + let mut form = MultipartForm::default(); + let _ = form.add_file("flac", new_file); + + // Create request + let content_type = form.content_type(); + let body = MultipartBody::from(form); + + let raw_uri = String::from(crate::callers::endpoints::QUEUESONGUPDATE); + let end_index = raw_uri.len() - 5; + // let mut uri: String = (&raw_uri[..end_index]).to_string(); + // uri += &id.to_string(); + + let uri = format!("{}/{}", (&raw_uri[..end_index]).to_string(), id.to_string()); + + // app.clone().oneshot(req).await + match app.clone().oneshot( + axum::http::Request::builder() + .method(axum::http::Method::PATCH) + .uri(uri) + .header(axum::http::header::CONTENT_TYPE, content_type) + .body(axum::body::Body::from_stream(body)) + .unwrap() + ).await { + Ok(_) => { + let resp = get_resp_data::< + crate::callers::song::response::fetch_queue_song::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); + } + }; + + let _ = db_mgr::drop_database(&tm_pool, &db_name).await; + } } -- 2.47.3 From ca85a5d46dce29cb5350c0c165b283214c651c3b Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 22 May 2025 16:12:48 -0400 Subject: [PATCH 7/9] Test is operational --- src/main.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8e9a2bc..8d01730 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1154,12 +1154,12 @@ mod tests { .body(axum::body::Body::from_stream(body)) .unwrap() ).await { - Ok(_) => { - let resp = get_resp_data::< - crate::callers::song::response::fetch_queue_song::Response, - >(response) - .await; - assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + Ok(response) => { + let resp = get_resp_data::< + crate::callers::song::response::update_song_queue::Response, + >(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); } Err(err) => { assert!(false, "Error: {:?}", err); -- 2.47.3 From b93db64c208d12d350d66999106575a2c51a5eba Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 22 May 2025 16:13:06 -0400 Subject: [PATCH 8/9] Code formatting --- src/callers/song.rs | 41 ++++++++++++++++++++--------------------- src/main.rs | 43 ++++++++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 34 deletions(-) diff --git a/src/callers/song.rs b/src/callers/song.rs index d29cf3f..a638274 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -118,25 +118,26 @@ mod song_queue { pool: &sqlx::PgPool, data: &Vec, id: &uuid::Uuid, - ) -> Result, sqlx::Error> { + ) -> Result, sqlx::Error> { let result = sqlx::query( r#" UPDATE "songQueue" SET data = $1 WHERE id = $2 RETURNING data; - "# - ) - .bind(data) - .bind(id) - .fetch_one(pool) - .await - .map_err(|e| { - eprintln!("Error inserting: {:?}", e); - }); + "#, + ) + .bind(data) + .bind(id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error inserting: {:?}", e); + }); match result { - Ok(row) => { - Ok(row.try_get("data").map_err(|_e| sqlx::Error::RowNotFound).unwrap()) - } - Err(_) => Err(sqlx::Error::RowNotFound) + Ok(row) => Ok(row + .try_get("data") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap()), + Err(_) => Err(sqlx::Error::RowNotFound), } } @@ -419,8 +420,10 @@ pub mod endpoint { axum::extract::Path(id): axum::extract::Path, axum::Extension(pool): axum::Extension, mut multipart: axum::extract::Multipart, - ) -> (axum::http::StatusCode, axum::Json) { - + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { let mut response = super::response::update_song_queue::Response::default(); if let Some(field) = multipart.next_field().await.unwrap() { @@ -442,11 +445,7 @@ pub mod endpoint { // file.write_all(&data).unwrap(); let raw_data: Vec = data.to_vec(); - match song_queue::update( - &pool, - &raw_data, - &id, - ).await { + match song_queue::update(&pool, &raw_data, &id).await { Ok(queued_data) => { response.data.push(queued_data); (axum::http::StatusCode::OK, axum::Json(response)) diff --git a/src/main.rs b/src/main.rs index 8d01730..c2994cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -83,7 +83,7 @@ pub mod init { ) .route( crate::callers::endpoints::QUEUESONGUPDATE, - patch(crate::callers::song::endpoint::update_song_queue) + patch(crate::callers::song::endpoint::update_song_queue), ) .route( crate::callers::endpoints::QUEUEMETADATA, @@ -1124,7 +1124,8 @@ mod tests { // let new_file = String::from("tests/Machine_gun/new_file.flac"); - let temp_file = tempfile::tempdir().expect("Could not create test directory"); + let temp_file = tempfile::tempdir() + .expect("Could not create test directory"); let test_dir = String::from(temp_file.path().to_str().unwrap()); let new_file = format!("{}/new_file.flac", test_dir); @@ -1138,28 +1139,44 @@ mod tests { let content_type = form.content_type(); let body = MultipartBody::from(form); - let raw_uri = String::from(crate::callers::endpoints::QUEUESONGUPDATE); + let raw_uri = + String::from(crate::callers::endpoints::QUEUESONGUPDATE); let end_index = raw_uri.len() - 5; // let mut uri: String = (&raw_uri[..end_index]).to_string(); // uri += &id.to_string(); - let uri = format!("{}/{}", (&raw_uri[..end_index]).to_string(), id.to_string()); + let uri = format!( + "{}/{}", + (&raw_uri[..end_index]).to_string(), + id.to_string() + ); // app.clone().oneshot(req).await - match app.clone().oneshot( - axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(uri) - .header(axum::http::header::CONTENT_TYPE, content_type) - .body(axum::body::Body::from_stream(body)) - .unwrap() - ).await { + match app + .clone() + .oneshot( + axum::http::Request::builder() + .method(axum::http::Method::PATCH) + .uri(uri) + .header( + axum::http::header::CONTENT_TYPE, + content_type, + ) + .body(axum::body::Body::from_stream(body)) + .unwrap(), + ) + .await + { Ok(response) => { let resp = get_resp_data::< crate::callers::song::response::update_song_queue::Response, >(response) .await; - assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + assert_eq!( + false, + resp.data.is_empty(), + "Should not be empty" + ); } Err(err) => { assert!(false, "Error: {:?}", err); -- 2.47.3 From 533614d07d8f1fbc13d15597cb59c101a7b52f05 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 22 May 2025 16:13:45 -0400 Subject: [PATCH 9/9] Code cleanup --- src/callers/song.rs | 4 ---- src/main.rs | 5 ----- 2 files changed, 9 deletions(-) diff --git a/src/callers/song.rs b/src/callers/song.rs index a638274..b4ddeef 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -440,10 +440,6 @@ pub mod endpoint { data.len() ); - // Save the file to disk - // let mut file = std::fs::File::create(&file_name).unwrap(); - // file.write_all(&data).unwrap(); - let raw_data: Vec = data.to_vec(); match song_queue::update(&pool, &raw_data, &id).await { Ok(queued_data) => { diff --git a/src/main.rs b/src/main.rs index c2994cb..038c9fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1122,8 +1122,6 @@ mod tests { "Queued data should not be empty" ); - // let new_file = String::from("tests/Machine_gun/new_file.flac"); - let temp_file = tempfile::tempdir() .expect("Could not create test directory"); let test_dir = String::from(temp_file.path().to_str().unwrap()); @@ -1142,8 +1140,6 @@ mod tests { let raw_uri = String::from(crate::callers::endpoints::QUEUESONGUPDATE); let end_index = raw_uri.len() - 5; - // let mut uri: String = (&raw_uri[..end_index]).to_string(); - // uri += &id.to_string(); let uri = format!( "{}/{}", @@ -1151,7 +1147,6 @@ mod tests { id.to_string() ); - // app.clone().oneshot(req).await match app .clone() .oneshot( -- 2.47.3