From 390b30d0844a5fa09c109c9fd827ba4e81cb2bc8 Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Sun, 2 Jul 2023 17:00:14 +0100 Subject: [PATCH] chore: add toggle --- src/animations.rs | 86 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + 2 files changed, 87 insertions(+) diff --git a/src/animations.rs b/src/animations.rs index 7c492fd..521e9ca 100644 --- a/src/animations.rs +++ b/src/animations.rs @@ -512,6 +512,30 @@ pub fn get_animation(name: &str, db: &State) -> Result Ok(Some(animation.animation)) } +fn get_animation_or_string_error(name: &str, db: &State) -> Result { + let animation = get_animation(name, db); + + if animation.is_err() || animation.as_ref().ok().is_none() { + return Err(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 Err(json!(ApiResponse { + code: 404, + message: "Animation not found".to_string() + }) + .to_string()); + } + + Ok(animation.unwrap()) +} + /** * * Delete animations @@ -617,3 +641,65 @@ pub async fn get_animation_request(name: &str, db: &State) -> String { .to_string(); } } + +fn create_message(code: u32, message: &str) -> String { + return json!(ApiResponse {code, message: message.to_string()}).to_string(); +} + +fn get_error_message(message: &str) -> String { + return create_message(500, message); +} + +/** + * + * Start animations + * + */ +#[get("/toggle/")] +pub async fn toggle_animation( + name: &str, + start_sender: &State>, + action: &State>, + messages: &State>, + stop_sender: &State>, + db: &State, +) -> String { + let animation = get_animation_or_string_error(name, db); + if let Err(error) = animation { + return error; + } + let animation = animation.unwrap(); + + let send = action.lock().unwrap().send(Action::GetActiveList); + if send.is_err() || send.ok().is_none() { + return get_error_message("Failed to get list"); + } + + let data = messages.lock().unwrap().recv(); + if data.is_err() || data.as_ref().ok().is_none() { + return get_error_message("Failed to get list"); + } + + if let RenderMessage::ActiveList(list) = data.ok().unwrap() { + if list.contains(&name.to_string()) { + // Disable + let r = stop_sender.lock().unwrap().send(name.to_string()); + + if r.is_err() || r.ok().is_none() { + return get_error_message("Something went wrong"); + } + + return create_message(200, "Configuration was successful"); + } else { + // Enable + let r = start_sender.lock().unwrap().send(animation); + if r.is_err() || r.as_ref().ok().is_none() { + return get_error_message("Probelms with the sender"); + } + + return create_message(200, "Started animation"); + } + } + + return get_error_message("Failed to get list"); +} diff --git a/src/main.rs b/src/main.rs index 2c33700..d879ecf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -222,6 +222,7 @@ async fn main() -> GResult { animations::delete, animations::clear_all, animations::get_animation_request, + animations::toggle_animation, ], ) .manage(conn_pool_arc.clone())