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

@@ -78,12 +78,18 @@ impl Ballot {
/// Create a new ballot with auto-generated ID
///
/// # Arguments
/// * `id` - Optional ID for the ballot (use None for auto-generated ID)
/// * `user_id` - ID of the user who cast this ballot
/// * `vote_option_id` - ID of the vote option chosen
/// * `shares_count` - Number of shares/tokens/voting power
pub fn new(user_id: u32, vote_option_id: u8, shares_count: i64) -> Self {
pub fn new(id: Option<u32>, user_id: u32, vote_option_id: u8, shares_count: i64) -> Self {
let mut base_data = BaseModelData::new();
if let Some(id) = id {
base_data.update_id(id);
}
Self {
base_data: BaseModelData::new(),
base_data,
user_id,
vote_option_id,
shares_count,
@@ -116,14 +122,20 @@ impl Proposal {
/// Create a new proposal with auto-generated ID
///
/// # Arguments
/// * `id` - Optional ID for the proposal (use None for auto-generated ID)
/// * `creator_id` - ID of the user who created the proposal
/// * `title` - Title of the proposal
/// * `description` - Description of the proposal
/// * `vote_start_date` - Date when voting starts
/// * `vote_end_date` - Date when voting ends
pub fn new(creator_id: impl ToString, title: impl ToString, description: impl ToString, vote_start_date: DateTime<Utc>, vote_end_date: DateTime<Utc>) -> Self {
pub fn new(id: Option<u32>, creator_id: impl ToString, title: impl ToString, description: impl ToString, vote_start_date: DateTime<Utc>, vote_end_date: DateTime<Utc>) -> Self {
let mut base_data = BaseModelData::new();
if let Some(id) = id {
base_data.update_id(id);
}
Self {
base_data: BaseModelData::new(),
base_data,
creator_id: creator_id.to_string(),
title: title.to_string(),
description: description.to_string(),
@@ -143,7 +155,7 @@ impl Proposal {
self
}
pub fn cast_vote(mut self, user_id: u32, chosen_option_id: u8, shares: i64) -> Self {
pub fn cast_vote(mut self, ballot_id: Option<u32>, user_id: u32, chosen_option_id: u8, shares: i64) -> Self {
if self.vote_status != VoteEventStatus::Open {
eprintln!("Voting is not open for proposal '{}'", self.title);
return self;
@@ -159,7 +171,7 @@ impl Proposal {
}
}
let new_ballot = Ballot::new(user_id, chosen_option_id, shares);
let new_ballot = Ballot::new(ballot_id, user_id, chosen_option_id, shares);
self.ballots.push(new_ballot);
if let Some(option) = self.options.iter_mut().find(|opt| opt.id == chosen_option_id) {