diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index 988d0ce..c0e73e2 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -38,6 +38,13 @@ pub mod request { pub coverart_queue_id: uuid::Uuid, } } + + pub mod wipe_data_from_coverart_queue { + #[derive(Debug, serde::Deserialize, serde::Serialize)] + pub struct Request { + pub coverart_queue_id: uuid::Uuid + } + } } pub mod response { @@ -84,6 +91,14 @@ pub mod response { pub data: Vec, } } + + pub mod wipe_data_from_coverart_queue { + #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] + pub struct Response { + pub message: String, + pub data: Vec, + } + } } pub mod db { @@ -246,6 +261,29 @@ pub mod db { Err(_) => Err(sqlx::Error::RowNotFound), } } + + pub async fn wipe_data(pool: &sqlx::PgPool, coverart_queue_id: &uuid::Uuid) -> Result { + let result = sqlx::query( + r#" + UPDATE "coverartQueue" SET data = NULL WHERE id = $1 RETURNING id; + "# + ) + .bind(coverart_queue_id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error updating query: {:?}", e); + }); + + match result { + Ok(row) => { + Ok(row.try_get("id").map_err(|_e| sqlx::Error::RowNotFound).unwrap()) + } + Err(_) => { + Err(sqlx::Error::RowNotFound) + } + } + } } pub mod cov_db { @@ -508,4 +546,33 @@ pub mod endpoint { } } } + + pub async fn wipe_data_from_coverart_queue(axum::Extension(pool): axum::Extension, + axum::Json(payload): axum::Json,) -> ( + axum::http::StatusCode, + axum::Json, + ) { + let mut response = super::response::wipe_data_from_coverart_queue::Response::default(); + let coverart_queue_id = payload.coverart_queue_id; + + match super::db::get_coverart_queue_with_id(&pool, &coverart_queue_id).await { + Ok(coverart_queue) => { + match super::db::wipe_data(&pool, &coverart_queue.id).await { + Ok(id) => { + response.message = String::from("Success"); + 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)) + } + } + } + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::NOT_FOUND, axum::Json(response)) + } + } + } }