From ca525dcab3117c8f4d762dca6bc0395e44e96000 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 1 May 2025 21:54:28 -0400 Subject: [PATCH] Added functionality and test --- src/callers/coverart.rs | 64 +++++++++++++++++++++++++++++++++++++---- src/main.rs | 48 +++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 5 deletions(-) diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index d70af9b..937be63 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -1,7 +1,40 @@ pub mod response { #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] pub struct Response { - pub rando: i32, + pub message: String, + pub data: Vec, + } +} + +mod db { + use sqlx::Row; + + pub async fn insert( + pool: &sqlx::PgPool, + data: &Vec, + ) -> Result { + let result = sqlx::query( + r#" + INSERT INTO "coverartQueue" (data) VALUES($1) RETURNING id; + "#, + ) + .bind(data) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error inserting: {}", e); + }); + + match result { + Ok(row) => { + let id: uuid::Uuid = row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(); + Ok(id) + } + Err(_err) => Err(sqlx::Error::RowNotFound), + } } } @@ -14,13 +47,16 @@ pub mod endpoint { axum::http::StatusCode, axum::Json, ) { - let response = super::response::Response::default(); + let mut response = super::response::Response::default(); - while let Some(field) = multipart.next_field().await.unwrap() { + // while let Some(field) = multipart.next_field().await.unwrap() { + match multipart.next_field().await { + Ok(Some(field)) => { 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(); + let raw_data = data.to_vec(); println!( "Received file '{}' (name = '{}', content-type = '{}', size = {})", @@ -30,11 +66,29 @@ pub mod endpoint { data.len() ); + match super::db::insert(&pool, &raw_data).await { + Ok(id) => { + response.message = String::from("Successful"); + response.data.push(id); + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + } + // Save the file to disk // let mut file = std::fs::File::create(&file_name).unwrap(); // file.write_all(&data).unwrap(); + } + Ok(None) => { + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } } - - (axum::http::StatusCode::OK, axum::Json(response)) } } diff --git a/src/main.rs b/src/main.rs index 9a70e89..3ffe04e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -595,4 +595,52 @@ mod tests { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } + + #[tokio::test] + async fn test_song_coverart_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; + let mut form = MultipartForm::default(); + let _ = form.add_file("jpg", "tests/Machine_gun/160809_machinegun.jpg"); + + // Create request + let content_type = form.content_type(); + let body = MultipartBody::from(form); + + // Send request + match app.clone().oneshot( + axum::http::Request::builder() + .method(axum::http::Method::POST) + .uri(crate::callers::endpoints::QUEUECOVERART) + .header(axum::http::header::CONTENT_TYPE, content_type) + .body(axum::body::Body::from_stream(body)) + .unwrap() + ).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"); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }; + + let _ = db_mgr::drop_database(&tm_pool, &db_name).await; + } }