Saving changes
This commit is contained in:
@@ -376,6 +376,77 @@ pub mod cov_db {
|
|||||||
Err(_) => Err(sqlx::Error::RowNotFound),
|
Err(_) => Err(sqlx::Error::RowNotFound),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_coverart_with_song_id(
|
||||||
|
pool: &sqlx::PgPool,
|
||||||
|
song_id: &uuid::Uuid,
|
||||||
|
) -> Result<icarus_models::coverart::CoverArt, sqlx::Error> {
|
||||||
|
let result = sqlx::query(
|
||||||
|
r#"
|
||||||
|
SELECT id, title, path, song_id FROM "coverart" WHERE song_id = $1;
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(song_id)
|
||||||
|
.fetch_one(pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
eprintln!("Error querying 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(),
|
||||||
|
data: Vec::new(),
|
||||||
|
song_id: row
|
||||||
|
.try_get("song_id")
|
||||||
|
.map_err(|_e| sqlx::Error::RowNotFound)
|
||||||
|
.unwrap(),
|
||||||
|
}),
|
||||||
|
Err(_) => Err(sqlx::Error::RowNotFound),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_coverart(pool: &sqlx::PgPool, id: &uuid::Uuid) -> Result<icarus_models::coverart::CoverArt, sqlx::Error> {
|
||||||
|
let result = sqlx::query(
|
||||||
|
r#"
|
||||||
|
DELETE FROM "coverrt"
|
||||||
|
WHERE id = $1
|
||||||
|
RETURNING id, title, path, song_id
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
.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("id").map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
||||||
|
path: row.try_get("id").map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
||||||
|
song_id: row.try_get("id").map_err(|_e| sqlx::Error::RowNotFound).unwrap(),
|
||||||
|
data: Vec::new(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Err(_err) => {
|
||||||
|
Err(sqlx::Error::RowNotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod endpoint {
|
pub mod endpoint {
|
||||||
|
|||||||
+23
-10
@@ -1204,21 +1204,34 @@ pub mod endpoint {
|
|||||||
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 song.song_path() {
|
Ok(song) => match super::super::coverart::cov_db::get_coverart_with_song_id(&pool, &song.id).await {
|
||||||
Ok(song_path) => match super::song_db::delete_song(&pool, &id).await {
|
Ok(coverart) => {
|
||||||
Ok(deleted_song) => match std::fs::remove_file(song_path) {
|
let coverart_path = std::path::Path::new(&coverart.path);
|
||||||
Ok(result) => {
|
if coverart_path.exists() {
|
||||||
response.data.push(deleted_song);
|
match song.song_path() {
|
||||||
(axum::http::StatusCode::OK, axum::Json(response))
|
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) => {
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
response.message = err.to_string();
|
}
|
||||||
(axum::http::StatusCode::NOT_FOUND, axum::Json(response))
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
response.message = String::from("Could not locate coverart on the filesystem");
|
||||||
|
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
response.message = err.to_string();
|
response.message = String::from("Could not locate coverart on the filesystem");
|
||||||
(axum::http::StatusCode::NOT_FOUND, axum::Json(response))
|
(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) => {
|
Err(err) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user