chore: update configuration
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Andre Henriques 2023-07-09 20:13:25 +01:00
parent 5dda11f170
commit d9e9a33b3d

View File

@ -25,50 +25,17 @@ pub struct LightConfigurationRequest {
#[post("/configure", data= "<data>")]
pub async fn configure(data: Json<LightConfigurationRequest>, db: &State<DBPool>, action: &State<ASender<Action>>) -> 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<LightConfigurationRequest>, db: &State<DBPool>
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()
}