From 2a273a52c546d9c464a2360f0e2db47a0a7fc4ab Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 20 May 2025 19:34:31 -0400 Subject: [PATCH 1/7] Changes to status definition --- src/callers/song.rs | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/callers/song.rs b/src/callers/song.rs index 0f4872d..0fcb004 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -27,16 +27,25 @@ pub mod response { } } +pub mod status { + pub const PENDING: &str = "pending"; + // Will be used later on + pub const PROCESSING: &str = "processing"; + pub const DONE: &str = "done"; + + pub async fn is_valid(status: &str) -> bool { + if status == PENDING || status == PROCESSING || status == DONE { + true + } else { + false + } + } +} + + mod song_queue { use sqlx::Row; - pub mod status { - pub const PENDING: &str = "pending"; - // Will be used later on - pub const PROCESSING: &str = "processing"; - pub const _DONE: &str = "done"; - } - #[derive(Debug, serde::Serialize, sqlx::FromRow)] pub struct InsertedData { pub id: uuid::Uuid, @@ -96,8 +105,8 @@ mod song_queue { RETURNING id, filename, status; "#, ) - .bind(status::PROCESSING) - .bind(status::PENDING) + .bind(super::status::PROCESSING) + .bind(super::status::PENDING) .fetch_one(pool) .await .map_err(|e| { @@ -186,7 +195,7 @@ pub mod endpoint { &pool, &raw_data, &file_name, - &song_queue::status::PENDING.to_string(), + &super::status::PENDING.to_string(), ) .await .unwrap(); -- 2.47.3 From 9562b4fabfad54f25cb97723eabac1f482541dc9 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 20 May 2025 20:09:31 -0400 Subject: [PATCH 2/7] Added endpoint to update song_queue status --- src/callers/song.rs | 111 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/src/callers/song.rs b/src/callers/song.rs index 0fcb004..a99d23f 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -5,6 +5,14 @@ pub mod request { pub struct Request { pub message: String, } + + pub mod update_status { + #[derive(Default, serde::Deserialize, serde::Serialize)] + pub struct Request { + pub id: uuid::Uuid, + pub status: String, + } + } } pub mod response { @@ -25,11 +33,24 @@ pub mod response { pub data: Vec, } } + + pub mod update_status { + #[derive(serde::Deserialize, serde::Serialize)] + pub struct ChangedStatus { + pub old_status: String, + pub new_status: String, + } + + #[derive(Default, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } } pub mod status { pub const PENDING: &str = "pending"; - // Will be used later on pub const PROCESSING: &str = "processing"; pub const DONE: &str = "done"; @@ -132,6 +153,53 @@ mod song_queue { } } + pub async fn get_status_of_song_queue(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result { + let result = sqlx::query( + r#" + SELECT id, status FROM "songQueue" WHERE id = $1 + "#, + ) + .bind(id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error selecting: {:?}", e); + }); + + match result { + Ok(row) => { + Ok(row.try_get("status").map_err(|_e| sqlx::Error::RowNotFound).unwrap()) + } + Err(_err) => { + Err(sqlx::Error::RowNotFound) + } + } + } + + pub async fn update_song_queue_status(pool: &sqlx::PgPool, status: &String, id: &uuid::Uuid) -> Result { + let result = sqlx::query( + r#" + UPDATE "songQueue" SET status = $1 WHERE id = $2 RETURNING status; + "#, + ) + .bind(status) + .bind(id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error updating record {:?}", e); + }); + + match result { + Ok(row) => { + Ok(row.try_get("status").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#" @@ -260,4 +328,45 @@ pub mod endpoint { Err(_err) => (StatusCode::BAD_REQUEST, axum::response::Response::default()), } } + + pub async fn update_song_queue_status( + axum::Extension(pool): axum::Extension, + axum::Json(payload): axum::Json, + ) -> (axum::http::StatusCode, super::response::update_status::Response) { + let mut response = super::response::update_status::Response::default(); + + if super::status::is_valid(&payload.status).await { + let id = payload.id; + if !id.is_nil() { + match super::song_queue::get_status_of_song_queue(&pool, &id).await { + Ok(old) => { + match super::song_queue::update_song_queue_status(&pool, &payload.status, &id).await { + Ok(new) => { + response.message = String::from("Successful"); + response.data.push(super::response::update_status::ChangedStatus{ + old_status: old, + new_status: new + }); + (axum::http::StatusCode::OK, response) + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::OK, response) + } + } + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::OK, response) + } + } + } else { + response.message = String::from("Id is nil"); + (axum::http::StatusCode::BAD_REQUEST, response) + } + } else { + response.message = String::from("Status not valid"); + (axum::http::StatusCode::BAD_REQUEST, response) + } + } } -- 2.47.3 From 3e04a6afb8661694c077102edd7b5ff2c4686038 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 20 May 2025 20:15:22 -0400 Subject: [PATCH 3/7] endpoint return fix --- 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 a99d23f..d6b43c5 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -351,13 +351,13 @@ pub mod endpoint { } Err(err) => { response.message = err.to_string(); - (axum::http::StatusCode::OK, response) + (axum::http::StatusCode::BAD_REQUEST, response) } } } Err(err) => { response.message = err.to_string(); - (axum::http::StatusCode::OK, response) + (axum::http::StatusCode::BAD_REQUEST, response) } } } else { -- 2.47.3 From e3546ffa071f387ba20b796c14291f4901b3d447 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 20 May 2025 20:20:27 -0400 Subject: [PATCH 4/7] Endpoint now available --- src/callers/song.rs | 12 ++++++------ src/main.rs | 4 ++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/callers/song.rs b/src/callers/song.rs index d6b43c5..c850211 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -332,7 +332,7 @@ pub mod endpoint { pub async fn update_song_queue_status( axum::Extension(pool): axum::Extension, axum::Json(payload): axum::Json, - ) -> (axum::http::StatusCode, super::response::update_status::Response) { + ) -> (axum::http::StatusCode, axum::Json) { let mut response = super::response::update_status::Response::default(); if super::status::is_valid(&payload.status).await { @@ -347,26 +347,26 @@ pub mod endpoint { old_status: old, new_status: new }); - (axum::http::StatusCode::OK, response) + (axum::http::StatusCode::OK, axum::Json(response)) } Err(err) => { response.message = err.to_string(); - (axum::http::StatusCode::BAD_REQUEST, response) + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } } Err(err) => { response.message = err.to_string(); - (axum::http::StatusCode::BAD_REQUEST, response) + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } } else { response.message = String::from("Id is nil"); - (axum::http::StatusCode::BAD_REQUEST, response) + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } else { response.message = String::from("Status not valid"); - (axum::http::StatusCode::BAD_REQUEST, response) + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } } diff --git a/src/main.rs b/src/main.rs index f2a6e14..8d275a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,6 +69,10 @@ pub mod init { crate::callers::endpoints::QUEUESONG, post(crate::callers::song::endpoint::queue_song), ) + .route( + crate::callers::endpoints::QUEUESONG, + patch(crate::callers::song::endpoint::update_song_queue_status), + ) .route( crate::callers::endpoints::QUEUESONGDATA, get(crate::callers::song::endpoint::download_flac), -- 2.47.3 From e1ad1f6cc4063673358af0ce6098bb2a5da5d256 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 20 May 2025 20:34:03 -0400 Subject: [PATCH 5/7] Added test --- src/main.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/src/main.rs b/src/main.rs index 8d275a7..fc4e37a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -749,4 +749,86 @@ mod tests { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } + + #[tokio::test] + async fn test_song_queue_update_status() { + 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 old = &resp.data[0].status; + let done = crate::callers::song::status::DONE; + let payload = serde_json::json!({ + "id": &resp.data[0].id, + "status": done, + }); + + match app + .clone() + .oneshot( + axum::http::Request::builder() + .method(axum::http::Method::PATCH) + .uri(crate::callers::endpoints::QUEUESONG) + .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::song::response::update_status::Response, + >(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + let changed_status = &resp.data[0]; + + assert_eq!(*old, changed_status.old_status, "Old status does not match"); + assert_eq!(done, changed_status.new_status, "New status does not match"); + } + 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 9f618d1db5640eb9dedfddac4c07ea35cf8ef470 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 20 May 2025 20:34:15 -0400 Subject: [PATCH 6/7] Code formatting --- src/callers/song.rs | 91 ++++++++++++++++++++++++++------------------- src/main.rs | 35 ++++++++++------- 2 files changed, 74 insertions(+), 52 deletions(-) diff --git a/src/callers/song.rs b/src/callers/song.rs index c850211..902fae3 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -63,7 +63,6 @@ pub mod status { } } - mod song_queue { use sqlx::Row; @@ -153,50 +152,55 @@ mod song_queue { } } - pub async fn get_status_of_song_queue(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result { + pub async fn get_status_of_song_queue( + pool: &sqlx::PgPool, + id: &uuid::Uuid, + ) -> Result { let result = sqlx::query( r#" SELECT id, status FROM "songQueue" WHERE id = $1 "#, - ) - .bind(id) - .fetch_one(pool) - .await - .map_err(|e| { - eprintln!("Error selecting: {:?}", e); - }); + ) + .bind(id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error selecting: {:?}", e); + }); match result { - Ok(row) => { - Ok(row.try_get("status").map_err(|_e| sqlx::Error::RowNotFound).unwrap()) - } - Err(_err) => { - Err(sqlx::Error::RowNotFound) - } + Ok(row) => Ok(row + .try_get("status") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap()), + Err(_err) => Err(sqlx::Error::RowNotFound), } } - pub async fn update_song_queue_status(pool: &sqlx::PgPool, status: &String, id: &uuid::Uuid) -> Result { + pub async fn update_song_queue_status( + pool: &sqlx::PgPool, + status: &String, + id: &uuid::Uuid, + ) -> Result { let result = sqlx::query( r#" UPDATE "songQueue" SET status = $1 WHERE id = $2 RETURNING status; "#, - ) - .bind(status) - .bind(id) - .fetch_one(pool) - .await - .map_err(|e| { - eprintln!("Error updating record {:?}", e); - }); + ) + .bind(status) + .bind(id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error updating record {:?}", e); + }); match result { - Ok(row) => { - Ok(row.try_get("status").map_err(|_e| sqlx::Error::RowNotFound).unwrap()) - } - Err(_) => { - Err(sqlx::Error::RowNotFound) - } + Ok(row) => Ok(row + .try_get("status") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap()), + Err(_) => Err(sqlx::Error::RowNotFound), } } @@ -330,9 +334,12 @@ pub mod endpoint { } pub async fn update_song_queue_status( - axum::Extension(pool): axum::Extension, - axum::Json(payload): axum::Json, - ) -> (axum::http::StatusCode, axum::Json) { + axum::Extension(pool): axum::Extension, + axum::Json(payload): axum::Json, + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { let mut response = super::response::update_status::Response::default(); if super::status::is_valid(&payload.status).await { @@ -340,13 +347,21 @@ pub mod endpoint { if !id.is_nil() { match super::song_queue::get_status_of_song_queue(&pool, &id).await { Ok(old) => { - match super::song_queue::update_song_queue_status(&pool, &payload.status, &id).await { + match super::song_queue::update_song_queue_status( + &pool, + &payload.status, + &id, + ) + .await + { Ok(new) => { response.message = String::from("Successful"); - response.data.push(super::response::update_status::ChangedStatus{ - old_status: old, - new_status: new - }); + response + .data + .push(super::response::update_status::ChangedStatus { + old_status: old, + new_status: new, + }); (axum::http::StatusCode::OK, axum::Json(response)) } Err(err) => { diff --git a/src/main.rs b/src/main.rs index fc4e37a..c68d9f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -802,22 +802,29 @@ mod tests { .body(axum::body::Body::from(payload.to_string())) .unwrap(), ) - .await { - Ok(response) => { - let resp = get_resp_data::< - crate::callers::song::response::update_status::Response, - >(response) - .await; - assert_eq!(false, resp.data.is_empty(), "Should not be empty"); - let changed_status = &resp.data[0]; + .await + { + Ok(response) => { + let resp = get_resp_data::< + crate::callers::song::response::update_status::Response, + >(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + let changed_status = &resp.data[0]; - assert_eq!(*old, changed_status.old_status, "Old status does not match"); - assert_eq!(done, changed_status.new_status, "New status does not match"); - } - Err(err) => { - assert!(false, "Error: {:?}", err); - } + assert_eq!( + *old, changed_status.old_status, + "Old status does not match" + ); + assert_eq!( + done, changed_status.new_status, + "New status does not match" + ); } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } } Err(err) => { assert!(false, "Error: {:?}", err); -- 2.47.3 From 2cf9b5742952c4dab1dbe540ef8cd943ee1ffc3b Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 20 May 2025 20:38:01 -0400 Subject: [PATCH 7/7] Clippy warning fix --- src/callers/song.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/callers/song.rs b/src/callers/song.rs index 902fae3..b87834b 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -55,11 +55,7 @@ pub mod status { pub const DONE: &str = "done"; pub async fn is_valid(status: &str) -> bool { - if status == PENDING || status == PROCESSING || status == DONE { - true - } else { - false - } + status == PENDING || status == PROCESSING || status == DONE } } -- 2.47.3