From aecde207f96463f97e84e3dee03642688ce4a7c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bianca=20F=C3=BCrstenau?= Date: Thu, 20 Feb 2025 05:26:53 +0100 Subject: [PATCH] Progress towards inventory --- src-tauri/src/data_door.rs | 12 ++--- src-tauri/src/lib.rs | 89 +++++++++++++++++++++++++++++++++++--- src/styles.css | 9 +++- 3 files changed, 95 insertions(+), 15 deletions(-) diff --git a/src-tauri/src/data_door.rs b/src-tauri/src/data_door.rs index 9f8ce15..a96441e 100644 --- a/src-tauri/src/data_door.rs +++ b/src-tauri/src/data_door.rs @@ -83,7 +83,7 @@ fn put_client(file: &str, payload: &[u8]) -> Result, ()> { #[tauri::command] pub async fn pull_data( - state: State<'_, Mutex>, + _state: State<'_, Mutex>, ) -> Result { let mut client = data_client("")?; client.custom_request("PROPFIND").map_err(|_| ())?; @@ -95,8 +95,8 @@ pub async fn pull_data( async fn push_key(id: &u64, key: &SigningKey) -> Result<(), ()> { let file = format!("{:016X}.key", id); let v_key = key.verifying_key(); - let mut client = put_client(&file, v_key.as_ref())?; - let perf = client.perform() + let client = put_client(&file, v_key.as_ref())?; + let _perf = client.perform() .map_err(|e| println!("{:?}", e))?; Ok(()) } @@ -109,8 +109,8 @@ fn push_db(id: &u64, db: &Connection, app: tauri::AppHandle) -> Result<(), ()> { .map_err(|e| println!("{:?}", e))?; let buf = std::fs::read(&path) .map_err(|e| println!("{:?}", e))?; - let mut client = put_client(&filename, buf.as_ref())?; - let perf = client.perform() + let client = put_client(&filename, buf.as_ref())?; + let _perf = client.perform() .map_err(|e| println!("{:?}", e))?; Ok(()) } @@ -122,6 +122,6 @@ pub async fn push_data( ) -> Result<(), ()> { let state = state.lock().await; push_key(&state.id, &state.key).await?; - push_db(&state.id, &state.db, app); + push_db(&state.id, &state.db, app)?; Ok(()) } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index a45aa09..b21e1f0 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,6 +1,5 @@ use chrono::offset::Utc; -use rand::prelude::*; -use rusqlite::{types::ToSqlOutput, Connection, ToSql}; +use rusqlite::{types::ToSqlOutput, ToSql}; use tauri::{Manager, State}; use tauri_plugin_fs::FsExt; use tokio::sync::Mutex; @@ -11,7 +10,7 @@ mod data_door; use app_state::AppState; -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug)] enum Store { Aldi, Edeka, @@ -21,6 +20,24 @@ enum Store { Tegut, } +#[derive(Clone, Copy, Debug)] +enum Account { + Sumpf, + Heinersyndikat, +} + +struct Inventory { + acc: Account, + cash: i64, + vouchers: Vec, +} + +#[derive(Debug)] +struct VoucherInventory { + store: Store, + count: i64, +} + impl TryFrom<&str> for Store { type Error = (); @@ -37,6 +54,18 @@ impl TryFrom<&str> for Store { } } +impl TryFrom<&str> for Account { + type Error = (); + + fn try_from(s: &str) -> Result { + match s { + "sumpf" => Ok(Account::Sumpf), + "hs" => Ok(Account::Heinersyndikat), + _ => Err(()), + } + } +} + impl ToSql for Store { fn to_sql(&self) -> rusqlite::Result> { match self { @@ -50,12 +79,60 @@ impl ToSql for Store { } } +impl ToSql for Account { + fn to_sql(&self) -> rusqlite::Result> { + match self { + Account::Sumpf => 0.to_sql(), + Account::Heinersyndikat => 1.to_sql(), + } + } +} + +fn parse_inventory(data: HashMap) -> Result { + let a = data.get("cafe-inventory-acc").ok_or(())?; + let acc: Account = Account::try_from(a.as_ref())?; + let mut vouchers = Vec::new(); + for s in ["aldi", "dm", "lidl", "rewe", "tegut"] { + let Ok(store) = s.try_into() else { + println!("Did not find '{}' in inventory data.", s); + continue; + }; + match data.get(&format!("cafe-inventory-{}", s)) { + None => (), + Some(c) => { + let c = if c == "" {"0"} else {c}; + let Ok(count) = c.parse() else { + println!("Invalid count '{}' for '{}' in inventory data.", c, s); + continue; + }; + let v = VoucherInventory { store, count }; + vouchers.push(v); + }, + } + } + unimplemented!() +} + #[tauri::command] async fn inventory( data: HashMap, state: State<'_, Mutex>, ) -> Result<(), ()> { - println!("{:?}", data); + let now = Utc::now().timestamp(); + let state = state.lock().await; + let inv = parse_inventory(data)?; + for v in inv.vouchers { + state.db.execute( + "INSERT INTO voucher_inventory VALUES ()", + ( + inv.acc, + v.store, + v.count, + now, + ), + ) + .map_err(|e| println!("{:?}", e))?; + }; Ok(()) } @@ -66,12 +143,10 @@ async fn swap( state: State<'_, Mutex>, ) -> Result<(), ()> { let state = state.lock().await; - let mut rng = rand::thread_rng(); let store: Store = store.try_into()?; state.db.execute( - "INSERT INTO swap VALUES (?1, ?2, ?3, ?4, ?5, ?6)", + "INSERT INTO swap VALUES (?1, ?2, ?3, ?4, ?5)", ( - rng.gen::(), store, acc, i64::from_ne_bytes(state.id.to_ne_bytes()), diff --git a/src/styles.css b/src/styles.css index f86db57..d239b06 100644 --- a/src/styles.css +++ b/src/styles.css @@ -55,8 +55,6 @@ body { .logo { margin: auto; padding: 0em 0em; - will-change: filter; - transition: 0.75s; max-height: 100%; max-width: 100%; height: 100%; @@ -96,8 +94,15 @@ button { cursor: pointer; } +#cafe-inventory form button { + height: 1.5cm; + font-size: 7mm; +} + input[type=number] { border: 1px solid #eee; + text-align: right; + font-size: inherit; } input[type=radio] {