From d9e9a33b3d2b176c09f90cbced83a1e44768f25d Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Sun, 9 Jul 2023 20:13:25 +0100 Subject: [PATCH] chore: update configuration --- src/configure.rs | 54 ++++++++++++------------------------------------ 1 file changed, 13 insertions(+), 41 deletions(-) diff --git a/src/configure.rs b/src/configure.rs index 2a85765..4bbac87 100644 --- a/src/configure.rs +++ b/src/configure.rs @@ -25,50 +25,17 @@ pub struct LightConfigurationRequest { #[post("/configure", data= "")] pub async fn configure(data: Json, db: &State, action: &State>) -> String { - - let conn = db.get(); - - if conn.is_err() || conn.as_ref().ok().is_none() { - return json!(ApiResponse {code: 500, message: "Clould not configure. Err: 1".to_string()}).to_string(); + let conn = get_conn(db); + if let Err(e) = conn { + return e; } + let conn = conn.unwrap(); - let conn = conn.ok().unwrap(); - - let stmt = conn.prepare("SELECT configured from meta"); - - if stmt.is_err() || stmt.as_ref().ok().is_none() { - return json!(ApiResponse {code: 500, message: format!("Clould not configure. Err: 2, {:?}", stmt)}).to_string(); + let recreate = is_configured(&conn); + if let Err(e) = recreate { + return e; } - - let mut stmt = stmt.ok().unwrap(); - - struct Data { - configured: bool, - } - - let version_iter = stmt.query_map([], |row| { - Ok(Data { - configured: row.get(0)? - }) - }); - - if version_iter.is_err() || version_iter.as_ref().ok().is_none() { - return json!(ApiResponse {code: 500, message: "Clould not configure. Err: 3".to_string()}).to_string(); - } - - let version_iter = version_iter.unwrap(); - - let mut recreate = false; - - for data in version_iter { - if data.unwrap().configured { - println!("Already configured deleting current"); - recreate = true; - break; - } - } - - drop(stmt); + let recreate = recreate.unwrap(); if recreate { let stmt = conn.execute("DELETE FROM configuration;", ()); @@ -99,6 +66,11 @@ pub async fn configure(data: Json, db: &State if send.is_err() || send.ok().is_none() { return json!(ApiResponse {code: 500, message: "Failed to reload".to_string()}).to_string() } + + let stmt = conn.execute("UPDATE meta set configured=1 where id=1;", ()); + if stmt.is_err() || stmt.as_ref().ok().is_none() { + return json!(ApiResponse {code: 500, message: format!("Clould not configure. Err: 5, {:?}", stmt)}).to_string(); + } json!(ApiResponse {code: 200, message: "Configuration was successful".to_string()}).to_string() }