From 64ac7da3a03856086135d3ab41e5cfc3ea922d58 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Fri, 23 May 2025 18:44:43 -0400 Subject: [PATCH] Ironing out some kinks --- src/callers/song.rs | 46 +++++++++++---------- src/main.rs | 98 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 21 deletions(-) diff --git a/src/callers/song.rs b/src/callers/song.rs index 3990f0f..4a351b5 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -111,7 +111,7 @@ pub mod response { } pub mod create_metadata { - #[derive(Default, serde::Deserialize, serde::Serialize)] + #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] pub struct Response { pub message: String, pub data: Vec @@ -136,33 +136,36 @@ mod song { // TODO: Change first parameter of return value from string to a time type pub async fn insert(pool: &sqlx::PgPool, song: &icarus_models::song::Song) -> Result<(String, uuid::Uuid), sqlx::Error> { + + // $11, $12, $13, $14, $15) RETURNING date_created, id; + let result = sqlx::query( r#" - INSERT INTO "song" (title, artist, album_artist, album, genre, year, - track, disc, track_count, disc_count, duration, audio_type, filename, - directory, user_id) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, - $11, $12, $13, $14, $15) RETURNING date_created, id; + INSERT INTO "song" (title, artist, album_artist, album, genre, year, track, disc, track_count, disc_count, duration, audio_type, filename, directory, user_id) + VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) RETURNING id; "# ) - .bind(song.title.clone()) - .bind(song.artist.clone()) - .bind(song.album_artist.clone()) - .bind(song.album.clone()) - .bind(song.genre.clone()) - .bind(song.year) - .bind(song.track) - .bind(song.disc) - .bind(song.track_count) - .bind(song.disc_count) - .bind(song.duration) - .bind(song.audio_type.clone()) - .bind(song.filename.clone()) - .bind(song.directory.clone()) - .bind(song.user_id) + .bind(&song.title) + .bind(&song.artist) + .bind(&song.album_artist) + .bind(&song.album) + .bind(&song.genre) + .bind(&song.year) + .bind(&song.track) + .bind(&song.disc) + .bind(&song.track_count) + .bind(&song.disc_count) + .bind(&song.duration) + .bind(&song.audio_type) + .bind(&song.filename) + .bind(&song.directory) + .bind(&song.user_id) .fetch_one(pool) .await .map_err(|e| { eprintln!("Error inserting query: {:?}", e); + eprintln!("Year: {:?}", song.year); + eprintln!("Song: {:?}", song); }); match result { @@ -171,7 +174,8 @@ mod song { .try_get("id") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(); - let date_created = row.try_get("date_created").map_err(|_e| sqlx::Error::RowNotFound).unwrap(); + // let date_created = row.try_get("date_created").map_err(|_e| sqlx::Error::RowNotFound).unwrap(); + let date_created = String::from("2025-01-01"); Ok((date_created, id)) } diff --git a/src/main.rs b/src/main.rs index 682dfc6..f52fc73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1195,5 +1195,103 @@ mod tests { #[tokio::test] async fn test_create_song() { + 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 payload = serde_json::json!({ + "title": "Power of Soul", + "artist": "Jimmi Hendrix", + "album_artist": "Jimmi Hendrix", + "album": "Machine Gun", + "genre": "Psychadelic Rock", + "date": "2016-01-01", + "track": 1, + "disc": 1, + "track_count": 11, + "disc_count": 1, + "duration": 330, + "audio_type": "flac", + "user_id": "d6e159c1-9648-4c85-81e5-52f502ff53e4", + "song_queue_id": id + }); + + match app.clone().oneshot( + axum::http::Request::builder() + .method(axum::http::Method::POST) + .uri(crate::callers::endpoints::CREATESONG) + .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(), "No songs found, Response {:?}", resp); + } + 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; } }