chore: add toggle
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Andre Henriques 2023-07-02 17:00:14 +01:00
parent 199c6c7d4f
commit 390b30d084
2 changed files with 87 additions and 0 deletions

View File

@ -512,6 +512,30 @@ pub fn get_animation(name: &str, db: &State<DBPool>) -> Result<Option<Animation>
Ok(Some(animation.animation))
}
fn get_animation_or_string_error(name: &str, db: &State<DBPool>) -> Result<Animation, String> {
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<DBPool>) -> 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/<name>")]
pub async fn toggle_animation(
name: &str,
start_sender: &State<ASender<Animation>>,
action: &State<ASender<Action>>,
messages: &State<AReceiver<RenderMessage>>,
stop_sender: &State<ASender<String>>,
db: &State<DBPool>,
) -> 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");
}

View File

@ -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())