Something running

This commit is contained in:
Bianca Fürstenau 2025-02-22 07:09:34 +01:00
parent e03a16d13e
commit 49a9810fd8
19 changed files with 173 additions and 132 deletions

View file

@ -1,5 +1,35 @@
use leptos::prelude::*;
use bkbh_lib::commands::*;
use leptos::task::spawn_local;
use bkbh_lib::types::*;
#[component]
fn SwapButton(store: Store, img_path: &'static str, pretty: &'static str) -> impl IntoView {
view! {
<button
on:click=move |_| {
spawn_local(async {
swap("aldi", 0).await.unwrap();
})
}
>
<img
src="assets/aldi.svg"
class="logo"
alt="ALDI-Süd"
/>
</button>
}
}
fn main() {
leptos::mount::mount_to_body(|| view! { <p>"Hello, world!"</p> })
console_error_panic_hook::set_once();
leptos::mount::mount_to_body(|| view! {
<SwapButton
store=Store::Aldi
img_path=&"aldi"
pretty=&"Aldi"
/>
});
}

View file

@ -1,5 +1,5 @@
use tauri_sys::Error;
use tauri_sys::tauri::invoke;
use tauri_sys::core::invoke;
#[derive(serde::Serialize)]
pub struct Swap<'a> {

View file

@ -1,9 +1,33 @@
mod commands;
pub mod commands;
#[cfg(feature = "server")]
mod server;
pub mod server;
pub mod types;
#[cfg(feature = "server")]
#[cfg(all(feature = "tauri", feature="server"))]
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
server::run();
use tauri::{Manager, State};
use tauri_plugin_fs::FsExt;
use server::app_state::AppState;
use tokio::sync::Mutex;
let state = AppState::new();
tauri::Builder::default()
.plugin(tauri_plugin_fs::init())
.setup(|app| {
app.manage(Mutex::new(state));
let scope = app.fs_scope();
let path = app.path();
scope.allow_directory(path.temp_dir()?, false)?;
Ok(())
})
.invoke_handler(tauri::generate_handler![
server::swap,
server::count,
server::inventory,
server::data_door::pull_data,
server::data_door::push_data,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

View file

@ -1,93 +1,16 @@
use chrono::offset::Utc;
use rusqlite::{types::ToSqlOutput, ToSql};
use tauri::{Manager, State};
use tauri_plugin_fs::FsExt;
use tokio::sync::Mutex;
use std::collections::HashMap;
use tokio::sync::Mutex;
use tauri::{Manager, State};
mod app_state;
mod data_door;
use crate::types::*;
pub mod app_state;
pub mod data_door;
use app_state::AppState;
#[derive(Clone, Copy, Debug)]
enum Store {
Aldi,
Edeka,
Dm,
Lidl,
Rewe,
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 = ();
fn try_from(s: &str) -> Result<Self, Self::Error> {
match s {
"aldi" => Ok(Store::Aldi),
"edeka" => Ok(Store::Edeka),
"dm" => Ok(Store::Dm),
"lidl" => Ok(Store::Lidl),
"rewe" => Ok(Store::Rewe),
"tegut" => Ok(Store::Tegut),
_ => Err(()),
}
}
}
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 {
Store::Aldi => 0.to_sql(),
Store::Edeka => 1.to_sql(),
Store::Dm => 2.to_sql(),
Store::Lidl => 3.to_sql(),
Store::Rewe => 4.to_sql(),
Store::Tegut => 5.to_sql(),
}
}
}
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())?;
@ -114,7 +37,7 @@ fn parse_inventory(data: HashMap<String, String>) -> Result<Inventory, ()> {
}
#[tauri::command]
async fn inventory(
pub async fn inventory(
data: HashMap<String, String>,
state: State<'_, Mutex<AppState>>,
) -> Result<(), ()> {
@ -137,7 +60,7 @@ async fn inventory(
}
#[tauri::command]
async fn swap(
pub async fn swap(
store: &str,
acc: i64,
state: State<'_, Mutex<AppState>>,
@ -159,7 +82,7 @@ async fn swap(
}
#[tauri::command]
async fn count(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
pub async fn count(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
let state = state.lock().await;
let mut stmt =
state.db.prepare("SELECT COUNT(*) FROM swap")
@ -176,25 +99,3 @@ async fn count(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
let cnt: u64 = row.get(0).map_err(|e| println!("{:?}", e))?;
Ok(cnt.to_string())
}
pub fn run() {
let state = AppState::new();
tauri::Builder::default()
.plugin(tauri_plugin_fs::init())
.setup(|app| {
app.manage(Mutex::new(state));
let scope = app.fs_scope();
let path = app.path();
scope.allow_directory(path.temp_dir()?, false)?;
Ok(())
})
.invoke_handler(tauri::generate_handler![
swap,
count,
inventory,
data_door::pull_data,
data_door::push_data,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

84
src/types.rs Normal file
View file

@ -0,0 +1,84 @@
#[cfg(feature = "server")]
use rusqlite::{types::ToSqlOutput, ToSql};
use serde::{Serialize, Deserialize};
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum Store {
Aldi,
Edeka,
Dm,
Lidl,
Rewe,
Tegut,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum Account {
Sumpf,
Heinersyndikat,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Inventory {
pub acc: Account,
pub cash: i64,
pub vouchers: Vec<VoucherInventory>,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct VoucherInventory {
pub store: Store,
pub count: i64,
}
impl TryFrom<&str> for Store {
type Error = ();
fn try_from(s: &str) -> Result<Self, Self::Error> {
match s {
"aldi" => Ok(Store::Aldi),
"edeka" => Ok(Store::Edeka),
"dm" => Ok(Store::Dm),
"lidl" => Ok(Store::Lidl),
"rewe" => Ok(Store::Rewe),
"tegut" => Ok(Store::Tegut),
_ => Err(()),
}
}
}
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(()),
}
}
}
#[cfg(feature = "server")]
impl ToSql for Store {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
match self {
Store::Aldi => 0.to_sql(),
Store::Edeka => 1.to_sql(),
Store::Dm => 2.to_sql(),
Store::Lidl => 3.to_sql(),
Store::Rewe => 4.to_sql(),
Store::Tegut => 5.to_sql(),
}
}
}
#[cfg(feature = "server")]
impl ToSql for Account {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
match self {
Account::Sumpf => 0.to_sql(),
Account::Heinersyndikat => 1.to_sql(),
}
}
}