168 lines
5.3 KiB
Rust
168 lines
5.3 KiB
Rust
// 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),
|
|
}
|
|
}
|
|
|
|
// Register a new product
|
|
pub fn register_product(
|
|
db: &sled::Db,
|
|
short_name: &str,
|
|
product: &Product,
|
|
) -> Result<String, sled::Error> {
|
|
info!("Registering new product {:?}", &product);
|
|
let bytes = bincode::serialize(&product).unwrap(); //Serialize the data
|
|
|
|
match db.insert(short_name, bytes) {
|
|
// Insert the product using the short name as a key
|
|
Ok(_) => info!("inserted product"),
|
|
Err(error) => return Err(error),
|
|
}
|
|
|
|
match db.flush() {
|
|
Ok(_) => info!("flushed database"),
|
|
Err(error) => return Err(error),
|
|
}
|
|
|
|
return Ok("succesfully registered product".to_string());
|
|
}
|
|
|
|
// Read info about a product
|
|
pub fn read_product(db: &sled::Db, short_name: &str) -> Result<Product, sled::Error> {
|
|
info!("Reading product from shortcode {}", short_name);
|
|
let entry = match db.get(short_name) {
|
|
Ok(entry) => entry,
|
|
Err(error) => return Err(error),
|
|
}; // Get a product entry out of the database using the short name
|
|
|
|
if let Some(product_entry) = entry {
|
|
let read_product = match bincode::deserialize(&product_entry) {
|
|
Ok(product) => product,
|
|
Err(error) => panic!("could not deserialize product: {:?}", error),
|
|
};
|
|
return Ok(read_product);
|
|
} else {
|
|
warn!("product not found");
|
|
return Err(sled::Error::Unsupported(
|
|
sled::Error::ReportableBug("product not found".to_string()).to_string(),
|
|
));
|
|
};
|
|
}
|
|
|
|
// Create a new order
|
|
pub fn make_order(order: &Order) -> Result<String, sled::Error> {
|
|
info!(
|
|
"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 bytes = match bincode::serialize(&order) {
|
|
// Serialize the new order
|
|
Ok(bytes) => bytes,
|
|
Err(error) => panic!("could not serialize order: {:?}", error),
|
|
};
|
|
|
|
match db.insert(order.uuid.to_hyphenated().to_string(), bytes) {
|
|
// Insert using the UUID as key
|
|
Ok(_) => info!("succesfully inserted product"),
|
|
Err(error) => return Err(error),
|
|
}
|
|
|
|
match db.flush() {
|
|
Ok(_) => info!("flushed database"),
|
|
Err(error) => return Err(error),
|
|
}
|
|
return Ok("succesfully made new order".to_string());
|
|
}
|
|
|
|
// 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 entry = match db.get(uuid) {
|
|
// Read an entry using UUID as a key
|
|
Ok(order) => order,
|
|
Err(error) => return Err(error),
|
|
};
|
|
|
|
if let Some(order_entry) = entry {
|
|
let read_order: Order = match bincode::deserialize(&order_entry) {
|
|
// If succesful, deserialize & return
|
|
Ok(order) => order,
|
|
Err(error) => panic!("could not deserialize order: {:?}", error),
|
|
};
|
|
info!("Succesfully deserialized order");
|
|
db.flush().unwrap();
|
|
return Ok(read_order);
|
|
} else {
|
|
// Fail
|
|
warn!("Failed on reading order from orders_db with Uuid {}", uuid);
|
|
db.flush().unwrap();
|
|
return Err(sled::Error::ReportableBug("order not found".to_string()));
|
|
};
|
|
}
|
|
|
|
// 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 first_key = match db.first() {
|
|
Ok(key) => key.unwrap().0,
|
|
Err(error) => return Err(error),
|
|
}; // Get the first key from the database
|
|
|
|
let iter = db.range(first_key..); // Create an iterator of all keys from the first one
|
|
let mut all_orders: Vec<Order> = Vec::new(); // Create a new vector to store the orders
|
|
|
|
for order in iter {
|
|
// Iterate over the orders
|
|
if let order_entry = order.unwrap().1 {
|
|
// If we can unwrap it:
|
|
let read_order: Order = match bincode::deserialize(&order_entry) {
|
|
Ok(order) => order,
|
|
Err(error) => panic!("could not deserialize order: {:?}", error),
|
|
}; // Deserialize the entry
|
|
all_orders.push(read_order); // Add the entry to the vector
|
|
} else {
|
|
// Fail
|
|
warn!("Could not find any more orders");
|
|
db.flush().unwrap();
|
|
}
|
|
}
|
|
return Ok(all_orders); // Return the orders
|
|
}
|
|
|
|
// Remove an order from the database
|
|
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),
|
|
};
|
|
|
|
match db.remove(uuid) {
|
|
Ok(_) => Ok("removed order".to_string()),
|
|
Err(error) => return Err(error),
|
|
}
|
|
}
|