fix: Fix all examples

This commit is contained in:
Mahmoud Emad
2025-05-17 16:03:00 +03:00
parent 57f59da43e
commit e4c50ca9d7
19 changed files with 166 additions and 80 deletions

View File

@@ -1,11 +1,16 @@
// heromodels/examples/governance_proposal_example/main.rs
use chrono::{Utc, Duration};
use heromodels::db::{Collection, Db};
use heromodels::models::governance::{Proposal, ProposalStatus, VoteEventStatus};
fn main() {
println!("Governance Proposal Model Example\n");
// Create a new DB instance, reset before every run
let db_path = "/tmp/ourdb_governance_proposal_example";
let db = heromodels::db::hero::OurDB::new(db_path, true).expect("Can create DB");
// Create a new proposal with auto-generated ID
let mut proposal = Proposal::new(
None, // id (auto-generated)
@@ -16,9 +21,9 @@ fn main() {
Utc::now() + Duration::days(14) // vote_end_date (14 days from now)
);
println!("Created Proposal: '{}' (ID: {})", proposal.title, proposal.base_data.id);
println!("Status: {:?}, Vote Status: {:?}", proposal.status, proposal.vote_status);
println!("Vote Period: {} to {}\n", proposal.vote_start_date, proposal.vote_end_date);
println!("Before saving - Created Proposal: '{}' (ID: {})", proposal.title, proposal.base_data.id);
println!("Before saving - Status: {:?}, Vote Status: {:?}", proposal.status, proposal.vote_status);
println!("Before saving - Vote Period: {} to {}\n", proposal.vote_start_date, proposal.vote_end_date);
// Add vote options
proposal = proposal.add_option(1, "Approve Allocation");
@@ -31,6 +36,16 @@ fn main() {
}
println!("");
// Save the proposal to the database
let collection = db.collection::<Proposal>().expect("can open proposal collection");
let (proposal_id, saved_proposal) = collection.set(&proposal).expect("can save proposal");
println!("After saving - Proposal ID: {}", saved_proposal.base_data.id);
println!("After saving - Returned ID: {}", proposal_id);
// Use the saved proposal for further operations
proposal = saved_proposal;
// Simulate casting votes
println!("Simulating Votes...");
// User 1 votes for 'Approve Allocation' with 100 shares (with explicit ballot ID)
@@ -83,7 +98,7 @@ fn main() {
// Example of a private proposal (not fully implemented in cast_vote eligibility yet)
let mut private_proposal = Proposal::new(
Some(2), // explicit ID
None, // auto-generated ID
"user_admin_001",
"Internal Team Restructure Vote",
"Vote on proposed internal team changes.",
@@ -94,8 +109,16 @@ fn main() {
private_proposal = private_proposal.add_option(1, "Accept Restructure");
private_proposal = private_proposal.add_option(2, "Reject Restructure");
println!("\nCreated Private Proposal: '{}'", private_proposal.title);
println!("Eligible Voters (Group): {:?}", private_proposal.private_group);
println!("\nBefore saving - Created Private Proposal: '{}'", private_proposal.title);
println!("Before saving - Eligible Voters (Group): {:?}", private_proposal.private_group);
// Save the private proposal to the database
let (private_proposal_id, saved_private_proposal) = collection.set(&private_proposal).expect("can save private proposal");
private_proposal = saved_private_proposal;
println!("After saving - Private Proposal ID: {}", private_proposal.base_data.id);
println!("After saving - Returned ID: {}", private_proposal_id);
// User 10 (eligible) votes with explicit ballot ID
private_proposal = private_proposal.cast_vote(Some(201), 10, 1, 100);
// User 40 (ineligible) tries to vote with auto-generated ballot ID
@@ -106,5 +129,6 @@ fn main() {
println!(" - {}: {} (Votes: {})", option.id, option.text, option.count);
}
println!("\nGovernance Proposal Example Finished.");
println!("\nExample finished. DB stored at {}", db_path);
println!("To clean up, you can manually delete the directory: {}", db_path);
}