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,8 +1,8 @@
// heromodels/src/models/finance/account.rs
use serde::{Deserialize, Serialize};
use heromodels_derive::model;
use heromodels_core::BaseModelData;
use heromodels_derive::model;
use serde::{Deserialize, Serialize};
use super::asset::Asset;
@@ -38,7 +38,7 @@ impl Account {
description: impl ToString,
ledger: impl ToString,
address: impl ToString,
pubkey: impl ToString
pubkey: impl ToString,
) -> Self {
let mut base_data = BaseModelData::new();
if let Some(id) = id {

View File

@@ -1,8 +1,8 @@
// heromodels/src/models/finance/asset.rs
use serde::{Deserialize, Serialize};
use heromodels_derive::model;
use heromodels_core::BaseModelData;
use heromodels_derive::model;
use serde::{Deserialize, Serialize};
/// AssetType defines the type of blockchain asset
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
@@ -24,12 +24,12 @@ impl Default for AssetType {
#[model] // Has base.Base in V spec
pub struct Asset {
pub base_data: BaseModelData,
pub name: String, // Name of the asset
pub description: String, // Description of the asset
pub amount: f64, // Amount of the asset
pub address: String, // Address of the asset on the blockchain or bank
pub asset_type: AssetType, // Type of the asset
pub decimals: u8, // Number of decimals of the asset
pub name: String, // Name of the asset
pub description: String, // Description of the asset
pub amount: f64, // Amount of the asset
pub address: String, // Address of the asset on the blockchain or bank
pub asset_type: AssetType, // Type of the asset
pub decimals: u8, // Number of decimals of the asset
}
impl Asset {

View File

@@ -1,9 +1,9 @@
// heromodels/src/models/finance/marketplace.rs
use serde::{Deserialize, Serialize};
use heromodels_derive::model;
use heromodels_core::BaseModelData;
use chrono::{DateTime, Utc};
use heromodels_core::BaseModelData;
use heromodels_derive::model;
use serde::{Deserialize, Serialize};
use super::asset::AssetType;
@@ -54,11 +54,11 @@ impl Default for BidStatus {
/// Bid represents a bid on an auction listing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bid {
pub listing_id: String, // ID of the listing this bid belongs to
pub bidder_id: u32, // ID of the user who placed the bid
pub amount: f64, // Bid amount
pub currency: String, // Currency of the bid
pub status: BidStatus, // Status of the bid
pub listing_id: String, // ID of the listing this bid belongs to
pub bidder_id: u32, // ID of the user who placed the bid
pub amount: f64, // Bid amount
pub currency: String, // Currency of the bid
pub status: BidStatus, // Status of the bid
pub created_at: DateTime<Utc>, // When the bid was created
}
@@ -97,7 +97,7 @@ pub struct Listing {
pub asset_id: String,
pub asset_type: AssetType,
pub seller_id: String,
pub price: f64, // Initial price for fixed price, or starting price for auction
pub price: f64, // Initial price for fixed price, or starting price for auction
pub currency: String,
pub listing_type: ListingType,
pub status: ListingStatus,
@@ -210,7 +210,11 @@ impl Listing {
}
/// Complete a sale (fixed price or auction)
pub fn complete_sale(mut self, buyer_id: impl ToString, sale_price: f64) -> Result<Self, &'static str> {
pub fn complete_sale(
mut self,
buyer_id: impl ToString,
sale_price: f64,
) -> Result<Self, &'static str> {
if self.status != ListingStatus::Active {
return Err("Cannot complete sale for inactive listing");
}
@@ -223,7 +227,9 @@ impl Listing {
// If this was an auction, accept the winning bid and reject others
if self.listing_type == ListingType::Auction {
for bid in &mut self.bids {
if bid.bidder_id.to_string() == self.buyer_id.as_ref().unwrap().to_string() && bid.amount == sale_price {
if bid.bidder_id.to_string() == self.buyer_id.as_ref().unwrap().to_string()
&& bid.amount == sale_price
{
bid.status = BidStatus::Accepted;
} else {
bid.status = BidStatus::Rejected;