json response, give feedback when playlist not exists

This commit is contained in:
jb-alvarado 2024-03-26 14:42:32 +01:00
parent 82c02e293c
commit 1742c9044d
2 changed files with 15 additions and 8 deletions

View File

@ -817,7 +817,7 @@ pub async fn save_playlist(
data: web::Json<JsonPlaylist>,
) -> Result<impl Responder, ServiceError> {
match write_playlist(&pool.into_inner(), *id, data.into_inner()).await {
Ok(res) => Ok(res),
Ok(res) => Ok(web::Json(res)),
Err(e) => Err(e),
}
}
@ -885,7 +885,7 @@ pub async fn del_playlist(
params: web::Path<(i32, String)>,
) -> Result<impl Responder, ServiceError> {
match delete_playlist(&pool.into_inner(), params.0, &params.1).await {
Ok(_) => Ok(format!("Delete playlist from {} success!", params.1)),
Ok(m) => Ok(web::Json(m)),
Err(e) => Err(e),
}
}

View File

@ -114,7 +114,11 @@ pub async fn generate_playlist(
}
}
pub async fn delete_playlist(conn: &Pool<Sqlite>, id: i32, date: &str) -> Result<(), ServiceError> {
pub async fn delete_playlist(
conn: &Pool<Sqlite>,
id: i32,
date: &str,
) -> Result<String, ServiceError> {
let (config, _) = playout_config(conn, &id).await?;
let mut playlist_path = PathBuf::from(&config.playlist.path);
let d: Vec<&str> = date.split('-').collect();
@ -125,11 +129,14 @@ pub async fn delete_playlist(conn: &Pool<Sqlite>, id: i32, date: &str) -> Result
.with_extension("json");
if playlist_path.is_file() {
if let Err(e) = fs::remove_file(playlist_path) {
error!("{e}");
return Err(ServiceError::InternalServerError);
match fs::remove_file(playlist_path) {
Ok(_) => return Ok(format!("Delete playlist from {date} success!")),
Err(e) => {
error!("{e}");
return Err(ServiceError::InternalServerError);
}
};
} else {
Ok(format!("No playlist to delete on: {date}"))
}
Ok(())
}