persist vendor mode to lmdb

This commit is contained in:
creating2morrow 2023-06-07 22:50:13 -04:00
parent 0d3539b8c4
commit eb806fa36e
3 changed files with 62 additions and 33 deletions

View file

@ -18,6 +18,8 @@ use std::error::Error;
/// Environment variable for activating vendor functionality /// Environment variable for activating vendor functionality
pub const NEVEKO_VENDOR_ENABLED: &str = "NEVEKO_VENDOR_ENABLED"; pub const NEVEKO_VENDOR_ENABLED: &str = "NEVEKO_VENDOR_ENABLED";
pub const NEVEKO_VENDOR_MODE_OFF: &str = "0";
pub const NEVEKO_VENDOR_MODE_ON: &str = "1";
/// Create a new contact /// Create a new contact
pub async fn create(c: &Json<Contact>) -> Contact { pub async fn create(c: &Json<Contact>) -> Contact {
@ -104,8 +106,9 @@ async fn validate_contact(j: &Json<Contact>) -> bool {
/// Send our information /// Send our information
pub async fn share() -> Contact { pub async fn share() -> Contact {
let vendor_env = std::env::var(NEVEKO_VENDOR_ENABLED).unwrap_or(String::from("0")); let s = db::Interface::async_open().await;
let is_vendor = vendor_env == String::from("1"); let r = db::Interface::async_read(&s.env, &s.handle, NEVEKO_VENDOR_ENABLED).await;
let is_vendor = r == NEVEKO_VENDOR_MODE_ON;
let wallet_name = String::from(crate::APP_NAME); let wallet_name = String::from(crate::APP_NAME);
let wallet_password = let wallet_password =
std::env::var(crate::MONERO_WALLET_PASSWORD).unwrap_or(String::from("password")); std::env::var(crate::MONERO_WALLET_PASSWORD).unwrap_or(String::from("password"));

View file

@ -237,7 +237,7 @@ pub fn product_to_json(m: &models::Product) -> Json<models::Product> {
image: m.image.iter().cloned().collect(), image: m.image.iter().cloned().collect(),
in_stock: m.in_stock, in_stock: m.in_stock,
name: String::from(&m.name), name: String::from(&m.name),
price: m.price, price: m.price,
qty: m.qty, qty: m.qty,
}; };
Json(r_message) Json(r_message)
@ -675,16 +675,25 @@ pub async fn can_transfer(invoice: u128) -> bool {
/// Gui toggle for vendor mode /// Gui toggle for vendor mode
pub fn toggle_vendor_enabled() -> bool { pub fn toggle_vendor_enabled() -> bool {
let off: &str = "0"; let s = db::Interface::open();
let on: &str = "1"; let r = db::Interface::read(&s.env, &s.handle, contact::NEVEKO_VENDOR_ENABLED);
let vendor_env = std::env::var(contact::NEVEKO_VENDOR_ENABLED).unwrap_or(String::from(off)); if r != contact::NEVEKO_VENDOR_MODE_ON {
if vendor_env == off {
info!("neveko vendor mode enabled"); info!("neveko vendor mode enabled");
std::env::set_var(contact::NEVEKO_VENDOR_ENABLED, on); db::Interface::write(
&s.env,
&s.handle,
contact::NEVEKO_VENDOR_ENABLED,
contact::NEVEKO_VENDOR_MODE_ON,
);
return true; return true;
} else { } else {
info!("neveko vendor mode disabled"); info!("neveko vendor mode disabled");
std::env::set_var(contact::NEVEKO_VENDOR_ENABLED, off); db::Interface::write(
&s.env,
&s.handle,
contact::NEVEKO_VENDOR_ENABLED,
contact::NEVEKO_VENDOR_MODE_OFF,
);
return false; return false;
} }
} }

View file

@ -29,15 +29,22 @@ impl Default for MarketApp {
let (_refresh_on_delete_product_tx, _refresh_on_delete_product_rx) = let (_refresh_on_delete_product_tx, _refresh_on_delete_product_rx) =
std::sync::mpsc::channel(); std::sync::mpsc::channel();
let read_product_image = std::fs::read("./assets/qr.png").unwrap_or(Vec::new()); let read_product_image = std::fs::read("./assets/qr.png").unwrap_or(Vec::new());
let s = db::Interface::open();
let r = db::Interface::read(&s.env, &s.handle, contact::NEVEKO_VENDOR_ENABLED);
let is_vendor_enabled = r == contact::NEVEKO_VENDOR_MODE_ON;
MarketApp { MarketApp {
is_product_image_set: false, is_product_image_set: false,
is_showing_orders: false, is_showing_orders: false,
is_showing_products: false, is_showing_products: false,
is_showing_product_image: false, is_showing_product_image: false,
is_showing_product_update: false, is_showing_product_update: false,
is_vendor_enabled: false, is_vendor_enabled,
orders: Vec::new(), orders: Vec::new(),
product_image: egui_extras::RetainedImage::from_image_bytes("qr.png", &read_product_image).unwrap(), product_image: egui_extras::RetainedImage::from_image_bytes(
"qr.png",
&read_product_image,
)
.unwrap(),
products: Vec::new(), products: Vec::new(),
product_update_pid: utils::empty_string(), product_update_pid: utils::empty_string(),
new_product_image: utils::empty_string(), new_product_image: utils::empty_string(),
@ -68,10 +75,12 @@ impl eframe::App for MarketApp {
self.is_showing_product_image = false; self.is_showing_product_image = false;
self.is_product_image_set = false; self.is_product_image_set = false;
let read_product_image = std::fs::read("./assets/qr.png").unwrap_or(Vec::new()); let read_product_image = std::fs::read("./assets/qr.png").unwrap_or(Vec::new());
self.product_image = egui_extras::RetainedImage::from_image_bytes("qr.png", &read_product_image).unwrap(); self.product_image =
egui_extras::RetainedImage::from_image_bytes("qr.png", &read_product_image)
.unwrap();
} }
}); });
// Update Product window // Update Product window
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
let mut is_showing_product_update = self.is_showing_product_update; let mut is_showing_product_update = self.is_showing_product_update;
@ -123,10 +132,10 @@ impl eframe::App for MarketApp {
}; };
let product: models::Product = models::Product { let product: models::Product = models::Product {
pid: self.product_update_pid.clone(), pid: self.product_update_pid.clone(),
description: self.new_product_desc.clone(), description: self.new_product_desc.clone(),
image, image,
in_stock: qty > 0, in_stock: qty > 0,
name: self.new_product_name.clone(), name: self.new_product_name.clone(),
price, price,
qty, qty,
}; };
@ -218,20 +227,28 @@ impl eframe::App for MarketApp {
self.is_showing_product_image = true; self.is_showing_product_image = true;
let file_path = format!( let file_path = format!(
"/home/{}/.neveko/{}.jpeg", "/home/{}/.neveko/{}.jpeg",
std::env::var("USER").unwrap_or(String::from("user")), std::env::var("USER")
.unwrap_or(String::from("user")),
p.pid p.pid
); );
// For the sake of brevity product list doesn't have image bytes, get them // For the sake of brevity product list doesn't have
// image bytes, get them
let i_product = product::find(&p.pid); let i_product = product::find(&p.pid);
match std::fs::write(&file_path, &i_product.image) { match std::fs::write(&file_path, &i_product.image) {
Ok(w) => w, Ok(w) => w,
Err(_) => log::error!("failed to write product image") Err(_) => {
log::error!("failed to write product image")
}
}; };
self.is_product_image_set = true; self.is_product_image_set = true;
let contents = std::fs::read(&file_path).unwrap_or(Vec::new()); let contents =
std::fs::read(&file_path).unwrap_or(Vec::new());
if !i_product.image.is_empty() { if !i_product.image.is_empty() {
self.product_image = self.product_image =
egui_extras::RetainedImage::from_image_bytes(file_path, &contents).unwrap(); egui_extras::RetainedImage::from_image_bytes(
file_path, &contents,
)
.unwrap();
} }
self.is_product_image_set = true; self.is_product_image_set = true;
ctx.request_repaint(); ctx.request_repaint();
@ -242,21 +259,21 @@ impl eframe::App for MarketApp {
ui.style_mut().wrap = Some(false); ui.style_mut().wrap = Some(false);
ui.horizontal(|ui| { ui.horizontal(|ui| {
if ui.button("Update").clicked() { if ui.button("Update").clicked() {
self.product_update_pid = p.pid.clone(); self.product_update_pid = p.pid.clone();
self.new_product_desc = p.description.clone(); self.new_product_desc = p.description.clone();
self.new_product_name = p.name.clone(); self.new_product_name = p.name.clone();
self.new_product_price = format!("{}", p.price); self.new_product_price = format!("{}", p.price);
self.new_product_qty = format!("{}", p.qty); self.new_product_qty = format!("{}", p.qty);
self.is_showing_product_update = true; self.is_showing_product_update = true;
} }
}); });
}); });
}); });
} }
}); });
if ui.button("Exit").clicked() { if ui.button("Exit").clicked() {
self.is_showing_products = false; self.is_showing_products = false;
} }
}); });
// TODO(c2m): Orders window // TODO(c2m): Orders window
@ -343,11 +360,11 @@ impl eframe::App for MarketApp {
} }
}); });
if ui.button("View Vendors").clicked() { if ui.button("View Vendors").clicked() {
// TODO(c2m):
} }
ui.label("\n"); ui.label("\n");
if ui.button("View Orders").clicked() { if ui.button("View Orders").clicked() {
// TODO(c2m):
} }
if self.is_vendor_enabled { if self.is_vendor_enabled {
ui.label("\n"); ui.label("\n");
@ -395,10 +412,10 @@ impl eframe::App for MarketApp {
}; };
let product: models::Product = models::Product { let product: models::Product = models::Product {
pid: utils::empty_string(), pid: utils::empty_string(),
description: self.new_product_desc.clone(), description: self.new_product_desc.clone(),
image, image,
in_stock: qty > 0, in_stock: qty > 0,
name: self.new_product_name.clone(), name: self.new_product_name.clone(),
price, price,
qty, qty,
}; };