Some database thing, idk

This commit is contained in:
~erin 2022-03-31 22:50:25 -04:00
parent 81ab1ee5ef
commit b4856487b3
No known key found for this signature in database
GPG Key ID: DA70E064A8C70F44
2 changed files with 77 additions and 39 deletions

View File

@ -13,7 +13,6 @@ use argon2::{password_hash::SaltString, Argon2};
// Endpoint to add a new product
#[post("/api/new", data = "<request>")]
pub fn new_product(
db: &State<sled::Db>,
argon2: &State<Argon2>,
request: Form<ProductRequest>,
) -> Json<Status> {
@ -29,7 +28,7 @@ pub fn new_product(
stock: request.stock,
};
match register_product(db, &new_product.short_name, &new_product) {
match register_product(&new_product.short_name, &new_product) {
// Register the new product in the database
Ok(s) => info!("{}", s),
Err(error) => {
@ -227,7 +226,6 @@ pub fn delete_order(auth: Form<Authenticate>, order: &str, argon2: &State<Argon2
// Update fields of a product
#[post("/api/update", data = "<update>")]
pub fn update_product(
db: &State<sled::Db>,
argon2: &State<Argon2>,
update: Form<Update<'_>>,
) -> Json<Status> {
@ -235,7 +233,7 @@ pub fn update_product(
match update.field {
// Check what field is being updated
"price_usd" => {
let mut new_product = match read_product(db, &update.product) {
let mut new_product = match read_product(&update.product) {
// Read the product from the database
Ok(product) => product,
Err(error) => {
@ -247,7 +245,7 @@ pub fn update_product(
};
new_product.price_usd = update.value; // Change the price_usd value to the provided value
match register_product(db, &update.product, &new_product) {
match register_product(&update.product, &new_product) {
// Update the product in the database
Ok(s) => info!("{}", s),
Err(error) => {
@ -266,7 +264,7 @@ pub fn update_product(
}
"stock" => {
// Same as before, but with stock
let mut new_product = match read_product(db, &update.product) {
let mut new_product = match read_product(&update.product) {
Ok(product) => product,
Err(error) => {
return Json(Status {
@ -277,7 +275,7 @@ pub fn update_product(
};
new_product.stock = update.value as u32;
match register_product(db, &update.product, &new_product) {
match register_product(&update.product, &new_product) {
Ok(s) => info!("{}", s),
Err(error) => {
return Json(Status {
@ -310,8 +308,8 @@ pub fn update_product(
// Get all data about a product
#[get("/api/product/<product>")]
pub fn get_product_info(db: &State<sled::Db>, product: &str) -> Json<Product> {
return Json(match read_product(db, &product) {
pub fn get_product_info(product: &str) -> Json<Product> {
return Json(match read_product(&product) {
Ok(product) => product,
Err(error) => panic!("{:?}", error),
});

View File

@ -1,20 +1,54 @@
// File for database functions
use crate::structures::*;
// Open the products database
pub fn open() -> sled::Db {
match sled::open("products_db") {
Ok(database) => database,
Err(error) => panic!("error opening database: {:?}", error),
use std::env;
use std::fs::{File, OpenOptions};
use std::io::prelude::*;
use std::path::PathBuf;
// Open the database
pub fn open_database(keyspace: &str) -> Option<sled::Tree> {
let db: sled::Db;
let key = "HOME";
let database_path = match env::var(key) {
Ok(val) => {
let mut tmp_path = PathBuf::new();
tmp_path.push(val);
tmp_path.push(".local/share/catgirl-pharmacy");
tmp_path.push("database");
tmp_path
}
Err(e) => {
panic!("failed to get key: {}", e);
}
};
match sled::open(database_path) {
Ok(database) => db = database,
Err(error) => {
println!("error in opening database: {}", error);
return None;
}
}
let database: Option<sled::Tree> = match db.open_tree(keyspace) {
Ok(tree) => Some(tree),
Err(e) => {
println!("error in opening Tree: {}", e);
None
}
};
return database;
}
// Register a new product
pub fn register_product(
db: &sled::Db,
short_name: &str,
product: &Product,
) -> Result<String, sled::Error> {
pub fn register_product(short_name: &str, product: &Product) -> Result<String, sled::Error> {
let db = match open_database("products") {
Some(database) => database,
None => panic!(),
};
info!("Registering new product {:?}", &product);
let bytes = bincode::serialize(&product).unwrap(); //Serialize the data
@ -33,7 +67,12 @@ pub fn register_product(
}
// Read info about a product
pub fn read_product(db: &sled::Db, short_name: &str) -> Result<Product, sled::Error> {
pub fn read_product(short_name: &str) -> Result<Product, sled::Error> {
let db = match open_database("products") {
Some(database) => database,
None => panic!(),
};
info!("Reading product from shortcode {}", short_name);
let entry = match db.get(short_name) {
Ok(entry) => entry,
@ -60,10 +99,10 @@ pub fn make_order(order: &Order) -> Result<String, sled::Error> {
"Creating new order with Uuid {}",
&order.uuid.to_hyphenated().to_string()
);
let db = match sled::open("order_db") {
// Open the orders database
Ok(database) => database,
Err(error) => return Err(error),
let db = match open_database("orders") {
Some(database) => database,
None => panic!(),
};
let bytes = match bincode::serialize(&order) {
@ -74,7 +113,7 @@ pub fn make_order(order: &Order) -> Result<String, sled::Error> {
match db.insert(order.uuid.to_hyphenated().to_string(), bytes) {
// Insert using the UUID as key
Ok(_) => info!("succesfully inserted product"),
Ok(_) => info!("succesfully inserted order"),
Err(error) => return Err(error),
}
@ -87,13 +126,13 @@ pub fn make_order(order: &Order) -> Result<String, sled::Error> {
// Read an order from UUID
pub fn read_order(uuid: &str) -> Result<Order, sled::Error> {
info!("Reading order with Uuid {}", uuid);
let db = match sled::open("order_db") {
// Open the orders database
Ok(database) => database,
Err(error) => return Err(error),
let db = match open_database("orders") {
Some(database) => database,
None => panic!(),
};
info!("Reading order with Uuid {}", uuid);
let entry = match db.get(uuid) {
// Read an entry using UUID as a key
Ok(order) => order,
@ -111,7 +150,7 @@ pub fn read_order(uuid: &str) -> Result<Order, sled::Error> {
return Ok(read_order);
} else {
// Fail
warn!("Failed on reading order from orders_db with Uuid {}", uuid);
warn!("Failed on reading order from database with Uuid {}", uuid);
db.flush().unwrap();
return Err(sled::Error::ReportableBug("order not found".to_string()));
};
@ -119,11 +158,12 @@ pub fn read_order(uuid: &str) -> Result<Order, sled::Error> {
// Read all orders pushed to the database
pub fn read_all_orders() -> Result<Vec<Order>, sled::Error> {
info!("Reading all orders in orders_db");
let db = match sled::open("order_db") {
Ok(database) => database,
Err(error) => return Err(error),
}; // Open the orders database
let db = match open_database("orders") {
Some(database) => database,
None => panic!(),
};
info!("Reading all orders from database");
let first_key = match db.first() {
Ok(key) => key.unwrap().0,
@ -155,9 +195,9 @@ pub fn read_all_orders() -> Result<Vec<Order>, sled::Error> {
pub fn remove_order(uuid: &str) -> Result<String, sled::Error> {
info!("Removing order with Uuid {}", uuid);
let db = match sled::open("order_db") {
Ok(database) => database,
Err(error) => return Err(error),
let db = match open_database("orders") {
Some(database) => database,
None => panic!(),
};
match db.remove(uuid) {