diff --git a/src/callers/song.rs b/src/callers/song.rs index b91d7ee..66731c2 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -35,10 +35,19 @@ pub mod request { impl Request { pub fn is_valid(&self) -> bool { - !self.title.is_empty() || !self.artist.is_empty() || !self.album_artist.is_empty() - || !self.album.is_empty() || !self.genre.is_empty() || !self.date.is_empty() - || self.track > 0 || self.disc > 0 || self.track_count > 0 || self.disc_count > 0 - || self.duration > 0 || !self.audio_type.is_empty() || !self.user_id.is_nil() + !self.title.is_empty() + || !self.artist.is_empty() + || !self.album_artist.is_empty() + || !self.album.is_empty() + || !self.genre.is_empty() + || !self.date.is_empty() + || self.track > 0 + || self.disc > 0 + || self.track_count > 0 + || self.disc_count > 0 + || self.duration > 0 + || !self.audio_type.is_empty() + || !self.user_id.is_nil() || !self.song_queue_id.is_nil() } @@ -114,7 +123,7 @@ pub mod response { #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] pub struct Response { pub message: String, - pub data: Vec + pub data: Vec, } } } @@ -135,7 +144,10 @@ mod song_db { use sqlx::Row; // 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> { + pub async fn insert( + pool: &sqlx::PgPool, + song: &icarus_models::song::Song, + ) -> Result<(String, uuid::Uuid), sqlx::Error> { 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) @@ -169,14 +181,15 @@ mod song_db { .try_get("id") .map_err(|_e| sqlx::Error::RowNotFound) .unwrap(); - let date_created_time: time::OffsetDateTime = row.try_get("date_created").map_err(|_e| sqlx::Error::RowNotFound).unwrap(); + let date_created_time: time::OffsetDateTime = row + .try_get("date_created") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(); let date_created = date_created_time.to_string(); Ok((date_created, id)) } - Err(_) => { - Err(sqlx::Error::RowNotFound) - } + Err(_) => Err(sqlx::Error::RowNotFound), } } } @@ -575,12 +588,16 @@ pub mod endpoint { pub async fn create_metadata( 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::create_metadata::Response::default(); if payload.is_valid() { let mut song = payload.to_song(); - song.filename = song.generate_filename(icarus_models::types::MusicTypes::FlacExtension, true); + song.filename = + song.generate_filename(icarus_models::types::MusicTypes::FlacExtension, true); song.directory = crate::environment::get_root_directory().await.unwrap(); match song_queue::get_data(&pool, &payload.song_queue_id).await { @@ -593,21 +610,19 @@ pub mod endpoint { file.write_all(&song.data).unwrap(); match song.song_path() { - Ok(_) => { - match super::song_db::insert(&pool, &song).await { - Ok((date_created, id)) => { - song.id = id; - song.date_created = date_created; - response.data.push(song); + Ok(_) => match super::song_db::insert(&pool, &song).await { + Ok((date_created, id)) => { + song.id = id; + song.date_created = date_created; + response.data.push(song); - (axum::http::StatusCode::OK, axum::Json(response)) - } - Err(err) => { - response.message = format!("{:?} song {:?}", err.to_string(), song); - (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) - } + (axum::http::StatusCode::OK, axum::Json(response)) } - } + Err(err) => { + response.message = format!("{:?} song {:?}", err.to_string(), song); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + }, Err(err) => { response.message = err.to_string(); (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) diff --git a/src/environment.rs b/src/environment.rs index 936efbf..27446ff 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -1,4 +1,3 @@ - pub async fn get_db_url() -> String { dotenvy::dotenv().ok(); std::env::var(crate::keys::DBURL).expect(crate::keys::error::ERROR) diff --git a/src/main.rs b/src/main.rs index 15b7ac2..d9d4892 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ pub mod keys; pub mod db { use sqlx::postgres::PgPoolOptions; - + pub mod connection_settings { pub const MAXCONN: u32 = 10; } @@ -99,7 +99,7 @@ pub mod init { ) .route( crate::callers::endpoints::CREATESONG, - post(crate::callers::song::endpoint::create_metadata) + post(crate::callers::song::endpoint::create_metadata), ) } @@ -269,15 +269,18 @@ mod tests { app.clone().oneshot(fetch_req).await } - async fn fetch_metadata_queue_req(app: &axum::Router, id: &uuid::Uuid) -> Result { + async fn fetch_metadata_queue_req( + app: &axum::Router, + id: &uuid::Uuid, + ) -> Result { let uri = format!("{}?id={}", crate::callers::endpoints::QUEUEMETADATA, id); let req = axum::http::Request::builder() - .method(axum::http::Method::GET) - .uri(uri) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .body(axum::body::Body::empty()) - .unwrap(); + .method(axum::http::Method::GET) + .uri(uri) + .header(axum::http::header::CONTENT_TYPE, "application/json") + .body(axum::body::Body::empty()) + .unwrap(); app.clone().oneshot(req).await } @@ -321,14 +324,18 @@ mod tests { app.clone().oneshot(req).await } - async fn queue_metadata_req(app: &axum::Router, id: &uuid::Uuid) -> Result { + async fn queue_metadata_req( + app: &axum::Router, + id: &uuid::Uuid, + ) -> Result { let payload = payload_data(&id).await; let req = 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(payload.to_string())).unwrap(); + .body(axum::body::Body::from(payload.to_string())) + .unwrap(); app.clone().oneshot(req).await } @@ -562,8 +569,7 @@ mod tests { 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 - { + match queue_metadata_req(&app, &resp.data[0]).await { Ok(response) => { let resp = get_resp_data::(response) @@ -610,8 +616,7 @@ mod tests { 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 - { + match queue_metadata_req(&app, &resp.data[0]).await { Ok(response) => { let resp = get_resp_data::(response) @@ -620,8 +625,7 @@ mod tests { let id = resp.data[0]; - match fetch_metadata_queue_req(&app, &id).await - { + match fetch_metadata_queue_req(&app, &id).await { Ok(response) => { let resp = get_resp_data::< crate::callers::metadata::response::fetch_metadata::Response, @@ -1215,8 +1219,7 @@ mod tests { 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 - { + match queue_metadata_req(&app, &resp.data[0]).await { Ok(response) => { let resp = get_resp_data::(response) @@ -1225,8 +1228,7 @@ mod tests { let id = resp.data[0]; - match fetch_metadata_queue_req(&app, &id).await - { + match fetch_metadata_queue_req(&app, &id).await { Ok(response) => { let resp = get_resp_data::< crate::callers::metadata::response::fetch_metadata::Response, @@ -1235,7 +1237,6 @@ mod tests { assert_eq!(false, resp.data.is_empty(), "Data should not be empty"); let song_q_id = resp.data[0].song_queue_id; - let payload = serde_json::json!({ "title": "Power of Soul", "artist": "Jimmi Hendrix", @@ -1255,23 +1256,37 @@ mod tests { eprintln!("Payload: {:?}", payload); - 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 { + 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); + 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); + assert_eq!( + false, + song_id.is_nil(), + "Song id should not be nil {:?}", + song + ); } Err(err) => { assert!(false, "Error: {:?}", err);