diff --git a/lektord/src/app/routes.rs b/lektord/src/app/routes.rs index e95260b2866bbf9d41a04760445817f178c1204d..694ce5d3c897bb00af03f80e78f81bdaa4eab74a 100644 --- a/lektord/src/app/routes.rs +++ b/lektord/src/app/routes.rs @@ -99,25 +99,31 @@ pub(crate) async fn toggle_play_state() -> Result<(), LektordError> { Ok(crate::c_wrapper::player_toggle_pause()?) } -/// Get all the informations about a kara by its id. +/// Get all the informations about a kara by its id. Returns the kara as a json object to avoid +/// cloning the kara structure and directly return the serialized object. #[axum::debug_handler(state = LektorStatePtr)] pub(crate) async fn get_kara_by_id( State(state): State<LektorStatePtr>, Path(id): Path<u64>, -) -> Result<Json<Kara>, LektordError> { - log::error!("find a way to not clone the kara"); - Ok(Json(state.database.get_kara_by_id(id).await?.clone())) +) -> Result<String, LektordError> { + Ok( + serde_json::to_string(state.database.get_kara_by_id(id).await?) + .map_err(|err| anyhow!("{err}"))?, + ) } -/// Get all the informations about a kara by its id. +/// Get all the informations about a kara by its id. Returns the kara as a json object to avoid +/// cloning the kara structure and directly return the serialized object. #[axum::debug_handler(state = LektorStatePtr)] pub(crate) async fn get_kara_by_kid( State(state): State<LektorStatePtr>, Path(id): Path<String>, -) -> Result<Json<Kara>, LektordError> { - log::error!("find a way to not clone the kara"); +) -> Result<String, LektordError> { match state.database.get_kid_from_str(&decode_base64(&id)?).await { - Some(id) => Ok(Json(state.database.get_kara_by_kid(id).await?.clone())), + Some(id) => Ok( + serde_json::to_string(state.database.get_kara_by_kid(id).await?) + .map_err(|err| anyhow!("{err}"))?, + ), None => Err(anyhow!("no kara found with id {id}").into()), } } @@ -251,12 +257,10 @@ pub(crate) async fn adm_update( admin: LektorUser, ) -> Result<(), LektordError> { let admin = state.verify_user(admin); - if admin.is_admin() { - log::info!("launching update requested by {admin:?}"); - } else { + if !admin.is_admin() { return Err(anyhow!("user {admin:?} is not an admin").into()); } - + log::info!("launching update requested by {admin:?}"); std::thread::spawn(move || { let rt = tokio::runtime::Builder::new_current_thread() .max_blocking_threads(1024) @@ -270,12 +274,11 @@ pub(crate) async fn adm_update( let count = state.repo.update_with(&handle).await?; handle.finished().await; state.database.refresh_playlist_contents().await; - Ok::<_, LektordError>(count) + Ok::<_, anyhow::Error>(count) }); - match res.await { + match res.await.map_err(|err| anyhow!("{err}")) { Ok(Ok(count)) => log::info!("finished updating database with karas: {count}"), - Ok(Err(err)) => log::error!("{err}"), - Err(err) => log::error!("{err}"), + Err(err) | Ok(Err(err)) => log::error!("{err}"), } }); rt.block_on(local);