Progress towards inventory

This commit is contained in:
Bianca Fürstenau 2025-02-20 05:26:53 +01:00
parent 48ec66d64b
commit aecde207f9
3 changed files with 95 additions and 15 deletions

View file

@ -83,7 +83,7 @@ fn put_client(file: &str, payload: &[u8]) -> Result<Easy2<Collector>, ()> {
#[tauri::command]
pub async fn pull_data(
state: State<'_, Mutex<AppState>>,
_state: State<'_, Mutex<AppState>>,
) -> Result<String, ()> {
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(())
}

View file

@ -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<VoucherInventory>,
}
#[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<Self, Self::Error> {
match s {
"sumpf" => Ok(Account::Sumpf),
"hs" => Ok(Account::Heinersyndikat),
_ => Err(()),
}
}
}
impl ToSql for Store {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
match self {
@ -50,12 +79,60 @@ impl ToSql for Store {
}
}
impl ToSql for Account {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
match self {
Account::Sumpf => 0.to_sql(),
Account::Heinersyndikat => 1.to_sql(),
}
}
}
fn parse_inventory(data: HashMap<String, String>) -> Result<Inventory, ()> {
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<String, String>,
state: State<'_, Mutex<AppState>>,
) -> 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<AppState>>,
) -> 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::<i64>(),
store,
acc,
i64::from_ne_bytes(state.id.to_ne_bytes()),

View file

@ -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] {