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,10 +1,10 @@
// heromodels/src/models/governance/proposal.rs
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use heromodels_derive::model; // For #[model]
use rhai_autobind_macros::rhai_model_export;
use rhai::{CustomType, TypeBuilder};
use rhai_autobind_macros::rhai_model_export;
use serde::{Deserialize, Serialize};
use heromodels_core::BaseModelData;
@@ -13,11 +13,11 @@ use heromodels_core::BaseModelData;
/// ProposalStatus defines the lifecycle status of a governance proposal itself
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ProposalStatus {
Draft, // Proposal is being prepared
Active, // Proposal is active
Approved, // Proposal has been formally approved
Rejected, // Proposal has been formally rejected
Cancelled,// Proposal was cancelled
Draft, // Proposal is being prepared
Active, // Proposal is active
Approved, // Proposal has been formally approved
Rejected, // Proposal has been formally rejected
Cancelled, // Proposal was cancelled
}
impl Default for ProposalStatus {
@@ -26,7 +26,6 @@ impl Default for ProposalStatus {
}
}
/// VoteEventStatus represents the status of the voting process for a proposal
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum VoteEventStatus {
@@ -46,9 +45,9 @@ impl Default for VoteEventStatus {
/// VoteOption represents a specific choice that can be voted on
#[derive(Debug, Clone, Serialize, Deserialize, CustomType)]
pub struct VoteOption {
pub id: u8, // Simple identifier for this option
pub text: String, // Descriptive text of the option
pub count: i64, // How many votes this option has received
pub id: u8, // Simple identifier for this option
pub text: String, // Descriptive text of the option
pub count: i64, // How many votes this option has received
pub min_valid: Option<i64>, // Optional: minimum votes needed
}
@@ -69,9 +68,9 @@ impl VoteOption {
#[model] // Has base.Base in V spec
pub struct Ballot {
pub base_data: BaseModelData,
pub user_id: u32, // The ID of the user who cast this ballot
pub vote_option_id: u8, // The 'id' of the VoteOption chosen
pub shares_count: i64, // Number of shares/tokens/voting power
pub user_id: u32, // The ID of the user who cast this ballot
pub vote_option_id: u8, // The 'id' of the VoteOption chosen
pub shares_count: i64, // Number of shares/tokens/voting power
}
impl Ballot {
@@ -97,7 +96,6 @@ impl Ballot {
}
}
/// Proposal represents a governance proposal that can be voted upon.
#[derive(Debug, Clone, Serialize, Deserialize, CustomType)]
#[rhai_model_export(db_type = "std::sync::Arc<crate::db::hero::OurDB>")]
@@ -128,7 +126,14 @@ impl Proposal {
/// * `description` - Description of the proposal
/// * `vote_start_date` - Date when voting starts
/// * `vote_end_date` - Date when voting ends
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 {
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);
@@ -155,18 +160,30 @@ impl Proposal {
self
}
pub fn cast_vote(mut self, ballot_id: Option<u32>, 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;
}
if !self.options.iter().any(|opt| opt.id == chosen_option_id) {
eprintln!("Chosen option ID {} does not exist for proposal '{}'", chosen_option_id, self.title);
eprintln!(
"Chosen option ID {} does not exist for proposal '{}'",
chosen_option_id, self.title
);
return self;
}
if let Some(group) = &self.private_group {
if !group.contains(&user_id) {
eprintln!("User {} is not eligible to vote on proposal '{}'", user_id, self.title);
eprintln!(
"User {} is not eligible to vote on proposal '{}'",
user_id, self.title
);
return self;
}
}
@@ -174,7 +191,11 @@ impl Proposal {
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) {
if let Some(option) = self
.options
.iter_mut()
.find(|opt| opt.id == chosen_option_id)
{
option.count += shares;
}
self