From 0ce0c549d6fb8e1944bc6212f5fcbdc27cef1dde Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 22 May 2025 15:41:56 -0400 Subject: [PATCH] Added endpoint to update song from queue --- src/callers/song.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/src/callers/song.rs b/src/callers/song.rs index b87834b..d29cf3f 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -47,6 +47,14 @@ pub mod response { pub data: Vec, } } + + pub mod update_song_queue { + #[derive(Default, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec>, + } + } } pub mod status { @@ -106,6 +114,32 @@ mod song_queue { } } + pub async fn update( + pool: &sqlx::PgPool, + data: &Vec, + id: &uuid::Uuid, + ) -> Result, sqlx::Error> { + let result = sqlx::query( + r#" + UPDATE "songQueue" SET data = $1 WHERE id = $2 RETURNING data; + "# + ) + .bind(data) + .bind(id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error inserting: {:?}", e); + }); + + match result { + Ok(row) => { + Ok(row.try_get("data").map_err(|_e| sqlx::Error::RowNotFound).unwrap()) + } + Err(_) => Err(sqlx::Error::RowNotFound) + } + } + pub async fn get_most_recent_and_update(pool: &sqlx::PgPool) -> Result { let result = sqlx::query( r#" @@ -380,4 +414,51 @@ pub mod endpoint { (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) } } + + pub async fn update_song_queue( + axum::extract::Path(id): axum::extract::Path, + axum::Extension(pool): axum::Extension, + mut multipart: axum::extract::Multipart, + ) -> (axum::http::StatusCode, axum::Json) { + + let mut response = super::response::update_song_queue::Response::default(); + + if let Some(field) = multipart.next_field().await.unwrap() { + 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(); + + println!( + "Received file '{}' (name = '{}', content-type = '{}', size = {})", + file_name, + name, + content_type, + data.len() + ); + + // Save the file to disk + // let mut file = std::fs::File::create(&file_name).unwrap(); + // file.write_all(&data).unwrap(); + + let raw_data: Vec = data.to_vec(); + match song_queue::update( + &pool, + &raw_data, + &id, + ).await { + Ok(queued_data) => { + response.data.push(queued_data); + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + } + } else { + response.message = String::from("No data provided"); + (axum::http::StatusCode::NOT_FOUND, axum::Json(response)) + } + } }