From c249de3a82f1dbd36c460a4eb615dca66df3cab4 Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Sun, 2 Jul 2023 18:08:41 +0100 Subject: [PATCH] fix: last commit --- src/animations.rs | 116 +++++++++------------------------------------- 1 file changed, 22 insertions(+), 94 deletions(-) diff --git a/src/animations.rs b/src/animations.rs index 5166a95..908e0bc 100644 --- a/src/animations.rs +++ b/src/animations.rs @@ -282,22 +282,13 @@ fn add_animation_to_db( * */ #[get("/stop/")] -pub async fn stop_animation(name: &str, stop_sender: &State>) -> String { - let r = stop_sender.lock().unwrap().send(name.to_string()); - +pub async fn stop_animation(name: &str, action: &State>) -> String { + let r = action.lock().unwrap().send(Action::Stop(name.to_string())); if r.is_err() || r.ok().is_none() { - return json!(ApiResponse { - code: 500, - message: "Something went wrong".to_string() - }) - .to_string(); + return get_error_message("Something went wrong"); } - json!(ApiResponse { - code: 200, - message: "Configuration was successful".to_string() - }) - .to_string() + create_message(200, "Configuration was successful") } /** @@ -311,44 +302,18 @@ pub async fn start_animation( action: &State>, db: &State, ) -> String { - println!("try to get animation!"); - let animation = get_animation(name, db); - println!("{:?}!", animation); - - if animation.is_err() || animation.as_ref().ok().is_none() { - return json!(ApiResponse { - code: 500, - message: format!("Probelms with the db: {:?}", animation.err()).to_string() - }) - .to_string(); - } - - let animation = animation.ok().unwrap(); - - if animation.is_none() { - return json!(ApiResponse { - code: 404, - message: "Animation not found".to_string() - }) - .to_string(); - } - + let animation = get_animation_or_string_error(name, db); + if let Err(animation) = animation { + return animation; + } let animation = animation.unwrap(); let r = action.lock().unwrap().send(Action::Start(animation)); if r.is_err() || r.as_ref().ok().is_none() { - return json!(ApiResponse { - code: 500, - message: "Probelms with the sender".to_string() - }) - .to_string(); + return get_error_message("Probelms with the sender"); } - json!(ApiResponse { - code: 200, - message: "Started animation".to_string() - }) - .to_string() + create_message(200, "Started animation") } /** @@ -359,56 +324,27 @@ pub async fn start_animation( #[get("/clear/")] pub async fn clear( name: &str, - start_sender: &State>, db: &State, action: &State>, ) -> String { - let r = action.lock().unwrap().send(Action::Clear); - + let action = lock().unwrap(); + let r = action.send(Action::Clear); if r.is_err() || r.ok().is_none() { - return json!(ApiResponse { - code: 500, - message: "Something went wrong".to_string() - }) - .to_string(); - } - - let animation = get_animation(name, db); - - if animation.is_err() || animation.as_ref().ok().is_none() { - return json!(ApiResponse { - code: 500, - message: format!("Probelms with the db: {:?}", animation.err()).to_string() - }) - .to_string(); - } - - let animation = animation.ok().unwrap(); - - if animation.is_none() { - return json!(ApiResponse { - code: 404, - message: "Animation not found".to_string() - }) - .to_string(); + return get_error_message("Something went wrong"); } + let animation = get_animation_or_string_error(name, db); + if let Err(error) = animation { + return error; + } let animation = animation.unwrap(); - let r = start_sender.lock().unwrap().send(animation); + let r = action.send(Action::Start(animation)); if r.is_err() || r.as_ref().ok().is_none() { - return json!(ApiResponse { - code: 500, - message: "Probelms with the db".to_string() - }) - .to_string(); + return get_error_message("Probelms with the db"); } - json!(ApiResponse { - code: 200, - message: "Started animation".to_string() - }) - .to_string() + create_message(200, "Started animation") } #[get("/clearall")] @@ -416,18 +352,10 @@ pub async fn clear_all(action: &State>) -> String { let r = action.lock().unwrap().send(Action::Clear); if r.is_err() || r.ok().is_none() { - return json!(ApiResponse { - code: 500, - message: "Something went wrong".to_string() - }) - .to_string(); + return get_error_message("Something went wrong"); } - json!(ApiResponse { - code: 200, - message: "Cleared all the animations".to_string() - }) - .to_string() + create_message(200, "Cleared all the animations") } pub fn get_animation(name: &str, db: &State) -> Result, Box> {