db/herodb/examples/circle_models_demo.rs
2025-04-20 09:21:32 +02:00

151 lines
4.4 KiB
Rust

use herodb::db::{DB, DBBuilder};
use herodb::models::circle::{Circle, Member, Name, Wallet, Asset, Role, Record, RecordType};
use std::path::Path;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a temporary directory for the database
let db_path = Path::new("./tmp/circle_demo");
if db_path.exists() {
std::fs::remove_dir_all(db_path)?;
}
std::fs::create_dir_all(db_path)?;
println!("Creating database at {:?}", db_path);
// Create a database with all circle models registered
let db = DBBuilder::new(db_path)
.register_model::<Circle>()
.register_model::<Member>()
.register_model::<Name>()
.register_model::<Wallet>()
.build()?;
// Create a circle
let mut circle = Circle::new(
1,
"ThreeFold Community".to_string(),
"A circle for ThreeFold community members".to_string(),
);
println!("Created circle: {:?}", circle);
db.set(&circle)?;
// Create members
let mut alice = Member::new(
1,
"Alice".to_string(),
"Core contributor".to_string(),
Role::Admin,
);
alice.add_email("alice@example.com".to_string());
let mut bob = Member::new(
2,
"Bob".to_string(),
"Community member".to_string(),
Role::Member,
);
bob.add_email("bob@example.com".to_string());
println!("Created members: {:?}, {:?}", alice, bob);
db.set(&alice)?;
db.set(&bob)?;
// Create a domain name
let mut domain = Name::new(
1,
"threefold.io".to_string(),
"ThreeFold main domain".to_string(),
);
let record = Record {
name: "www".to_string(),
text: "ThreeFold Website".to_string(),
category: RecordType::A,
addr: vec!["192.168.1.1".to_string()],
};
domain.add_record(record);
domain.add_admin("alice_pubkey".to_string());
println!("Created domain: {:?}", domain);
db.set(&domain)?;
// Create wallets
let mut alice_wallet = Wallet::new(
1,
"Alice's TFT Wallet".to_string(),
"Main TFT wallet".to_string(),
"Stellar".to_string(),
"GALICE...".to_string(),
);
alice_wallet.set_asset("TFT".to_string(), 1000.0);
alice_wallet.set_asset("XLM".to_string(), 100.0);
let mut bob_wallet = Wallet::new(
2,
"Bob's TFT Wallet".to_string(),
"Main TFT wallet".to_string(),
"Stellar".to_string(),
"GBOB...".to_string(),
);
bob_wallet.set_asset("TFT".to_string(), 500.0);
println!("Created wallets: {:?}, {:?}", alice_wallet, bob_wallet);
db.set(&alice_wallet)?;
db.set(&bob_wallet)?;
// Link wallets to members
alice.link_wallet(alice_wallet.id);
bob.link_wallet(bob_wallet.id);
db.set(&alice)?;
db.set(&bob)?;
// Retrieve and display all data
println!("\nRetrieving data from database:");
let circles = db.list_circles()?;
println!("Circles: {:#?}", circles);
let members = db.list_members()?;
println!("Members: {:#?}", members);
let names = db.list_names()?;
println!("Names: {:#?}", names);
let wallets = db.list_wallets()?;
println!("Wallets: {:#?}", wallets);
// Demonstrate wallet operations
println!("\nDemonstrating wallet operations:");
let mut alice_wallet = db.get_wallet(1)?;
println!("Alice's wallet before transfer: {:?}", alice_wallet);
println!("Alice's wallet total value: {}", alice_wallet.total_value());
let mut bob_wallet = db.get_wallet(2)?;
println!("Bob's wallet before transfer: {:?}", bob_wallet);
println!("Bob's wallet total value: {}", bob_wallet.total_value());
// Simulate a transfer of 100 TFT from Alice to Bob
alice_wallet.set_asset("TFT".to_string(), 900.0); // Decrease Alice's TFT by 100
bob_wallet.set_asset("TFT".to_string(), 600.0); // Increase Bob's TFT by 100
db.set(&alice_wallet)?;
db.set(&bob_wallet)?;
let alice_wallet = db.get_wallet(1)?;
println!("Alice's wallet after transfer: {:?}", alice_wallet);
println!("Alice's wallet total value: {}", alice_wallet.total_value());
let bob_wallet = db.get_wallet(2)?;
println!("Bob's wallet after transfer: {:?}", bob_wallet);
println!("Bob's wallet total value: {}", bob_wallet.total_value());
println!("\nCircle models demo completed successfully!");
Ok(())
}