From db8e5458caa250a573c541208fba754979bf6322 Mon Sep 17 00:00:00 2001 From: phoenix Date: Thu, 13 Aug 2026 11:12:44 -0400 Subject: [PATCH] Saving code --- src/callers/queue/song.rs | 61 ++++++++++++++++++++++++++++++++------- src/repo/queue/data.rs | 16 ++++++++++ 2 files changed, 66 insertions(+), 11 deletions(-) diff --git a/src/callers/queue/song.rs b/src/callers/queue/song.rs index 4f90857..419fb7b 100644 --- a/src/callers/queue/song.rs +++ b/src/callers/queue/song.rs @@ -358,14 +358,7 @@ pub mod endpoint { println!("Id: {song_queue_id}"); let lab_config = crate::util::maze::get_config(); - let lr = labyrinth::Labyrinth { config: lab_config }; - /* - let data = labyrinth::Data { - raw_data: copied_raw_data, - ..Default::default() - }; - */ match repo::data::get_with_song_queue_id(&pool, &song_queue_id).await { Ok((id, file_key, _bucket, _region, _)) => match lr.download(&file_key).await { @@ -481,7 +474,7 @@ pub mod endpoint { ) )] pub async fn update_song_queue( - axum::extract::Path(id): axum::extract::Path, + axum::extract::Path(song_queue_id): axum::extract::Path, axum::Extension(pool): axum::Extension, mut multipart: axum::extract::Multipart, ) -> ( @@ -512,15 +505,61 @@ pub mod endpoint { match super::is_song_valid(&raw_data).await { Ok(valid) => { if valid { - match repo::song::update(&pool, &raw_data, &id).await { - Ok(_) => { + let lab_config = crate::util::maze::get_config(); + let lr = labyrinth::Labyrinth { config: lab_config }; + + match repo::data::get_with_song_queue_id(&pool, &song_queue_id).await { + Ok((id, file_key, _bucket, _region, _)) => match lr.delete(&file_key).await { + Ok(_response) => { + let data = labyrinth::Data { + raw_data: raw_data, + ..Default::default() + }; + + let filename = simodels::song::generate_filename( + simodels::types::MusicType::FlacExtension, + true, + ) + .unwrap(); + let new_file_key = format!("queued/song/{filename}"); + + match lr.upload(&new_file_key, &data).await { + Ok(_) => { + match repo::data::update_file_key(&pool, &song_queue_id, &new_file_key).await { + Ok(_) => { response.message = String::from(super::super::super::response::SUCCESSFUL); response.data.push(id); (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + response.message = err.to_string(); + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) + } + } + } + Err(err) => { + eprintln!("Error: {err:?}"); + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) + } + } + } + Err(err) => { + eprintln!("Error: {err:?}"); + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) + } } Err(err) => { - response.message = err.to_string(); + eprintln!("Error: {err:?}"); ( axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response), diff --git a/src/repo/queue/data.rs b/src/repo/queue/data.rs index 007826d..d7e0f23 100644 --- a/src/repo/queue/data.rs +++ b/src/repo/queue/data.rs @@ -25,6 +25,22 @@ pub async fn insert( } } +pub async fn update_file_key(pool: &sqlx::PgPool, id: &uuid::Uuid, file_key: &str) -> Result<(), sqlx::Error> { + match sqlx::query( + r#" + UPDATE "songQueueData" SET file_key = $1 WHERE id = $2; + "#, + ) + .bind(file_key) + .bind(id) + .execute(pool) + .await + { + Ok(_row) => Ok(()), + Err(_) => Err(sqlx::Error::RowNotFound), + } +} + pub async fn get( pool: &sqlx::PgPool, id: &uuid::Uuid,