diff --git a/src/callers/coverart.rs b/src/callers/coverart.rs index 8102cc1..a2b2aa0 100644 --- a/src/callers/coverart.rs +++ b/src/callers/coverart.rs @@ -417,34 +417,45 @@ pub mod cov_db { } } - pub async fn delete_coverart(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result { + pub async fn delete_coverart( + pool: &sqlx::PgPool, + id: &uuid::Uuid, + ) -> Result { let result = sqlx::query( r#" DELETE FROM "coverart" WHERE id = $1 RETURNING id, title, path, song_id - "# - ) - .bind(id) - .fetch_one(pool) - .await - .map_err(|e| { - eprintln!("Error deleting data: {e:?}"); - }); + "#, + ) + .bind(id) + .fetch_one(pool) + .await + .map_err(|e| { + eprintln!("Error deleting data: {e:?}"); + }); match result { - Ok(row) => { - Ok(icarus_models::coverart::CoverArt { - id: row.try_get("id").map_err(|_e| sqlx::Error::RowNotFound).unwrap(), - title: row.try_get("title").map_err(|_e| sqlx::Error::RowNotFound).unwrap(), - path: row.try_get("path").map_err(|_e| sqlx::Error::RowNotFound).unwrap(), - song_id: row.try_get("song_id").map_err(|_e| sqlx::Error::RowNotFound).unwrap(), - data: Vec::new(), - }) - } - Err(_err) => { - Err(sqlx::Error::RowNotFound) - } + Ok(row) => Ok(icarus_models::coverart::CoverArt { + id: row + .try_get("id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + title: row + .try_get("title") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + path: row + .try_get("path") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + song_id: row + .try_get("song_id") + .map_err(|_e| sqlx::Error::RowNotFound) + .unwrap(), + data: Vec::new(), + }), + Err(_err) => Err(sqlx::Error::RowNotFound), } } } diff --git a/src/callers/song.rs b/src/callers/song.rs index ba14736..fcdcfd6 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -178,7 +178,7 @@ pub mod response { #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] pub struct SongAndCoverArt { pub song: icarus_models::song::Song, - pub coverart: icarus_models::coverart::CoverArt + pub coverart: icarus_models::coverart::CoverArt, } #[derive(Debug, Default, serde::Deserialize, serde::Serialize)] @@ -351,7 +351,10 @@ pub mod song_db { } } - pub async fn delete_song(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result { + pub async fn delete_song( + pool: &sqlx::PgPool, + id: &uuid::Uuid, + ) -> Result { let result = sqlx::query( // icarus_models::song::Song, r#" @@ -1205,59 +1208,94 @@ pub mod endpoint { } } - pub async fn delete_song(axum::Extension(pool): axum::Extension, axum::extract::Path(id): axum::extract::Path) - -> (axum::http::StatusCode, axum::Json) { + pub async fn delete_song( + axum::Extension(pool): axum::Extension, + axum::extract::Path(id): axum::extract::Path, + ) -> ( + axum::http::StatusCode, + axum::Json, + ) { let mut response = super::response::delete_song::Response::default(); match super::song_db::get_song(&pool, &id).await { - Ok(song) => match super::super::coverart::cov_db::get_coverart_with_song_id(&pool, &song.id).await { - Ok(coverart) => { - let coverart_path = std::path::Path::new(&coverart.path); - if coverart_path.exists() { - match song.song_path() { - Ok(song_path) => match super::song_db::delete_song(&pool, &song.id).await { - Ok(deleted_song) => match super::super::coverart::cov_db::delete_coverart(&pool, &coverart.id).await { - Ok(deleted_coverart) => match std::fs::remove_file(song_path) { - Ok(_) => match std::fs::remove_file(&coverart.path) { - Ok(_) => { - response.message = String::from(super::super::response::SUCCESSFUL); - response.data.push(super::response::delete_song::SongAndCoverArt{ song: deleted_song, coverart: deleted_coverart }); - (axum::http::StatusCode::OK, axum::Json(response)) - } - Err(err) => { - response.message = err.to_string(); - (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)) + Ok(song) => { + match super::super::coverart::cov_db::get_coverart_with_song_id(&pool, &song.id) + .await + { + Ok(coverart) => { + let coverart_path = std::path::Path::new(&coverart.path); + if coverart_path.exists() { + match song.song_path() { + Ok(song_path) => { + match super::song_db::delete_song(&pool, &song.id).await { + Ok(deleted_song) => { + match super::super::coverart::cov_db::delete_coverart( + &pool, + &coverart.id, + ) + .await + { + Ok(deleted_coverart) => { + match std::fs::remove_file(song_path) { + Ok(_) => match std::fs::remove_file( + &coverart.path, + ) { + Ok(_) => { + response.message = String::from(super::super::response::SUCCESSFUL); + response.data.push(super::response::delete_song::SongAndCoverArt{ song: deleted_song, coverart: deleted_coverart }); + ( + 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) => { + response.message = err.to_string(); + (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)) + } + } + } + + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)) + } } } Err(err) => { response.message = err.to_string(); - (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)) + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) } } - - Err(err) => { - response.message = err.to_string(); - (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)) - } } Err(err) => { - response.message = err.to_string(); - (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)) + response.message = err.to_string(); + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) } } - Err(err) => { - response.message = err.to_string(); - (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)) - } + } else { + response.message = + String::from("Could not locate coverart on the filesystem"); + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response), + ) } - } else { - response.message = String::from("Could not locate coverart on the filesystem"); - (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)) } - } - Err(err) => { - response.message = err.to_string(); - (axum::http::StatusCode::NOT_FOUND, axum::Json(response)) + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::NOT_FOUND, axum::Json(response)) + } } } Err(err) => { diff --git a/src/main.rs b/src/main.rs index c5a5cf1..bbdbe0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -135,7 +135,10 @@ pub mod init { crate::callers::endpoints::DOWNLOADSONG, get(crate::callers::song::endpoint::download_song), ) - .route(crate::callers::endpoints::DELETESONG, delete(crate::callers::song::endpoint::delete_song)) + .route( + crate::callers::endpoints::DELETESONG, + delete(crate::callers::song::endpoint::delete_song), + ) } pub async fn app() -> axum::Router { @@ -2159,22 +2162,18 @@ mod tests { .await { Ok(response) => { - let resp = super::get_resp_data::(response).await; + let resp = super::get_resp_data::< + crate::callers::song::response::delete_song::Response, + >(response) + .await; assert_eq!(false, resp.data.is_empty(), "Response has no data"); + let song_and_coverart = &resp.data[0]; - assert_eq!(id, song_and_coverart.song.id, "Song Ids do not match {id:?} {:?}", song_and_coverart.song.id); - // let e = response.into_body(); - // let mut data = e.into_data_stream(); - /* - while let Some(chunk) = data.next().await { - match chunk { - Ok(_data) => {} - Err(err) => { - assert!(false, "Error: {err:?}"); - } - } - } - */ + assert_eq!( + id, song_and_coverart.song.id, + "Song Ids do not match {id:?} {:?}", + song_and_coverart.song.id + ); } Err(err) => { assert!(false, "Error: {err:?}");