feat: Add an example about how to create a vote with comment

This commit is contained in:
Mahmoud-Emad
2025-05-21 14:50:55 +03:00
parent bebb35e686
commit a71a966989
2 changed files with 148 additions and 1 deletions

View File

@@ -2,7 +2,7 @@
use chrono::{Duration, Utc};
use heromodels::db::{Collection, Db};
use heromodels::models::governance::{Proposal, ProposalStatus, VoteEventStatus};
use heromodels::models::governance::{Ballot, Proposal, ProposalStatus, VoteEventStatus};
fn main() {
println!("Governance Proposal Model Example\n");
@@ -99,6 +99,46 @@ fn main() {
}
println!("");
// Example of voting with comments using the cast_vote_with_comment method
println!("Adding votes with comments...");
// User 7 votes for 'Approve Allocation' with a comment
proposal = proposal.cast_vote_with_comment(
Some(110), // ballot_id
7, // user_id
1, // chosen_option_id (Approve Allocation)
80, // shares
"I strongly support this proposal because it aligns with our community values."
);
// User 8 votes for 'Reject Allocation' with a comment
proposal = proposal.cast_vote_with_comment(
Some(111), // ballot_id
8, // user_id
2, // chosen_option_id (Reject Allocation)
60, // shares
"I have concerns about the allocation priorities."
);
println!("\nBallots with Comments:");
for ballot in &proposal.ballots {
if let Some(comment) = &ballot.comment {
println!(
"- Ballot ID: {}, User ID: {}, Option ID: {}, Shares: {}",
ballot.base_data.id, ballot.user_id, ballot.vote_option_id, ballot.shares_count
);
println!(" Comment: \"{}\"", comment);
}
}
println!("\nUpdated Vote Counts After Comments:");
for option in &proposal.options {
println!(
"- Option ID: {}, Text: '{}', Votes: {}",
option.id, option.text, option.count
);
}
// Change proposal status
proposal = proposal.change_proposal_status(ProposalStatus::Active);
println!("Changed Proposal Status to: {:?}", proposal.status);
@@ -176,6 +216,50 @@ fn main() {
// User 40 (ineligible) tries to vote with auto-generated ballot ID
private_proposal = private_proposal.cast_vote(None, 40, 1, 50);
// Example of voting with comments on a private proposal
println!("\nAdding votes with comments to private proposal...");
// User 20 (eligible) votes with a comment
private_proposal = private_proposal.cast_vote_with_comment(
Some(202), // ballot_id
20, // user_id (eligible)
1, // chosen_option_id
75, // shares
"I support this restructuring plan with some reservations."
);
// User 30 (eligible) votes with a comment
private_proposal = private_proposal.cast_vote_with_comment(
Some(203), // ballot_id
30, // user_id (eligible)
2, // chosen_option_id
90, // shares
"I believe we should reconsider the timing of these changes."
);
// User 40 (ineligible) tries to vote with a comment
private_proposal = private_proposal.cast_vote_with_comment(
Some(204), // ballot_id
40, // user_id (ineligible)
1, // chosen_option_id
50, // shares
"This restructuring seems unnecessary."
);
println!("Eligible users 20 and 30 added votes with comments.");
println!("Ineligible user 40 attempted to vote with a comment (should be rejected).");
println!("\nPrivate Proposal Ballots with Comments:");
for ballot in &private_proposal.ballots {
if let Some(comment) = &ballot.comment {
println!(
"- Ballot ID: {}, User ID: {}, Option ID: {}, Shares: {}",
ballot.base_data.id, ballot.user_id, ballot.vote_option_id, ballot.shares_count
);
println!(" Comment: \"{}\"", comment);
}
}
println!("Private Proposal Vote Counts:");
for option in &private_proposal.options {
println!(