From 1c8aba9066d9d6cc9c661402c711af0b0a424c57 Mon Sep 17 00:00:00 2001 From: ultrageese Date: Sun, 21 Jun 2026 00:04:21 +1000 Subject: [PATCH] =?UTF-8?q?=D0=BE=D0=BD=D0=BE=20=D0=B2=D1=8B=D0=B4=D0=B0?= =?UTF-8?q?=D1=91=D1=82=20=D1=81=D1=82=D1=80=D0=B0=D0=BD=D0=BD=D1=83=D1=8E?= =?UTF-8?q?=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D1=83,=20=D0=B0=20=D1=83=20?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D1=8F=20=D0=BD=D0=B5=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=B5=D1=82=20=D0=B3=D0=BE=D0=BB=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0.=20=D1=8F=20=20=D1=81=D0=BF=D0=B0=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/src/app.rs | 102 ++++++++++++++++++++++++++++++++++++++++++- code/src/database.rs | 26 +++++++++++ 2 files changed, 126 insertions(+), 2 deletions(-) diff --git a/code/src/app.rs b/code/src/app.rs index 6c84ddc..a017244 100644 --- a/code/src/app.rs +++ b/code/src/app.rs @@ -494,8 +494,106 @@ impl MainTabViewer { }; if ui.button(btext).clicked() { match self.equipment_modal_state.status { - ModalStatus::Add => {} - ModalStatus::Edit => {} + ModalStatus::Add => self.rt.block_on(async { + self.db_oper + .add_equipment(Equipment { + id: 0, + name: self + .equipment_modal_state + .data + .get("name") + .unwrap_or(&String::new()) + .clone(), + inv_number: self + .equipment_modal_state + .data + .get("inv_number") + .unwrap_or(&String::new()) + .clone(), + maintenance_date: chrono::NaiveDate::from_str( + self.equipment_modal_state + .data + .get("maintenance_date") + .unwrap_or(&"1970.01.01".to_owned()), + ) + .unwrap() + .and_hms_opt(0, 0, 0) + .unwrap() + .and_local_timezone(chrono::Local) + .unwrap(), + worker: self + .workers + .read() + .clone() + .iter() + .find(|w| { + &w.id.to_string() + == self + .equipment_modal_state + .data + .get("worker_id") + .unwrap_or(&"0".to_owned()) + }) + .unwrap() + .clone(), + is_written_off: false, + }) + .await + .unwrap(); + }), + ModalStatus::Edit => self.rt.block_on(async { + self.db_oper + .update_equipment(Equipment { + id: self + .equipment_modal_state + .data + .get("id") + .unwrap() + .parse::() + .unwrap(), + name: self + .equipment_modal_state + .data + .get("name") + .unwrap_or(&String::new()) + .clone(), + inv_number: self + .equipment_modal_state + .data + .get("inv_number") + .unwrap_or(&String::new()) + .clone(), + maintenance_date: chrono::NaiveDate::from_str( + self.equipment_modal_state + .data + .get("maintenance_date") + .unwrap_or(&"1970.01.01".to_owned()), + ) + .unwrap() + .and_hms_opt(0, 0, 0) + .unwrap() + .and_local_timezone(chrono::Local) + .unwrap(), + worker: self + .workers + .read() + .clone() + .iter() + .find(|w| { + &w.id.to_string() + == self + .equipment_modal_state + .data + .get("worker_id") + .unwrap_or(&"0".to_owned()) + }) + .unwrap() + .clone(), + is_written_off: false, + }) + .await + .unwrap(); + }), ModalStatus::Remove => { self.rt.block_on(async { self.db_oper diff --git a/code/src/database.rs b/code/src/database.rs index e48bc2e..90a5db7 100644 --- a/code/src/database.rs +++ b/code/src/database.rs @@ -78,6 +78,32 @@ impl DBOperator { } Ok(rets) } + pub async fn add_equipment(&self, equipment: Equipment) -> Result { + match sqlx::query("INSERT INTO `equipment` (id, inv_number, maintenance_date, worker_id, name, is_written_off) VALUES(0, ?, ?, ?, ?, 0);") + .bind(equipment.inv_number) + .bind(equipment.maintenance_date) + .bind(equipment.worker.id) + .bind(equipment.name) + .execute(&self.pool) + .await{ + Ok(_) => Ok(true), + Err(_) => Ok(false) + } + } + pub async fn update_equipment(&self, equipment: Equipment) -> Result { + match sqlx::query("UPDATE Brewery.equipment SET inv_number=?, maintenance_date=?, worker_id=?, name=?, is_written_off=? WHERE id=?;") + .bind(equipment.inv_number) + .bind(equipment.maintenance_date) + .bind(equipment.worker.id) + .bind(equipment.name) + .bind(equipment.is_written_off) + .bind(equipment.id) + .execute(&self.pool) + .await{ + Ok(_) => Ok(true), + Err(_) => Ok(false) + } + } pub async fn get_mcat(&self) -> Result, sqlx::Error> { let rets = sqlx::query_as::<_, MaterialCategory>("SELECT * FROM `material_category`") .fetch_all(&self.pool)