Code cleanup and formatting
This commit is contained in:
+24
-13
@@ -417,13 +417,16 @@ pub mod cov_db {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete_coverart(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result<icarus_models::coverart::CoverArt, sqlx::Error> {
|
pub async fn delete_coverart(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
id: &uuid::Uuid,
|
||||||
|
) -> Result<icarus_models::coverart::CoverArt, sqlx::Error> {
|
||||||
let result = sqlx::query(
|
let result = sqlx::query(
|
||||||
r#"
|
r#"
|
||||||
DELETE FROM "coverart"
|
DELETE FROM "coverart"
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
RETURNING id, title, path, song_id
|
RETURNING id, title, path, song_id
|
||||||
"#
|
"#,
|
||||||
)
|
)
|
||||||
.bind(id)
|
.bind(id)
|
||||||
.fetch_one(pool)
|
.fetch_one(pool)
|
||||||
@@ -433,18 +436,26 @@ pub mod cov_db {
|
|||||||
});
|
});
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(row) => {
|
Ok(row) => Ok(icarus_models::coverart::CoverArt {
|
||||||
Ok(icarus_models::coverart::CoverArt {
|
id: row
|
||||||
id: row.try_get("id").map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
.try_get("id")
|
||||||
title: row.try_get("title").map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
path: row.try_get("path").map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
.unwrap(),
|
||||||
song_id: row.try_get("song_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(),
|
data: Vec::new(),
|
||||||
})
|
}),
|
||||||
}
|
Err(_err) => Err(sqlx::Error::RowNotFound),
|
||||||
Err(_err) => {
|
|
||||||
Err(sqlx::Error::RowNotFound)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+53
-15
@@ -178,7 +178,7 @@ pub mod response {
|
|||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
||||||
pub struct SongAndCoverArt {
|
pub struct SongAndCoverArt {
|
||||||
pub song: icarus_models::song::Song,
|
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)]
|
#[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<icarus_models::song::Song, sqlx::Error> {
|
pub async fn delete_song(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
id: &uuid::Uuid,
|
||||||
|
) -> Result<icarus_models::song::Song, sqlx::Error> {
|
||||||
let result = sqlx::query(
|
let result = sqlx::query(
|
||||||
// icarus_models::song::Song,
|
// icarus_models::song::Song,
|
||||||
r#"
|
r#"
|
||||||
@@ -1205,54 +1208,88 @@ pub mod endpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete_song(axum::Extension(pool): axum::Extension<sqlx::PgPool>, axum::extract::Path(id): axum::extract::Path<uuid::Uuid>)
|
pub async fn delete_song(
|
||||||
-> (axum::http::StatusCode, axum::Json<super::response::delete_song::Response>) {
|
axum::Extension(pool): axum::Extension<sqlx::PgPool>,
|
||||||
|
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
|
||||||
|
) -> (
|
||||||
|
axum::http::StatusCode,
|
||||||
|
axum::Json<super::response::delete_song::Response>,
|
||||||
|
) {
|
||||||
let mut response = super::response::delete_song::Response::default();
|
let mut response = super::response::delete_song::Response::default();
|
||||||
|
|
||||||
match super::song_db::get_song(&pool, &id).await {
|
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(song) => {
|
||||||
|
match super::super::coverart::cov_db::get_coverart_with_song_id(&pool, &song.id)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(coverart) => {
|
Ok(coverart) => {
|
||||||
let coverart_path = std::path::Path::new(&coverart.path);
|
let coverart_path = std::path::Path::new(&coverart.path);
|
||||||
if coverart_path.exists() {
|
if coverart_path.exists() {
|
||||||
match song.song_path() {
|
match song.song_path() {
|
||||||
Ok(song_path) => match super::song_db::delete_song(&pool, &song.id).await {
|
Ok(song_path) => {
|
||||||
Ok(deleted_song) => match super::super::coverart::cov_db::delete_coverart(&pool, &coverart.id).await {
|
match super::song_db::delete_song(&pool, &song.id).await {
|
||||||
Ok(deleted_coverart) => match std::fs::remove_file(song_path) {
|
Ok(deleted_song) => {
|
||||||
Ok(_) => match std::fs::remove_file(&coverart.path) {
|
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(_) => {
|
Ok(_) => {
|
||||||
response.message = String::from(super::super::response::SUCCESSFUL);
|
response.message = String::from(super::super::response::SUCCESSFUL);
|
||||||
response.data.push(super::response::delete_song::SongAndCoverArt{ song: deleted_song, coverart: deleted_coverart });
|
response.data.push(super::response::delete_song::SongAndCoverArt{ song: deleted_song, coverart: deleted_coverart });
|
||||||
(axum::http::StatusCode::OK, axum::Json(response))
|
(
|
||||||
|
axum::http::StatusCode::OK,
|
||||||
|
axum::Json(response),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
response.message = err.to_string();
|
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) => {
|
Err(err) => {
|
||||||
response.message = err.to_string();
|
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) => {
|
Err(err) => {
|
||||||
response.message = err.to_string();
|
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) => {
|
Err(err) => {
|
||||||
response.message = err.to_string();
|
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) => {
|
Err(err) => {
|
||||||
response.message = err.to_string();
|
response.message = err.to_string();
|
||||||
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response))
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
response.message = String::from("Could not locate coverart on the filesystem");
|
response.message =
|
||||||
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response))
|
String::from("Could not locate coverart on the filesystem");
|
||||||
|
(
|
||||||
|
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
axum::Json(response),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@@ -1260,6 +1297,7 @@ pub mod endpoint {
|
|||||||
(axum::http::StatusCode::NOT_FOUND, axum::Json(response))
|
(axum::http::StatusCode::NOT_FOUND, axum::Json(response))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
response.message = err.to_string();
|
response.message = err.to_string();
|
||||||
(axum::http::StatusCode::NOT_FOUND, axum::Json(response))
|
(axum::http::StatusCode::NOT_FOUND, axum::Json(response))
|
||||||
|
|||||||
+14
-15
@@ -135,7 +135,10 @@ pub mod init {
|
|||||||
crate::callers::endpoints::DOWNLOADSONG,
|
crate::callers::endpoints::DOWNLOADSONG,
|
||||||
get(crate::callers::song::endpoint::download_song),
|
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 {
|
pub async fn app() -> axum::Router {
|
||||||
@@ -2159,22 +2162,18 @@ mod tests {
|
|||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let resp = super::get_resp_data::<crate::callers::song::response::delete_song::Response>(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");
|
assert_eq!(false, resp.data.is_empty(), "Response has no data");
|
||||||
|
|
||||||
let song_and_coverart = &resp.data[0];
|
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);
|
assert_eq!(
|
||||||
// let e = response.into_body();
|
id, song_and_coverart.song.id,
|
||||||
// let mut data = e.into_data_stream();
|
"Song Ids do not match {id:?} {:?}",
|
||||||
/*
|
song_and_coverart.song.id
|
||||||
while let Some(chunk) = data.next().await {
|
);
|
||||||
match chunk {
|
|
||||||
Ok(_data) => {}
|
|
||||||
Err(err) => {
|
|
||||||
assert!(false, "Error: {err:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
assert!(false, "Error: {err:?}");
|
assert!(false, "Error: {err:?}");
|
||||||
|
|||||||
Reference in New Issue
Block a user