...
This commit is contained in:
		@@ -1,5 +0,0 @@
 | 
			
		||||
//! Simple binary to test if binaries are working
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    println!("Hello from dummy binary!");
 | 
			
		||||
}
 | 
			
		||||
@@ -1,7 +0,0 @@
 | 
			
		||||
//! Simple binary to demonstrate Rhai wrapper functionality
 | 
			
		||||
use herodb::zaz::cmd::examples::run_rhai_demo;
 | 
			
		||||
 | 
			
		||||
fn main() -> Result<(), String> {
 | 
			
		||||
    println!("Running Rhai wrapper examples for zaz models...");
 | 
			
		||||
    run_rhai_demo()
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										190
									
								
								herodb/src/cmd/dbexample2/main.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										190
									
								
								herodb/src/cmd/dbexample2/main.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,190 @@
 | 
			
		||||
use chrono::{DateTime, Utc, Duration};
 | 
			
		||||
use herodb::db::{DB, DBBuilder};
 | 
			
		||||
use herodb::models::biz::{
 | 
			
		||||
    Currency, CurrencyBuilder,
 | 
			
		||||
    Product, ProductBuilder, ProductComponent, ProductComponentBuilder,
 | 
			
		||||
    ProductType, ProductStatus,
 | 
			
		||||
    Sale, SaleBuilder, SaleItem, SaleItemBuilder, SaleStatus
 | 
			
		||||
};
 | 
			
		||||
use std::path::PathBuf;
 | 
			
		||||
use std::fs;
 | 
			
		||||
 | 
			
		||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
 | 
			
		||||
    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/dbexample2");
 | 
			
		||||
    if db_path.exists() {
 | 
			
		||||
        fs::remove_dir_all(&db_path)?;
 | 
			
		||||
    }
 | 
			
		||||
    fs::create_dir_all(&db_path)?;
 | 
			
		||||
    println!("Database path: {:?}", db_path);
 | 
			
		||||
 | 
			
		||||
    // Create a database instance with our models registered
 | 
			
		||||
    let 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()
 | 
			
		||||
        .amount(0.0)  // Initial amount
 | 
			
		||||
        .currency_code("USD")
 | 
			
		||||
        .build()?;
 | 
			
		||||
 | 
			
		||||
    // Insert the currency
 | 
			
		||||
    db.insert_currency(&usd)?;
 | 
			
		||||
    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()
 | 
			
		||||
            .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()
 | 
			
		||||
            .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)?;
 | 
			
		||||
    db.insert_product(&product2)?;
 | 
			
		||||
 | 
			
		||||
    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(1)
 | 
			
		||||
        .name("Standard Plan")
 | 
			
		||||
        .quantity(1)
 | 
			
		||||
        .unit_price(CurrencyBuilder::new()
 | 
			
		||||
            .amount(29.99)
 | 
			
		||||
            .currency_code("USD")
 | 
			
		||||
            .build()?)
 | 
			
		||||
        .active_till(now + Duration::days(30))
 | 
			
		||||
        .build()?;
 | 
			
		||||
 | 
			
		||||
    let sale = SaleBuilder::new()
 | 
			
		||||
        .id(1)
 | 
			
		||||
        .company_id(101)
 | 
			
		||||
        .buyer_name("John Doe")
 | 
			
		||||
        .buyer_email("john.doe@example.com")
 | 
			
		||||
        .currency_code("USD")
 | 
			
		||||
        .status(SaleStatus::Pending)
 | 
			
		||||
        .add_item(item1)
 | 
			
		||||
        .build()?;
 | 
			
		||||
 | 
			
		||||
    // Insert the sale using model-specific methods
 | 
			
		||||
    db.insert_sale(&sale)?;
 | 
			
		||||
    println!("Sale created: #{} for {} (${:.2})", 
 | 
			
		||||
        sale.id, 
 | 
			
		||||
        sale.buyer_name,
 | 
			
		||||
        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)?;
 | 
			
		||||
    
 | 
			
		||||
    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(())
 | 
			
		||||
}
 | 
			
		||||
@@ -4,8 +4,9 @@
 | 
			
		||||
//! and includes support for defining and working with data models.
 | 
			
		||||
 | 
			
		||||
// Core modules
 | 
			
		||||
mod db;
 | 
			
		||||
mod error;
 | 
			
		||||
pub mod db;
 | 
			
		||||
pub mod error;
 | 
			
		||||
pub mod models;
 | 
			
		||||
 | 
			
		||||
// Re-exports
 | 
			
		||||
pub use error::Error;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								herodb/src/models/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								herodb/src/models/mod.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
pub mod biz;
 | 
			
		||||
		Reference in New Issue
	
	Block a user