From d2302f44fdce592554f15989d67e0f018d52f485 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 26 Apr 2025 19:13:12 -0400 Subject: [PATCH 01/15] Added time crate --- Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index f11a69a..b891c3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,10 +17,11 @@ tracing-subscriber = "0.3.19" futures = { version = "0.3.31" } uuid = { version = "1.16.0", features = ["v4", "serde"] } sqlx = { version = "0.8.5", features = ["postgres", "runtime-tokio-native-tls", "time", "uuid"] } +time = { version = "0.3.41", features = ["formatting", "macros", "parsing", "serde"] } dotenvy = { version = "0.15.7" } icarus_meta = { git = "ssh://git@git.kundeng.us/phoenix/icarus_meta.git", tag = "v0.1.30" } icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.4.3" } [dev-dependencies] common-multipart-rfc7578 = { version = "0.7.0" } -url = { version = "2.5.4" } \ No newline at end of file +url = { version = "2.5.4" } -- 2.47.3 From 43d84bcd302bb165dd65483331a478221db7e007 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 26 Apr 2025 19:29:24 -0400 Subject: [PATCH 02/15] Added endpoint to get metadata --- src/callers/metadata.rs | 97 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/src/callers/metadata.rs b/src/callers/metadata.rs index c16546d..abb0f0f 100644 --- a/src/callers/metadata.rs +++ b/src/callers/metadata.rs @@ -36,6 +36,14 @@ pub mod request { }) } } + + pub mod fetch_metadata { + #[derive(serde::Deserialize)] + pub struct Params { + pub id: Option, + pub song_queue_id: Option, + } + } } pub mod response { @@ -46,9 +54,19 @@ pub mod response { pub message: String, pub data: Vec, } + + pub mod fetch_metadata { + use serde::{Deserialize, Serialize}; + + #[derive(Default, Deserialize, Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } } -mod metadata_queue { +pub mod metadata_queue { use sqlx::Row; #[derive(Debug, serde::Serialize, sqlx::FromRow)] @@ -56,6 +74,14 @@ mod metadata_queue { pub id: uuid::Uuid, } + #[derive(Debug, serde::Deserialize, serde::Serialize, sqlx::FromRow)] + pub struct MetadataQueue { + pub id: uuid::Uuid, + pub metadata: String, + pub created_at: time::OffsetDateTime, + pub song_queue_id: uuid::Uuid, + } + pub async fn insert( pool: &sqlx::PgPool, metadata: &serde_json::Value, @@ -85,6 +111,47 @@ mod metadata_queue { Err(_err) => Err(sqlx::Error::RowNotFound), } } + + pub async fn get_with_song_queue_id( + pool: &sqlx::PgPool, + song_queue_id: &uuid::Uuid, + ) -> Result { + let result = sqlx::query( + r#" + SELECT * FROM "metadataQueue" WHERE song_queue_id = $1; + "#, + ) + .bind(song_queue_id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error inserting: {}", e); + }); + + match result { + Ok(row) => { + Ok(MetadataQueue{ + id: row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + metadata: row + .try_get("metadata") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + created_at: row + .try_get("created_at") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + song_queue_id: row + .try_get("song_queue_id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + }) + } + Err(_err) => Err(sqlx::Error::RowNotFound), + } + } } pub mod endpoint { @@ -115,4 +182,32 @@ pub mod endpoint { } } } + + + pub async fn fetch_metadata( + axum::Extension(pool): axum::Extension, + axum::extract::Query(params): axum::extract::Query + ) -> ( + StatusCode, + Json, + ) { + let mut response = super::response::fetch_metadata::Response::default(); + let song_queue_id: uuid::Uuid = match params.song_queue_id { + Some(id) => id, + None => uuid::Uuid::nil(), + }; + // TODO: Make sure id works as well + + match super::metadata_queue::get_with_song_queue_id(&pool, &song_queue_id).await { + Ok(item) => { + response.message = String::from("Successful"); + response.data.push(item); + (StatusCode::OK, Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (StatusCode::BAD_REQUEST, Json(response)) + } + } + } } -- 2.47.3 From 0b491667a6154588685d2363fa5d58d7eb5ceedc Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 26 Apr 2025 19:30:41 -0400 Subject: [PATCH 03/15] Formatting --- src/callers/metadata.rs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/callers/metadata.rs b/src/callers/metadata.rs index abb0f0f..2be6f1d 100644 --- a/src/callers/metadata.rs +++ b/src/callers/metadata.rs @@ -115,7 +115,7 @@ pub mod metadata_queue { pub async fn get_with_song_queue_id( pool: &sqlx::PgPool, song_queue_id: &uuid::Uuid, - ) -> Result { + ) -> Result { let result = sqlx::query( r#" SELECT * FROM "metadataQueue" WHERE song_queue_id = $1; @@ -129,26 +129,24 @@ pub mod metadata_queue { }); match result { - Ok(row) => { - Ok(MetadataQueue{ - id: row + Ok(row) => Ok(MetadataQueue { + id: row .try_get("id") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), - metadata: row + metadata: row .try_get("metadata") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), - created_at: row + created_at: row .try_get("created_at") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), - song_queue_id: row + song_queue_id: row .try_get("song_queue_id") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), - }) - } + }), Err(_err) => Err(sqlx::Error::RowNotFound), } } @@ -183,14 +181,10 @@ pub mod endpoint { } } - pub async fn fetch_metadata( axum::Extension(pool): axum::Extension, - axum::extract::Query(params): axum::extract::Query - ) -> ( - StatusCode, - Json, - ) { + axum::extract::Query(params): axum::extract::Query, + ) -> (StatusCode, Json) { let mut response = super::response::fetch_metadata::Response::default(); let song_queue_id: uuid::Uuid = match params.song_queue_id { Some(id) => id, -- 2.47.3 From 19c34da7ba8df863ecd113b7628b02d7854fc132 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Mon, 28 Apr 2025 20:51:46 -0400 Subject: [PATCH 04/15] Added more code to get metadata Having issues returning response --- src/callers/metadata.rs | 93 ++++++++++++++++++++++++++++++++++------- src/main.rs | 4 ++ 2 files changed, 82 insertions(+), 15 deletions(-) diff --git a/src/callers/metadata.rs b/src/callers/metadata.rs index 2be6f1d..35a9376 100644 --- a/src/callers/metadata.rs +++ b/src/callers/metadata.rs @@ -1,7 +1,7 @@ pub mod request { use serde::{Deserialize, Serialize}; - #[derive(Default, Deserialize, Serialize)] + #[derive(Debug, Default, Deserialize, Serialize, sqlx::FromRow)] pub struct Request { pub id: uuid::Uuid, pub album: String, @@ -38,7 +38,7 @@ pub mod request { } pub mod fetch_metadata { - #[derive(serde::Deserialize)] + #[derive(Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, sqlx::Decode)] pub struct Params { pub id: Option, pub song_queue_id: Option, @@ -77,7 +77,8 @@ pub mod metadata_queue { #[derive(Debug, serde::Deserialize, serde::Serialize, sqlx::FromRow)] pub struct MetadataQueue { pub id: uuid::Uuid, - pub metadata: String, + // pub metadata: serde_json::Value, + pub metadata: serde_json::Value, pub created_at: time::OffsetDateTime, pub song_queue_id: uuid::Uuid, } @@ -150,6 +151,48 @@ pub mod metadata_queue { Err(_err) => Err(sqlx::Error::RowNotFound), } } + + pub async fn get_with_id( + pool: &sqlx::PgPool, + id: &uuid::Uuid, + ) -> Result { + let result = sqlx::query( + r#" + SELECT id, metadata, created_at, song_queue_id FROM "metadataQueue" WHERE id = $1; + "#, + ) + .bind(id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error inserting: {}", e); + }); + + // println!("SQL {:?}", result); + + match result { + Ok(row) => Ok(MetadataQueue { + id: row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + metadata: row + .try_get("metadata") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + // serde_json::Value::new(), + created_at: row + .try_get("created_at") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + song_queue_id: row + .try_get("song_queue_id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + }), + Err(_err) => Err(sqlx::Error::RowNotFound), + } + } } pub mod endpoint { @@ -186,20 +229,40 @@ pub mod endpoint { axum::extract::Query(params): axum::extract::Query, ) -> (StatusCode, Json) { let mut response = super::response::fetch_metadata::Response::default(); - let song_queue_id: uuid::Uuid = match params.song_queue_id { - Some(id) => id, - None => uuid::Uuid::nil(), - }; - // TODO: Make sure id works as well - match super::metadata_queue::get_with_song_queue_id(&pool, &song_queue_id).await { - Ok(item) => { - response.message = String::from("Successful"); - response.data.push(item); - (StatusCode::OK, Json(response)) + // TODO: Make sure id works as well + match (params.id, params.song_queue_id) { + (Some(id), Some(song_queue_id)) => { + println!("Something works {:?} {:?}", id, song_queue_id); + + if !id.is_nil() { + match super::metadata_queue::get_with_id(&pool, &id).await { + Ok(item) => { + response.message = String::from("Successful"); + response.data.push(item); + (StatusCode::OK, Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (StatusCode::BAD_REQUEST, Json(response)) + } + } + } else { + match super::metadata_queue::get_with_song_queue_id(&pool, &song_queue_id).await { + Ok(item) => { + response.message = String::from("Successful"); + response.data.push(item); + (StatusCode::OK, Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (StatusCode::BAD_REQUEST, Json(response)) + } + } + } } - Err(err) => { - response.message = err.to_string(); + _ => { + println!("What is going on?"); (StatusCode::BAD_REQUEST, Json(response)) } } diff --git a/src/main.rs b/src/main.rs index 5a12085..cbff386 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,6 +81,10 @@ pub mod init { crate::callers::endpoints::QUEUEMETADATA, post(crate::callers::metadata::endpoint::queue_metadata), ) + .route( + crate::callers::endpoints::QUEUEMETADATA, + get(crate::callers::metadata::endpoint::fetch_metadata), + ) } pub async fn app() -> axum::Router { -- 2.47.3 From 720651843a5b3b96b07d43b45fc4ba81f8a4f60d Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Mon, 28 Apr 2025 20:52:35 -0400 Subject: [PATCH 05/15] Formatting --- src/callers/metadata.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/callers/metadata.rs b/src/callers/metadata.rs index 35a9376..f7948e3 100644 --- a/src/callers/metadata.rs +++ b/src/callers/metadata.rs @@ -38,7 +38,9 @@ pub mod request { } pub mod fetch_metadata { - #[derive(Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, sqlx::Decode)] + #[derive( + Debug, Default, serde::Deserialize, serde::Serialize, sqlx::FromRow, sqlx::Decode, + )] pub struct Params { pub id: Option, pub song_queue_id: Option, @@ -180,7 +182,7 @@ pub mod metadata_queue { .try_get("metadata") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), - // serde_json::Value::new(), + // serde_json::Value::new(), created_at: row .try_get("created_at") .map_err(|_e| sqlx::Error::RowNotFound) @@ -248,7 +250,8 @@ pub mod endpoint { } } } else { - match super::metadata_queue::get_with_song_queue_id(&pool, &song_queue_id).await { + match super::metadata_queue::get_with_song_queue_id(&pool, &song_queue_id).await + { Ok(item) => { response.message = String::from("Successful"); response.data.push(item); -- 2.47.3 From ccbb82c643854a705d616504297871903c7eac3e Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 29 Apr 2025 20:57:00 -0400 Subject: [PATCH 06/15] Migration change --- migrations/20250420185217_init_migration.sql | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/migrations/20250420185217_init_migration.sql b/migrations/20250420185217_init_migration.sql index 6ea4c60..7243417 100644 --- a/migrations/20250420185217_init_migration.sql +++ b/migrations/20250420185217_init_migration.sql @@ -12,9 +12,10 @@ CREATE TABLE IF NOT EXISTS "songQueue" ( CREATE TABLE IF NOT EXISTS "metadataQueue" ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), metadata jsonb NOT NULL, - created_at timestamp DEFAULT now(), + created_at timestamptz DEFAULT now(), + -- TIMESTAMPTZ song_queue_id UUID NOT NULL ); -- Create an index for better query performance -CREATE INDEX metadata_queue_data_metadata ON "metadataQueue" USING gin (metadata); \ No newline at end of file +CREATE INDEX metadata_queue_data_metadata ON "metadataQueue" USING gin (metadata); -- 2.47.3 From 1d4db581b1bbbb876916c998ee83148404932fc2 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 29 Apr 2025 21:04:27 -0400 Subject: [PATCH 07/15] Fix date issue --- src/callers/metadata.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/callers/metadata.rs b/src/callers/metadata.rs index f7948e3..e1a2c54 100644 --- a/src/callers/metadata.rs +++ b/src/callers/metadata.rs @@ -80,7 +80,8 @@ pub mod metadata_queue { pub struct MetadataQueue { pub id: uuid::Uuid, // pub metadata: serde_json::Value, - pub metadata: serde_json::Value, + // pub metadata: serde_json::Value, + #[serde(with = "time::serde::rfc3339")] pub created_at: time::OffsetDateTime, pub song_queue_id: uuid::Uuid, } @@ -137,10 +138,10 @@ pub mod metadata_queue { .try_get("id") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), - metadata: row - .try_get("metadata") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), + // metadata: row + // .try_get("metadata") + // .map_err(|_e| sqlx::Error::RowNotFound) + // .unwrap(), created_at: row .try_get("created_at") .map_err(|_e| sqlx::Error::RowNotFound) @@ -178,10 +179,10 @@ pub mod metadata_queue { .try_get("id") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), - metadata: row - .try_get("metadata") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), + // metadata: row + // .try_get("metadata") + // .map_err(|_e| sqlx::Error::RowNotFound) + // .unwrap(), // serde_json::Value::new(), created_at: row .try_get("created_at") @@ -238,6 +239,8 @@ pub mod endpoint { println!("Something works {:?} {:?}", id, song_queue_id); if !id.is_nil() { + println!("Id is not nil"); + match super::metadata_queue::get_with_id(&pool, &id).await { Ok(item) => { response.message = String::from("Successful"); @@ -250,6 +253,7 @@ pub mod endpoint { } } } else { + println!("Song queue Id is probably not nil"); match super::metadata_queue::get_with_song_queue_id(&pool, &song_queue_id).await { Ok(item) => { -- 2.47.3 From 4597f2d016114fb88952af8f39ff0edfb311a9e8 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 29 Apr 2025 21:14:50 -0400 Subject: [PATCH 08/15] Got it somewhat working --- src/callers/metadata.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/callers/metadata.rs b/src/callers/metadata.rs index e1a2c54..e674522 100644 --- a/src/callers/metadata.rs +++ b/src/callers/metadata.rs @@ -80,7 +80,7 @@ pub mod metadata_queue { pub struct MetadataQueue { pub id: uuid::Uuid, // pub metadata: serde_json::Value, - // pub metadata: serde_json::Value, + pub metadata: serde_json::Value, #[serde(with = "time::serde::rfc3339")] pub created_at: time::OffsetDateTime, pub song_queue_id: uuid::Uuid, @@ -138,10 +138,10 @@ pub mod metadata_queue { .try_get("id") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), - // metadata: row - // .try_get("metadata") - // .map_err(|_e| sqlx::Error::RowNotFound) - // .unwrap(), + metadata: row + .try_get("metadata") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), created_at: row .try_get("created_at") .map_err(|_e| sqlx::Error::RowNotFound) @@ -174,11 +174,15 @@ pub mod metadata_queue { // println!("SQL {:?}", result); match result { - Ok(row) => Ok(MetadataQueue { + Ok(row) => + { + let data: serde_json::Value = row.try_get("metadata").map_err(|_e| sqlx::Error::RowNotFound).unwrap(); + Ok(MetadataQueue { id: row .try_get("id") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), + metadata: data, // metadata: row // .try_get("metadata") // .map_err(|_e| sqlx::Error::RowNotFound) @@ -192,7 +196,8 @@ pub mod metadata_queue { .try_get("song_queue_id") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), - }), + }) + }, Err(_err) => Err(sqlx::Error::RowNotFound), } } -- 2.47.3 From a6a682f45868bf9febf4d26a3d4abe1606f0b9b6 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 29 Apr 2025 21:18:45 -0400 Subject: [PATCH 09/15] Got things working --- src/callers/metadata.rs | 46 +++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/callers/metadata.rs b/src/callers/metadata.rs index e674522..fab85b1 100644 --- a/src/callers/metadata.rs +++ b/src/callers/metadata.rs @@ -239,12 +239,10 @@ pub mod endpoint { let mut response = super::response::fetch_metadata::Response::default(); // TODO: Make sure id works as well - match (params.id, params.song_queue_id) { - (Some(id), Some(song_queue_id)) => { - println!("Something works {:?} {:?}", id, song_queue_id); + match params.id { + Some(id) => { + println!("Something works {:?}", id); - if !id.is_nil() { - println!("Id is not nil"); match super::metadata_queue::get_with_id(&pool, &id).await { Ok(item) => { @@ -257,25 +255,29 @@ pub mod endpoint { (StatusCode::BAD_REQUEST, Json(response)) } } - } else { - println!("Song queue Id is probably not nil"); - match super::metadata_queue::get_with_song_queue_id(&pool, &song_queue_id).await - { - Ok(item) => { - response.message = String::from("Successful"); - response.data.push(item); - (StatusCode::OK, Json(response)) - } - Err(err) => { - response.message = err.to_string(); - (StatusCode::BAD_REQUEST, Json(response)) - } - } - } } _ => { - println!("What is going on?"); - (StatusCode::BAD_REQUEST, Json(response)) + match params.song_queue_id { + Some(song_queue_id) => { + println!("Song queue Id is probably not nil"); + match super::metadata_queue::get_with_song_queue_id(&pool, &song_queue_id).await + { + Ok(item) => { + response.message = String::from("Successful"); + response.data.push(item); + (StatusCode::OK, Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (StatusCode::BAD_REQUEST, Json(response)) + } + } + } + None => { + println!("What is going on?"); + (StatusCode::BAD_REQUEST, Json(response)) + } + } } } } -- 2.47.3 From 05c39b442150c55f7e372d405fdfd22088d28600 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 29 Apr 2025 21:26:14 -0400 Subject: [PATCH 10/15] Code fix --- src/callers/metadata.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/callers/metadata.rs b/src/callers/metadata.rs index fab85b1..7e4324e 100644 --- a/src/callers/metadata.rs +++ b/src/callers/metadata.rs @@ -79,7 +79,6 @@ pub mod metadata_queue { #[derive(Debug, serde::Deserialize, serde::Serialize, sqlx::FromRow)] pub struct MetadataQueue { pub id: uuid::Uuid, - // pub metadata: serde_json::Value, pub metadata: serde_json::Value, #[serde(with = "time::serde::rfc3339")] pub created_at: time::OffsetDateTime, @@ -171,8 +170,6 @@ pub mod metadata_queue { eprintln!("Error inserting: {}", e); }); - // println!("SQL {:?}", result); - match result { Ok(row) => { @@ -183,11 +180,6 @@ pub mod metadata_queue { .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(), metadata: data, - // metadata: row - // .try_get("metadata") - // .map_err(|_e| sqlx::Error::RowNotFound) - // .unwrap(), - // serde_json::Value::new(), created_at: row .try_get("created_at") .map_err(|_e| sqlx::Error::RowNotFound) @@ -201,6 +193,7 @@ pub mod metadata_queue { Err(_err) => Err(sqlx::Error::RowNotFound), } } + } pub mod endpoint { @@ -238,7 +231,6 @@ pub mod endpoint { ) -> (StatusCode, Json) { let mut response = super::response::fetch_metadata::Response::default(); - // TODO: Make sure id works as well match params.id { Some(id) => { println!("Something works {:?}", id); -- 2.47.3 From fe818fc154dbdf33d645478028b6aaf5d37fa5de Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 29 Apr 2025 21:36:38 -0400 Subject: [PATCH 11/15] Added test --- src/main.rs | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/src/main.rs b/src/main.rs index cbff386..a52323e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -501,4 +501,97 @@ mod tests { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } + + #[tokio::test] + async fn test_get_metadata_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 new_payload: serde_json::Value = serde_json::json!( + { + "id": resp.data[0], + "album" : "Machine Gun: The FillMore East First Show", + "album_artist" : "Jimi Hendrix", + "artist" : "Jimi Hendrix", + "disc" : 1, + "disc_count" : 1, + "duration" : 330, + "genre" : "Psychadelic Rock", + "title" : "Power of Soul", + "track" : 1, + "track_count" : 11, + "year" : 2016 + }); + + match app + .clone() + .oneshot( + axum::http::Request::builder() + .method(axum::http::Method::POST) + .uri(crate::callers::endpoints::QUEUEMETADATA) + .header(axum::http::header::CONTENT_TYPE, "application/json") + .body(axum::body::Body::from(new_payload.to_string())) + .unwrap(), + ) + .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]; + let uri = format!("{}?id={}", crate::callers::endpoints::QUEUEMETADATA, id); + + match app.clone().oneshot( + axum::http::Request::builder() + .method(axum::http::Method::GET) + // .uri(crate::callers::endpoints::QUEUEMETADATA) + .uri(crate::callers::endpoints::QUEUEMETADATA) + .header(axum::http::header::CONTENT_TYPE, "application/json") + // .body(axum::body::Body::from(new_payload.to_string())) + .body(axum::body::Body::empty()) + .unwrap(), + ).await { + Ok(response) => { + } + 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 07eead8b92ee99c9aa39e6859d9988225728b693 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 29 Apr 2025 21:37:24 -0400 Subject: [PATCH 12/15] Formatting --- src/callers/metadata.rs | 84 ++++++++++++++++++++--------------------- src/main.rs | 27 +++++++------ 2 files changed, 56 insertions(+), 55 deletions(-) diff --git a/src/callers/metadata.rs b/src/callers/metadata.rs index 7e4324e..253023f 100644 --- a/src/callers/metadata.rs +++ b/src/callers/metadata.rs @@ -171,29 +171,30 @@ pub mod metadata_queue { }); match result { - Ok(row) => - { - let data: serde_json::Value = row.try_get("metadata").map_err(|_e| sqlx::Error::RowNotFound).unwrap(); + Ok(row) => { + let data: serde_json::Value = row + .try_get("metadata") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(); Ok(MetadataQueue { - id: row - .try_get("id") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), - metadata: data, - created_at: row - .try_get("created_at") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), - song_queue_id: row - .try_get("song_queue_id") - .map_err(|_e| sqlx::Error::RowNotFound) - .unwrap(), + id: row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + metadata: data, + created_at: row + .try_get("created_at") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + song_queue_id: row + .try_get("song_queue_id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), }) - }, + } Err(_err) => Err(sqlx::Error::RowNotFound), } } - } pub mod endpoint { @@ -235,8 +236,23 @@ pub mod endpoint { Some(id) => { println!("Something works {:?}", id); - - match super::metadata_queue::get_with_id(&pool, &id).await { + match super::metadata_queue::get_with_id(&pool, &id).await { + Ok(item) => { + response.message = String::from("Successful"); + response.data.push(item); + (StatusCode::OK, Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (StatusCode::BAD_REQUEST, Json(response)) + } + } + } + _ => match params.song_queue_id { + Some(song_queue_id) => { + println!("Song queue Id is probably not nil"); + match super::metadata_queue::get_with_song_queue_id(&pool, &song_queue_id).await + { Ok(item) => { response.message = String::from("Successful"); response.data.push(item); @@ -247,30 +263,12 @@ pub mod endpoint { (StatusCode::BAD_REQUEST, Json(response)) } } - } - _ => { - match params.song_queue_id { - Some(song_queue_id) => { - println!("Song queue Id is probably not nil"); - match super::metadata_queue::get_with_song_queue_id(&pool, &song_queue_id).await - { - Ok(item) => { - response.message = String::from("Successful"); - response.data.push(item); - (StatusCode::OK, Json(response)) - } - Err(err) => { - response.message = err.to_string(); - (StatusCode::BAD_REQUEST, Json(response)) - } - } - } - None => { - println!("What is going on?"); - (StatusCode::BAD_REQUEST, Json(response)) - } } - } + None => { + println!("What is going on?"); + (StatusCode::BAD_REQUEST, Json(response)) + } + }, } } } diff --git a/src/main.rs b/src/main.rs index a52323e..9096227 100644 --- a/src/main.rs +++ b/src/main.rs @@ -565,18 +565,21 @@ mod tests { let id = resp.data[0]; let uri = format!("{}?id={}", crate::callers::endpoints::QUEUEMETADATA, id); - match app.clone().oneshot( - axum::http::Request::builder() - .method(axum::http::Method::GET) - // .uri(crate::callers::endpoints::QUEUEMETADATA) - .uri(crate::callers::endpoints::QUEUEMETADATA) - .header(axum::http::header::CONTENT_TYPE, "application/json") - // .body(axum::body::Body::from(new_payload.to_string())) - .body(axum::body::Body::empty()) - .unwrap(), - ).await { - Ok(response) => { - } + match app + .clone() + .oneshot( + axum::http::Request::builder() + .method(axum::http::Method::GET) + // .uri(crate::callers::endpoints::QUEUEMETADATA) + .uri(crate::callers::endpoints::QUEUEMETADATA) + .header(axum::http::header::CONTENT_TYPE, "application/json") + // .body(axum::body::Body::from(new_payload.to_string())) + .body(axum::body::Body::empty()) + .unwrap(), + ) + .await + { + Ok(response) => {} Err(err) => { assert!(false, "Error: {:?}", err); } -- 2.47.3 From 15ce2ff9506deed30106fef0f751f7fce9d0ad4b Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 29 Apr 2025 21:47:24 -0400 Subject: [PATCH 13/15] Test works --- src/main.rs | 62 ++++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9096227..f61e41c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -279,6 +279,24 @@ mod tests { serde_json::from_slice(&body).unwrap() } + pub async fn payload_data(id: &uuid::Uuid) -> serde_json::Value { + serde_json::json!( + { + "id": id, + "album" : "Machine Gun: The FillMore East First Show", + "album_artist" : "Jimi Hendrix", + "artist" : "Jimi Hendrix", + "disc" : 1, + "disc_count" : 1, + "duration" : 330, + "genre" : "Psychadelic Rock", + "title" : "Power of Soul", + "track" : 1, + "track_count" : 11, + "year" : 2016 + }) + } + #[tokio::test] async fn test_song_queue() { let tm_pool = db_mgr::get_pool().await.unwrap(); @@ -455,21 +473,7 @@ mod tests { 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 new_payload: serde_json::Value = serde_json::json!( - { - "id": resp.data[0], - "album" : "Machine Gun: The FillMore East First Show", - "album_artist" : "Jimi Hendrix", - "artist" : "Jimi Hendrix", - "disc" : 1, - "disc_count" : 1, - "duration" : 330, - "genre" : "Psychadelic Rock", - "title" : "Power of Soul", - "track" : 1, - "track_count" : 11, - "year" : 2016 - }); + let new_payload = payload_data(&resp.data[0]).await; match app .clone() @@ -528,21 +532,7 @@ mod tests { 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 new_payload: serde_json::Value = serde_json::json!( - { - "id": resp.data[0], - "album" : "Machine Gun: The FillMore East First Show", - "album_artist" : "Jimi Hendrix", - "artist" : "Jimi Hendrix", - "disc" : 1, - "disc_count" : 1, - "duration" : 330, - "genre" : "Psychadelic Rock", - "title" : "Power of Soul", - "track" : 1, - "track_count" : 11, - "year" : 2016 - }); + let new_payload = payload_data(&resp.data[0]).await; match app .clone() @@ -570,16 +560,20 @@ mod tests { .oneshot( axum::http::Request::builder() .method(axum::http::Method::GET) - // .uri(crate::callers::endpoints::QUEUEMETADATA) - .uri(crate::callers::endpoints::QUEUEMETADATA) + .uri(uri) .header(axum::http::header::CONTENT_TYPE, "application/json") - // .body(axum::body::Body::from(new_payload.to_string())) .body(axum::body::Body::empty()) .unwrap(), ) .await { - Ok(response) => {} + Ok(response) => + { + let resp = + get_resp_data::(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Data should not be empty"); + } Err(err) => { assert!(false, "Error: {:?}", err); } -- 2.47.3 From 8a2dfc31e92637150b12866c56bdd48f5f7a5b6d Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 29 Apr 2025 21:47:40 -0400 Subject: [PATCH 14/15] Cleanup --- src/main.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index f61e41c..560d5fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -567,11 +567,11 @@ mod tests { ) .await { - Ok(response) => - { - let resp = - get_resp_data::(response) - .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"); } Err(err) => { -- 2.47.3 From 4633ccc98c73be3c4003b3af5c0c5767f9f3b460 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 29 Apr 2025 21:49:00 -0400 Subject: [PATCH 15/15] Migrations cleanup --- 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 7243417..fd94f5f 100644 --- a/migrations/20250420185217_init_migration.sql +++ b/migrations/20250420185217_init_migration.sql @@ -9,11 +9,11 @@ CREATE TABLE IF NOT EXISTS "songQueue" ( data BYTEA NOT NULL ); +-- Table to store queued metadata CREATE TABLE IF NOT EXISTS "metadataQueue" ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), metadata jsonb NOT NULL, created_at timestamptz DEFAULT now(), - -- TIMESTAMPTZ song_queue_id UUID NOT NULL ); -- 2.47.3