...
This commit is contained in:
parent
f4296b3261
commit
10831fd260
@ -1,34 +1,17 @@
|
|||||||
use herodb::db::{DB, DBBuilder};
|
|
||||||
use herodb::models::circle::{Circle, Member, Name, Wallet, Role, Record, RecordType};
|
use herodb::models::circle::{Circle, Member, Name, Wallet, Role, Record, RecordType};
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
// Create a temporary directory for the database
|
println!("Circle Models Demo (In-Memory Version)");
|
||||||
let db_path = Path::new("./tmp/circle_demo");
|
println!("=====================================\n");
|
||||||
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
|
// Create a circle
|
||||||
let mut circle = Circle::new(
|
let circle = Circle::new(
|
||||||
1,
|
1,
|
||||||
"ThreeFold Community".to_string(),
|
"ThreeFold Community".to_string(),
|
||||||
"A circle for ThreeFold community members".to_string(),
|
"A circle for ThreeFold community members".to_string(),
|
||||||
);
|
);
|
||||||
|
|
||||||
println!("Created circle: {:?}", circle);
|
println!("Created circle: {:?}", circle);
|
||||||
db.set(&circle)?;
|
|
||||||
|
|
||||||
// Create members
|
// Create members
|
||||||
let mut alice = Member::new(
|
let mut alice = Member::new(
|
||||||
@ -48,8 +31,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
bob.add_email("bob@example.com".to_string());
|
bob.add_email("bob@example.com".to_string());
|
||||||
|
|
||||||
println!("Created members: {:?}, {:?}", alice, bob);
|
println!("Created members: {:?}, {:?}", alice, bob);
|
||||||
db.set(&alice)?;
|
|
||||||
db.set(&bob)?;
|
|
||||||
|
|
||||||
// Create a domain name
|
// Create a domain name
|
||||||
let mut domain = Name::new(
|
let mut domain = Name::new(
|
||||||
@ -69,7 +50,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
domain.add_admin("alice_pubkey".to_string());
|
domain.add_admin("alice_pubkey".to_string());
|
||||||
|
|
||||||
println!("Created domain: {:?}", domain);
|
println!("Created domain: {:?}", domain);
|
||||||
db.set(&domain)?;
|
|
||||||
|
|
||||||
// Create wallets
|
// Create wallets
|
||||||
let mut alice_wallet = Wallet::new(
|
let mut alice_wallet = Wallet::new(
|
||||||
@ -94,39 +74,27 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
bob_wallet.set_asset("TFT".to_string(), 500.0);
|
bob_wallet.set_asset("TFT".to_string(), 500.0);
|
||||||
|
|
||||||
println!("Created wallets: {:?}, {:?}", alice_wallet, bob_wallet);
|
println!("Created wallets: {:?}, {:?}", alice_wallet, bob_wallet);
|
||||||
db.set(&alice_wallet)?;
|
|
||||||
db.set(&bob_wallet)?;
|
|
||||||
|
|
||||||
// Link wallets to members
|
// Link wallets to members
|
||||||
alice.link_wallet(alice_wallet.id);
|
alice.link_wallet(alice_wallet.id);
|
||||||
bob.link_wallet(bob_wallet.id);
|
bob.link_wallet(bob_wallet.id);
|
||||||
|
|
||||||
db.set(&alice)?;
|
println!("Linked wallets to members");
|
||||||
db.set(&bob)?;
|
|
||||||
|
|
||||||
// Retrieve and display all data
|
// Display all data
|
||||||
println!("\nRetrieving data from database:");
|
println!("\nDisplaying all data:");
|
||||||
|
|
||||||
let circles = db.list::<Circle>()?;
|
println!("Circles: {:#?}", vec![circle]);
|
||||||
println!("Circles: {:#?}", circles);
|
println!("Members: {:#?}", vec![alice.clone(), bob.clone()]);
|
||||||
|
println!("Names: {:#?}", vec![domain]);
|
||||||
let members = db.list::<Member>()?;
|
println!("Wallets: {:#?}", vec![alice_wallet.clone(), bob_wallet.clone()]);
|
||||||
println!("Members: {:#?}", members);
|
|
||||||
|
|
||||||
let names = db.list::<Name>()?;
|
|
||||||
println!("Names: {:#?}", names);
|
|
||||||
|
|
||||||
let wallets = db.list::<Wallet>()?;
|
|
||||||
println!("Wallets: {:#?}", wallets);
|
|
||||||
|
|
||||||
// Demonstrate wallet operations
|
// Demonstrate wallet operations
|
||||||
println!("\nDemonstrating 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 before transfer: {:?}", alice_wallet);
|
||||||
println!("Alice's wallet total value: {}", alice_wallet.total_value());
|
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 before transfer: {:?}", bob_wallet);
|
||||||
println!("Bob's wallet total value: {}", bob_wallet.total_value());
|
println!("Bob's wallet total value: {}", bob_wallet.total_value());
|
||||||
|
|
||||||
@ -134,14 +102,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
alice_wallet.set_asset("TFT".to_string(), 900.0); // Decrease Alice's TFT by 100
|
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
|
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 after transfer: {:?}", alice_wallet);
|
||||||
println!("Alice's wallet total value: {}", alice_wallet.total_value());
|
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 after transfer: {:?}", bob_wallet);
|
||||||
println!("Bob's wallet total value: {}", bob_wallet.total_value());
|
println!("Bob's wallet total value: {}", bob_wallet.total_value());
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user