feat: Add get_all method to list all db objects based on the object model

This commit is contained in:
Mahmoud-Emad
2025-05-21 09:13:58 +03:00
parent bd36d6bda0
commit 4c0c7be574
7 changed files with 225 additions and 87 deletions

View File

@@ -1,6 +1,6 @@
// heromodels/examples/governance_proposal_example/main.rs
use chrono::{Utc, Duration};
use chrono::{Duration, Utc};
use heromodels::db::{Collection, Db};
use heromodels::models::governance::{Proposal, ProposalStatus, VoteEventStatus};
@@ -13,17 +13,26 @@ fn main() {
// Create a new proposal with auto-generated ID
let mut proposal = Proposal::new(
None, // id (auto-generated)
"user_creator_123", // creator_id
"Community Fund Allocation for Q3", // title
None, // id (auto-generated)
"user_creator_123", // creator_id
"Community Fund Allocation for Q3", // title
"Proposal to allocate funds for community projects in the third quarter.", // description
Utc::now(), // vote_start_date
Utc::now() + Duration::days(14) // vote_end_date (14 days from now)
Utc::now(), // vote_start_date
Utc::now() + Duration::days(14), // vote_end_date (14 days from now)
);
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);
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");
@@ -32,15 +41,23 @@ fn main() {
println!("Added Vote Options:");
for option in &proposal.options {
println!("- Option ID: {}, Text: '{}', Votes: {}", option.id, option.text, option.count);
println!(
"- Option ID: {}, Text: '{}', Votes: {}",
option.id, option.text, option.count
);
}
println!("");
// Save the proposal to the database
let collection = db.collection::<Proposal>().expect("can open proposal collection");
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 - Proposal ID: {}",
saved_proposal.base_data.id
);
println!("After saving - Returned ID: {}", proposal_id);
// Use the saved proposal for further operations
@@ -63,13 +80,18 @@ fn main() {
println!("\nVote Counts After Simulation:");
for option in &proposal.options {
println!("- Option ID: {}, Text: '{}', Votes: {}", option.id, option.text, option.count);
println!(
"- Option ID: {}, Text: '{}', Votes: {}",
option.id, option.text, option.count
);
}
println!("\nBallots Cast:");
for ballot in &proposal.ballots {
println!("- Ballot ID: {}, User ID: {}, Option ID: {}, Shares: {}",
ballot.base_data.id, ballot.user_id, ballot.vote_option_id, ballot.shares_count);
println!(
"- Ballot ID: {}, User ID: {}, Option ID: {}, Shares: {}",
ballot.base_data.id, ballot.user_id, ballot.vote_option_id, ballot.shares_count
);
}
println!("");
@@ -92,7 +114,10 @@ fn main() {
println!("Vote Status: {:?}", proposal.vote_status);
println!("Options:");
for option in &proposal.options {
println!(" - {}: {} (Votes: {})", option.id, option.text, option.count);
println!(
" - {}: {} (Votes: {})",
option.id, option.text, option.count
);
}
println!("Total Ballots: {}", proposal.ballots.len());
@@ -103,20 +128,31 @@ fn main() {
"Internal Team Restructure Vote",
"Vote on proposed internal team changes.",
Utc::now(),
Utc::now() + Duration::days(7)
Utc::now() + Duration::days(7),
);
private_proposal.private_group = Some(vec![10, 20, 30]); // Only users 10, 20, 30 can vote
private_proposal = private_proposal.add_option(1, "Accept Restructure");
private_proposal = private_proposal.add_option(2, "Reject Restructure");
println!("\nBefore saving - Created Private Proposal: '{}'", private_proposal.title);
println!("Before saving - 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");
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 - Private Proposal ID: {}",
private_proposal.base_data.id
);
println!("After saving - Returned ID: {}", private_proposal_id);
// User 10 (eligible) votes with explicit ballot ID
@@ -125,10 +161,42 @@ fn main() {
private_proposal = private_proposal.cast_vote(None, 40, 1, 50);
println!("Private Proposal Vote Counts:");
for option in &private_proposal.options {
println!(" - {}: {} (Votes: {})", option.id, option.text, option.count);
for option in &private_proposal.options {
println!(
" - {}: {} (Votes: {})",
option.id, option.text, option.count
);
}
println!("\nExample finished. DB stored at {}", db_path);
println!("To clean up, you can manually delete the directory: {}", db_path);
println!(
"To clean up, you can manually delete the directory: {}",
db_path
);
// --- Additional Example: Listing and Filtering Proposals ---
println!("\n--- Listing All Proposals ---");
// List all proposals from the DB
let all_proposals = collection.get_all().expect("can list all proposals");
for proposal in &all_proposals {
println!(
"- Proposal ID: {}, Title: '{}', Status: {:?}",
proposal.base_data.id, proposal.title, proposal.status
);
}
println!("Total proposals in DB: {}", all_proposals.len());
// Filter proposals by status (e.g., only Active proposals)
let active_proposals: Vec<_> = all_proposals
.iter()
.filter(|p| p.status == ProposalStatus::Active)
.collect();
println!("\n--- Filtering Proposals by Status: Active ---");
for proposal in &active_proposals {
println!(
"- Proposal ID: {}, Title: '{}', Status: {:?}",
proposal.base_data.id, proposal.title, proposal.status
);
}
println!("Total ACTIVE proposals: {}", active_proposals.len());
}