219 lines
6.5 KiB
Rust
219 lines
6.5 KiB
Rust
use chrono::{DateTime, Duration, Utc};
|
|
use herodb::db::{DB, DBBuilder};
|
|
use herodb::models::biz::{
|
|
Currency, CurrencyBuilder, Product, ProductBuilder, ProductComponent, ProductComponentBuilder,
|
|
ProductStatus, ProductType, Sale, SaleBuilder, SaleItem, SaleItemBuilder, SaleStatus,
|
|
};
|
|
use rhai::{Engine, packages::Package};
|
|
use std::fs;
|
|
use std::path::PathBuf;
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
// Note: This example has been modified to work with the current API
|
|
println!("DB Example 2: Using Builder Pattern and Model-Specific Methods");
|
|
println!("============================================================");
|
|
|
|
// Create a temporary directory for the database
|
|
let db_path = PathBuf::from("/tmp/dbexample_prod");
|
|
if db_path.exists() {
|
|
fs::remove_dir_all(&db_path)?;
|
|
}
|
|
fs::create_dir_all(&db_path)?;
|
|
println!("Database path: {:?}", db_path);
|
|
|
|
// Skip the Rhai engine part as it has compatibility issues
|
|
println!("\nSkipping Rhai engine part due to compatibility issues");
|
|
|
|
// Create a database instance with our models registered
|
|
let mut db = DBBuilder::new(&db_path)
|
|
.register_model::<Product>()
|
|
.register_model::<Currency>()
|
|
.register_model::<Sale>()
|
|
.build()?;
|
|
|
|
println!("\n1. Creating Products with Builder Pattern");
|
|
println!("----------------------------------------");
|
|
|
|
// Create a currency using the builder
|
|
let usd = CurrencyBuilder::new()
|
|
.id(1) // Add the required ID
|
|
.amount(0.0) // Initial amount
|
|
.currency_code("USD")
|
|
.build()?;
|
|
|
|
// Insert the currency
|
|
db.insert_currency(usd.clone())?;
|
|
println!("Currency created: ${:.2} {}", usd.amount, usd.currency_code);
|
|
|
|
// Create product components using the builder
|
|
let component1 = ProductComponentBuilder::new()
|
|
.id(101)
|
|
.name("Basic Support")
|
|
.description("24/7 email support")
|
|
.quantity(1)
|
|
.build()?;
|
|
|
|
let component2 = ProductComponentBuilder::new()
|
|
.id(102)
|
|
.name("Premium Support")
|
|
.description("24/7 phone and email support")
|
|
.quantity(1)
|
|
.build()?;
|
|
|
|
// Create products using the builder
|
|
let product1 = ProductBuilder::new()
|
|
.id(1)
|
|
.name("Standard Plan")
|
|
.description("Our standard service offering")
|
|
.price(
|
|
CurrencyBuilder::new()
|
|
.id(2) // Add ID
|
|
.amount(29.99)
|
|
.currency_code("USD")
|
|
.build()?,
|
|
)
|
|
.type_(ProductType::Service)
|
|
.category("Subscription")
|
|
.status(ProductStatus::Available)
|
|
.max_amount(1000)
|
|
.validity_days(30)
|
|
.add_component(component1)
|
|
.build()?;
|
|
|
|
let product2 = ProductBuilder::new()
|
|
.id(2)
|
|
.name("Premium Plan")
|
|
.description("Our premium service offering with priority support")
|
|
.price(
|
|
CurrencyBuilder::new()
|
|
.id(3) // Add ID
|
|
.amount(99.99)
|
|
.currency_code("USD")
|
|
.build()?,
|
|
)
|
|
.type_(ProductType::Service)
|
|
.category("Subscription")
|
|
.status(ProductStatus::Available)
|
|
.max_amount(500)
|
|
.validity_days(30)
|
|
.add_component(component2)
|
|
.build()?;
|
|
|
|
// Insert products using model-specific methods
|
|
db.insert_product(product1.clone())?;
|
|
db.insert_product(product2.clone())?;
|
|
|
|
println!(
|
|
"Product created: {} (${:.2})",
|
|
product1.name, product1.price.amount
|
|
);
|
|
println!(
|
|
"Product created: {} (${:.2})",
|
|
product2.name, product2.price.amount
|
|
);
|
|
|
|
println!("\n2. Retrieving Products");
|
|
println!("--------------------");
|
|
|
|
// Retrieve products using model-specific methods
|
|
let retrieved_product1 = db.get_product(1)?;
|
|
println!(
|
|
"Retrieved: {} (${:.2})",
|
|
retrieved_product1.name, retrieved_product1.price.amount
|
|
);
|
|
println!("Components:");
|
|
for component in &retrieved_product1.components {
|
|
println!(" - {} ({})", component.name, component.description);
|
|
}
|
|
|
|
println!("\n3. Listing All Products");
|
|
println!("----------------------");
|
|
|
|
// List all products using model-specific methods
|
|
let all_products = db.list_products()?;
|
|
println!("Found {} products:", all_products.len());
|
|
for product in all_products {
|
|
println!(
|
|
" - {} (${:.2}, {})",
|
|
product.name,
|
|
product.price.amount,
|
|
if product.is_purchasable() {
|
|
"Available"
|
|
} else {
|
|
"Unavailable"
|
|
}
|
|
);
|
|
}
|
|
|
|
println!("\n4. Creating a Sale");
|
|
println!("-----------------");
|
|
|
|
// Create a sale using the builder
|
|
let now = Utc::now();
|
|
|
|
let item1 = SaleItemBuilder::new()
|
|
.id(201)
|
|
.sale_id(1)
|
|
.product_id(Some(1))
|
|
.name("Standard Plan")
|
|
.quantity(1)
|
|
.unit_price(
|
|
CurrencyBuilder::new()
|
|
.id(4) // Add ID
|
|
.amount(29.99)
|
|
.currency_code("USD")
|
|
.build()?,
|
|
)
|
|
.active_till(now + Duration::days(30))
|
|
.build()?;
|
|
|
|
let sale = SaleBuilder::new()
|
|
.id(1)
|
|
.company_id(101)
|
|
.customer_id(123)
|
|
.currency_code("USD")
|
|
.status(SaleStatus::Pending)
|
|
.add_item(item1)
|
|
.build()?;
|
|
|
|
// Insert the sale using model-specific methods
|
|
db.insert_sale(sale.clone())?;
|
|
println!(
|
|
"Sale created: #{} for customer #{} (${:.2})",
|
|
sale.id, sale.customer_id, sale.total_amount.amount
|
|
);
|
|
|
|
println!("\n5. Updating a Sale");
|
|
println!("-----------------");
|
|
|
|
// Retrieve the sale, update it, and save it back
|
|
let mut retrieved_sale = db.get_sale(1)?;
|
|
println!(
|
|
"Retrieved sale: #{} with status {:?}",
|
|
retrieved_sale.id, retrieved_sale.status
|
|
);
|
|
|
|
// Update the status
|
|
retrieved_sale.update_status(SaleStatus::Completed);
|
|
db.insert_sale(retrieved_sale.clone())?;
|
|
|
|
println!("Updated sale status to {:?}", retrieved_sale.status);
|
|
|
|
println!("\n6. Deleting Objects");
|
|
println!("------------------");
|
|
|
|
// Delete a product
|
|
db.delete_product(2)?;
|
|
println!("Deleted product #2");
|
|
|
|
// List remaining products
|
|
let remaining_products = db.list_products()?;
|
|
println!("Remaining products: {}", remaining_products.len());
|
|
for product in remaining_products {
|
|
println!(" - {}", product.name);
|
|
}
|
|
|
|
println!("\nExample completed successfully!");
|
|
Ok(())
|
|
}
|