...
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
use chrono::{DateTime, Utc, Duration};
|
||||
use herodb::db::{DB, DBBuilder};
|
||||
use chrono::{Utc, Duration};
|
||||
use herodb::db::DBBuilder;
|
||||
use herodb::models::biz::{
|
||||
Currency, CurrencyBuilder,
|
||||
Product, ProductBuilder, ProductComponent, ProductComponentBuilder,
|
||||
Product, ProductBuilder, ProductComponentBuilder,
|
||||
ProductType, ProductStatus,
|
||||
Sale, SaleBuilder, SaleItem, SaleItemBuilder, SaleStatus
|
||||
Sale, SaleBuilder, SaleItemBuilder, SaleStatus,
|
||||
ExchangeRate, ExchangeRateBuilder, EXCHANGE_RATE_SERVICE
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
use std::fs;
|
||||
@@ -26,6 +27,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.register_model::<Product>()
|
||||
.register_model::<Currency>()
|
||||
.register_model::<Sale>()
|
||||
.register_model::<ExchangeRate>()
|
||||
.build()?;
|
||||
|
||||
println!("\n1. Creating Products with Builder Pattern");
|
||||
@@ -39,14 +41,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
// Insert the currency
|
||||
db.insert_currency(&usd)?;
|
||||
println!("Currency created: ${:.2} {}", usd.amount, usd.currency_code);
|
||||
println!("Currency created: ${} {}", usd.amount, usd.currency_code);
|
||||
|
||||
// Create product components using the builder
|
||||
// Create product components using the builder with energy usage and cost
|
||||
let component1 = ProductComponentBuilder::new()
|
||||
.id(101)
|
||||
.name("Basic Support")
|
||||
.description("24/7 email support")
|
||||
.quantity(1)
|
||||
.energy_usage(5.0) // 5 watts
|
||||
.cost(CurrencyBuilder::new()
|
||||
.amount(5.0)
|
||||
.currency_code("USD")
|
||||
.build()?)
|
||||
.build()?;
|
||||
|
||||
let component2 = ProductComponentBuilder::new()
|
||||
@@ -54,6 +61,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.name("Premium Support")
|
||||
.description("24/7 phone and email support")
|
||||
.quantity(1)
|
||||
.energy_usage(10.0) // 10 watts
|
||||
.cost(CurrencyBuilder::new()
|
||||
.amount(15.0)
|
||||
.currency_code("USD")
|
||||
.build()?)
|
||||
.build()?;
|
||||
|
||||
// Create products using the builder
|
||||
@@ -67,7 +79,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.build()?)
|
||||
.type_(ProductType::Service)
|
||||
.category("Subscription")
|
||||
.status(ProductStatus::Available)
|
||||
.status(ProductStatus::Active)
|
||||
.max_amount(1000)
|
||||
.validity_days(30)
|
||||
.add_component(component1)
|
||||
@@ -83,7 +95,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.build()?)
|
||||
.type_(ProductType::Service)
|
||||
.category("Subscription")
|
||||
.status(ProductStatus::Available)
|
||||
.status(ProductStatus::Active)
|
||||
.max_amount(500)
|
||||
.validity_days(30)
|
||||
.add_component(component2)
|
||||
@@ -93,18 +105,32 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
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!("Product created: {} (${}) USD", product1.name, product1.price.amount);
|
||||
println!("Product created: {} (${}) USD", 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!("Retrieved: {} (${}) USD", retrieved_product1.name, retrieved_product1.price.amount);
|
||||
println!("Components:");
|
||||
for component in &retrieved_product1.components {
|
||||
println!(" - {} ({})", component.name, component.description);
|
||||
println!(" - {} ({}, Energy: {}W, Cost: ${} USD)",
|
||||
component.name,
|
||||
component.description,
|
||||
component.energy_usage,
|
||||
component.cost.amount
|
||||
);
|
||||
}
|
||||
|
||||
// Calculate total energy usage
|
||||
let total_energy = retrieved_product1.total_energy_usage();
|
||||
println!("Total energy usage: {}W", total_energy);
|
||||
|
||||
// Calculate components cost
|
||||
if let Some(components_cost) = retrieved_product1.components_cost_in_usd() {
|
||||
println!("Total components cost: ${} USD", components_cost.amount);
|
||||
}
|
||||
|
||||
println!("\n3. Listing All Products");
|
||||
@@ -114,7 +140,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let all_products = db.list_products()?;
|
||||
println!("Found {} products:", all_products.len());
|
||||
for product in all_products {
|
||||
println!(" - {} (${:.2}, {})",
|
||||
println!(" - {} (${} USD, {})",
|
||||
product.name,
|
||||
product.price.amount,
|
||||
if product.is_purchasable() { "Available" } else { "Unavailable" }
|
||||
@@ -152,7 +178,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
// Insert the sale using model-specific methods
|
||||
db.insert_sale(&sale)?;
|
||||
println!("Sale created: #{} for {} (${:.2})",
|
||||
println!("Sale created: #{} for {} (${} USD)",
|
||||
sale.id,
|
||||
sale.buyer_name,
|
||||
sale.total_amount.amount
|
||||
@@ -171,7 +197,77 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
println!("Updated sale status to {:?}", retrieved_sale.status);
|
||||
|
||||
println!("\n6. Deleting Objects");
|
||||
println!("\n6. Working with Exchange Rates");
|
||||
println!("----------------------------");
|
||||
|
||||
// Create and set exchange rates using the builder
|
||||
let eur_rate = ExchangeRateBuilder::new()
|
||||
.base_currency("EUR")
|
||||
.target_currency("USD")
|
||||
.rate(1.18)
|
||||
.build()?;
|
||||
|
||||
let gbp_rate = ExchangeRateBuilder::new()
|
||||
.base_currency("GBP")
|
||||
.target_currency("USD")
|
||||
.rate(1.38)
|
||||
.build()?;
|
||||
|
||||
// Insert exchange rates into the database
|
||||
db.insert_exchange_rate(&eur_rate)?;
|
||||
db.insert_exchange_rate(&gbp_rate)?;
|
||||
|
||||
// Set the exchange rates in the service
|
||||
EXCHANGE_RATE_SERVICE.set_rate(eur_rate.clone());
|
||||
EXCHANGE_RATE_SERVICE.set_rate(gbp_rate.clone());
|
||||
|
||||
println!("Exchange rates set:");
|
||||
println!(" - 1 EUR = {} USD", eur_rate.rate);
|
||||
println!(" - 1 GBP = {} USD", gbp_rate.rate);
|
||||
|
||||
// Create currencies in different denominations
|
||||
let eur_price = CurrencyBuilder::new()
|
||||
.amount(100.0)
|
||||
.currency_code("EUR")
|
||||
.build()?;
|
||||
|
||||
let gbp_price = CurrencyBuilder::new()
|
||||
.amount(85.0)
|
||||
.currency_code("GBP")
|
||||
.build()?;
|
||||
|
||||
// Convert to USD
|
||||
if let Some(eur_in_usd) = eur_price.to_usd() {
|
||||
println!("{} EUR = {} USD", eur_price.amount, eur_in_usd.amount);
|
||||
} else {
|
||||
println!("Could not convert EUR to USD");
|
||||
}
|
||||
|
||||
if let Some(gbp_in_usd) = gbp_price.to_usd() {
|
||||
println!("{} GBP = {} USD", gbp_price.amount, gbp_in_usd.amount);
|
||||
} else {
|
||||
println!("Could not convert GBP to USD");
|
||||
}
|
||||
|
||||
// Convert between currencies
|
||||
if let Some(eur_in_gbp) = eur_price.to_currency("GBP") {
|
||||
println!("{} EUR = {} GBP", eur_price.amount, eur_in_gbp.amount);
|
||||
} else {
|
||||
println!("Could not convert EUR to GBP");
|
||||
}
|
||||
|
||||
// Test product price conversion
|
||||
let retrieved_product2 = db.get_product(2)?;
|
||||
|
||||
if let Some(price_in_eur) = retrieved_product2.cost_in_currency("EUR") {
|
||||
println!("Product '{}' price: ${} USD = {} EUR",
|
||||
retrieved_product2.name,
|
||||
retrieved_product2.price.amount,
|
||||
price_in_eur.amount
|
||||
);
|
||||
}
|
||||
|
||||
println!("\n7. Deleting Objects");
|
||||
println!("------------------");
|
||||
|
||||
// Delete a product
|
||||
|
Reference in New Issue
Block a user