final db models wip
This commit is contained in:
@@ -8,7 +8,7 @@ use heromodels_core::BaseModelData;
|
||||
use super::asset::Asset;
|
||||
|
||||
/// Account represents a financial account owned by a user
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, CustomType)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, CustomType, Default)]
|
||||
#[model] // Has base.Base in V spec
|
||||
pub struct Account {
|
||||
pub base_data: BaseModelData,
|
||||
@@ -18,82 +18,75 @@ pub struct Account {
|
||||
pub ledger: String, // describes the ledger/blockchain where the account is located
|
||||
pub address: String, // address of the account on the blockchain
|
||||
pub pubkey: String, // public key
|
||||
pub assets: Vec<Asset>, // list of assets in this account
|
||||
pub assets: Vec<u32>, // list of assets in this account
|
||||
}
|
||||
|
||||
impl Account {
|
||||
/// Create a new account with auto-generated ID
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - Optional ID for the account (use None for auto-generated ID)
|
||||
/// * `name` - Name of the account
|
||||
/// * `user_id` - ID of the user who owns the account
|
||||
/// * `description` - Description of the account
|
||||
/// * `ledger` - Ledger/blockchain where the account is located
|
||||
/// * `address` - Address of the account on the blockchain
|
||||
/// * `pubkey` - Public key
|
||||
pub fn new(
|
||||
id: Option<u32>,
|
||||
name: impl ToString,
|
||||
user_id: u32,
|
||||
description: impl ToString,
|
||||
ledger: impl ToString,
|
||||
address: impl ToString,
|
||||
pubkey: impl ToString,
|
||||
) -> Self {
|
||||
let mut base_data = BaseModelData::new();
|
||||
if let Some(id) = id {
|
||||
base_data.update_id(id);
|
||||
}
|
||||
|
||||
/// Create a new account with default values
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base_data,
|
||||
name: name.to_string(),
|
||||
user_id,
|
||||
description: description.to_string(),
|
||||
ledger: ledger.to_string(),
|
||||
address: address.to_string(),
|
||||
pubkey: pubkey.to_string(),
|
||||
base_data: BaseModelData::new(),
|
||||
name: String::new(),
|
||||
user_id: 0,
|
||||
description: String::new(),
|
||||
ledger: String::new(),
|
||||
address: String::new(),
|
||||
pubkey: String::new(),
|
||||
assets: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the name of the account
|
||||
pub fn name(mut self, name: impl ToString) -> Self {
|
||||
self.name = name.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the user ID of the account owner
|
||||
pub fn user_id(mut self, user_id: u32) -> Self {
|
||||
self.user_id = user_id;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the description of the account
|
||||
pub fn description(mut self, description: impl ToString) -> Self {
|
||||
self.description = description.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the ledger/blockchain where the account is located
|
||||
pub fn ledger(mut self, ledger: impl ToString) -> Self {
|
||||
self.ledger = ledger.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the address of the account on the blockchain
|
||||
pub fn address(mut self, address: impl ToString) -> Self {
|
||||
self.address = address.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the public key of the account
|
||||
pub fn pubkey(mut self, pubkey: impl ToString) -> Self {
|
||||
self.pubkey = pubkey.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
/// Add an asset to the account
|
||||
pub fn add_asset(mut self, asset: Asset) -> Self {
|
||||
self.assets.push(asset);
|
||||
pub fn add_asset(mut self, asset_id: u32) -> Self {
|
||||
self.assets.push(asset_id);
|
||||
self
|
||||
}
|
||||
|
||||
/// Get the total value of all assets in the account
|
||||
pub fn total_value(&self) -> f64 {
|
||||
self.assets.iter().map(|asset| asset.amount).sum()
|
||||
/// TODO: implement
|
||||
0.0
|
||||
}
|
||||
|
||||
/// Find an asset by name
|
||||
pub fn find_asset_by_name(&self, name: &str) -> Option<&Asset> {
|
||||
self.assets.iter().find(|asset| asset.name == name)
|
||||
}
|
||||
|
||||
/// Update the account details
|
||||
pub fn update_details(
|
||||
mut self,
|
||||
name: Option<impl ToString>,
|
||||
description: Option<impl ToString>,
|
||||
address: Option<impl ToString>,
|
||||
pubkey: Option<impl ToString>,
|
||||
) -> Self {
|
||||
if let Some(name) = name {
|
||||
self.name = name.to_string();
|
||||
}
|
||||
if let Some(description) = description {
|
||||
self.description = description.to_string();
|
||||
}
|
||||
if let Some(address) = address {
|
||||
self.address = address.to_string();
|
||||
}
|
||||
if let Some(pubkey) = pubkey {
|
||||
self.pubkey = pubkey.to_string();
|
||||
}
|
||||
self
|
||||
/// TODO: implement
|
||||
return None
|
||||
}
|
||||
}
|
||||
|
@@ -21,7 +21,7 @@ impl Default for AssetType {
|
||||
}
|
||||
|
||||
/// Asset represents a digital asset or token
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, CustomType)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, CustomType, Default)]
|
||||
#[model] // Has base.Base in V spec
|
||||
pub struct Asset {
|
||||
pub base_data: BaseModelData,
|
||||
@@ -34,47 +34,55 @@ pub struct Asset {
|
||||
}
|
||||
|
||||
impl Asset {
|
||||
/// Create a new asset with auto-generated ID
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - Optional ID for the asset (use None for auto-generated ID)
|
||||
/// * `name` - Name of the asset
|
||||
/// * `description` - Description of the asset
|
||||
/// * `amount` - Amount of the asset
|
||||
/// * `address` - Address of the asset on the blockchain or bank
|
||||
/// * `asset_type` - Type of the asset
|
||||
/// * `decimals` - Number of decimals of the asset
|
||||
pub fn new(
|
||||
id: Option<u32>,
|
||||
name: impl ToString,
|
||||
description: impl ToString,
|
||||
amount: f64,
|
||||
address: impl ToString,
|
||||
asset_type: AssetType,
|
||||
decimals: u8,
|
||||
) -> Self {
|
||||
let mut base_data = BaseModelData::new();
|
||||
if let Some(id) = id {
|
||||
base_data.update_id(id);
|
||||
}
|
||||
|
||||
/// Create a new asset with default values
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base_data,
|
||||
name: name.to_string(),
|
||||
description: description.to_string(),
|
||||
amount,
|
||||
address: address.to_string(),
|
||||
asset_type,
|
||||
decimals,
|
||||
base_data: BaseModelData::new(),
|
||||
name: String::new(),
|
||||
description: String::new(),
|
||||
amount: 0.0,
|
||||
address: String::new(),
|
||||
asset_type: AssetType::default(),
|
||||
decimals: 18, // Default for most tokens
|
||||
}
|
||||
}
|
||||
|
||||
/// Update the asset amount
|
||||
pub fn update_amount(mut self, amount: f64) -> Self {
|
||||
/// Set the name of the asset
|
||||
pub fn name(mut self, name: impl ToString) -> Self {
|
||||
self.name = name.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the description of the asset
|
||||
pub fn description(mut self, description: impl ToString) -> Self {
|
||||
self.description = description.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the amount of the asset
|
||||
pub fn amount(mut self, amount: f64) -> Self {
|
||||
self.amount = amount;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the address of the asset on the blockchain
|
||||
pub fn address(mut self, address: impl ToString) -> Self {
|
||||
self.address = address.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the type of the asset
|
||||
pub fn asset_type(mut self, asset_type: AssetType) -> Self {
|
||||
self.asset_type = asset_type;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the number of decimals of the asset
|
||||
pub fn decimals(mut self, decimals: u8) -> Self {
|
||||
self.decimals = decimals;
|
||||
self
|
||||
}
|
||||
|
||||
/// Get the formatted amount with proper decimal places
|
||||
pub fn formatted_amount(&self) -> String {
|
||||
let factor = 10_f64.powi(self.decimals as i32);
|
||||
|
@@ -54,6 +54,7 @@ impl Default for BidStatus {
|
||||
|
||||
/// Bid represents a bid on an auction listing
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, CustomType)]
|
||||
#[derive(Default)]
|
||||
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
|
||||
@@ -64,32 +65,44 @@ pub struct Bid {
|
||||
}
|
||||
|
||||
impl Bid {
|
||||
/// Create a new bid
|
||||
pub fn new(
|
||||
listing_id: impl ToString,
|
||||
bidder_id: u32,
|
||||
amount: f64,
|
||||
currency: impl ToString,
|
||||
) -> Self {
|
||||
Self {
|
||||
listing_id: listing_id.to_string(),
|
||||
bidder_id,
|
||||
amount,
|
||||
currency: currency.to_string(),
|
||||
status: BidStatus::default(),
|
||||
created_at: Utc::now(),
|
||||
}
|
||||
/// Create a new bid with default values
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Update the status of the bid
|
||||
pub fn update_status(mut self, status: BidStatus) -> Self {
|
||||
/// Set the listing ID for the bid
|
||||
pub fn listing_id(mut self, listing_id: impl ToString) -> Self {
|
||||
self.listing_id = listing_id.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the bidder ID for the bid
|
||||
pub fn bidder_id(mut self, bidder_id: u32) -> Self {
|
||||
self.bidder_id = bidder_id;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the amount for the bid
|
||||
pub fn amount(mut self, amount: f64) -> Self {
|
||||
self.amount = amount;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the currency for the bid
|
||||
pub fn currency(mut self, currency: impl ToString) -> Self {
|
||||
self.currency = currency.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the status of the bid
|
||||
pub fn status(mut self, status: BidStatus) -> Self {
|
||||
self.status = status;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Listing represents a marketplace listing for an asset
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, CustomType)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, CustomType, Default)]
|
||||
#[model] // Has base.Base in V spec
|
||||
pub struct Listing {
|
||||
pub base_data: BaseModelData,
|
||||
@@ -112,66 +125,82 @@ pub struct Listing {
|
||||
}
|
||||
|
||||
impl Listing {
|
||||
/// Create a new listing with auto-generated ID
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - Optional ID for the listing (use None for auto-generated ID)
|
||||
/// * `title` - Title of the listing
|
||||
/// * `description` - Description of the listing
|
||||
/// * `asset_id` - ID of the asset being listed
|
||||
/// * `asset_type` - Type of the asset
|
||||
/// * `seller_id` - ID of the seller
|
||||
/// * `price` - Initial price for fixed price, or starting price for auction
|
||||
/// * `currency` - Currency of the price
|
||||
/// * `listing_type` - Type of the listing
|
||||
/// * `expires_at` - Optional expiration date
|
||||
/// * `tags` - Tags for the listing
|
||||
/// * `image_url` - Optional image URL
|
||||
pub fn new(
|
||||
id: Option<u32>,
|
||||
title: impl ToString,
|
||||
description: impl ToString,
|
||||
asset_id: impl ToString,
|
||||
asset_type: AssetType,
|
||||
seller_id: impl ToString,
|
||||
price: f64,
|
||||
currency: impl ToString,
|
||||
listing_type: ListingType,
|
||||
expires_at: Option<DateTime<Utc>>,
|
||||
tags: Vec<String>,
|
||||
image_url: Option<impl ToString>,
|
||||
) -> Self {
|
||||
let mut base_data = BaseModelData::new();
|
||||
if let Some(id) = id {
|
||||
base_data.update_id(id);
|
||||
}
|
||||
/// Create a new listing with default values
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
Self {
|
||||
base_data,
|
||||
title: title.to_string(),
|
||||
description: description.to_string(),
|
||||
asset_id: asset_id.to_string(),
|
||||
asset_type,
|
||||
seller_id: seller_id.to_string(),
|
||||
price,
|
||||
currency: currency.to_string(),
|
||||
listing_type,
|
||||
status: ListingStatus::default(),
|
||||
expires_at,
|
||||
sold_at: None,
|
||||
buyer_id: None,
|
||||
sale_price: None,
|
||||
bids: Vec::new(),
|
||||
tags,
|
||||
image_url: image_url.map(|url| url.to_string()),
|
||||
}
|
||||
/// Set the title of the listing
|
||||
pub fn title(mut self, title: impl ToString) -> Self {
|
||||
self.title = title.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the description of the listing
|
||||
pub fn description(mut self, description: impl ToString) -> Self {
|
||||
self.description = description.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the asset ID of the listing
|
||||
pub fn asset_id(mut self, asset_id: impl ToString) -> Self {
|
||||
self.asset_id = asset_id.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the asset type of the listing
|
||||
pub fn asset_type(mut self, asset_type: AssetType) -> Self {
|
||||
self.asset_type = asset_type;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the seller ID of the listing
|
||||
pub fn seller_id(mut self, seller_id: impl ToString) -> Self {
|
||||
self.seller_id = seller_id.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the price of the listing
|
||||
pub fn price(mut self, price: f64) -> Self {
|
||||
self.price = price;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the currency of the listing
|
||||
pub fn currency(mut self, currency: impl ToString) -> Self {
|
||||
self.currency = currency.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the listing type
|
||||
pub fn listing_type(mut self, listing_type: ListingType) -> Self {
|
||||
self.listing_type = listing_type;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the status of the listing
|
||||
pub fn status(mut self, status: ListingStatus) -> Self {
|
||||
self.status = status;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the expiration date of the listing
|
||||
pub fn expires_at(mut self, expires_at: Option<DateTime<Utc>>) -> Self {
|
||||
self.expires_at = expires_at;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the image URL of the listing
|
||||
pub fn image_url(mut self, image_url: Option<impl ToString>) -> Self {
|
||||
self.image_url = image_url.map(|url| url.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
/// Add a bid to an auction listing
|
||||
pub fn add_bid(mut self, bid: Bid) -> Result<Self, &'static str> {
|
||||
// Check if listing is an auction
|
||||
if self.listing_type != ListingType::Auction {
|
||||
return Err("Bids can only be placed on auction listings");
|
||||
return Err("Cannot add bid to non-auction listing");
|
||||
}
|
||||
|
||||
// Check if listing is active
|
||||
@@ -210,27 +239,51 @@ impl Listing {
|
||||
.max_by(|a, b| a.amount.partial_cmp(&b.amount).unwrap())
|
||||
}
|
||||
|
||||
/// Set the buyer ID for completing a sale
|
||||
pub fn buyer_id(mut self, buyer_id: impl ToString) -> Self {
|
||||
self.buyer_id = Some(buyer_id.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the sale price for completing a sale
|
||||
pub fn sale_price(mut self, sale_price: f64) -> Self {
|
||||
self.sale_price = Some(sale_price);
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the sold date for completing a sale
|
||||
pub fn sold_at(mut self, sold_at: Option<DateTime<Utc>>) -> Self {
|
||||
self.sold_at = sold_at;
|
||||
self
|
||||
}
|
||||
|
||||
/// 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) -> Result<Self, &'static str> {
|
||||
if self.status != ListingStatus::Active {
|
||||
return Err("Cannot complete sale for inactive listing");
|
||||
}
|
||||
|
||||
if self.buyer_id.is_none() {
|
||||
return Err("Buyer ID must be set before completing sale");
|
||||
}
|
||||
|
||||
if self.sale_price.is_none() {
|
||||
return Err("Sale price must be set before completing sale");
|
||||
}
|
||||
|
||||
self.status = ListingStatus::Sold;
|
||||
self.buyer_id = Some(buyer_id.to_string());
|
||||
self.sale_price = Some(sale_price);
|
||||
self.sold_at = Some(Utc::now());
|
||||
|
||||
if self.sold_at.is_none() {
|
||||
self.sold_at = Some(Utc::now());
|
||||
}
|
||||
|
||||
// If this was an auction, accept the winning bid and reject others
|
||||
if self.listing_type == ListingType::Auction {
|
||||
let buyer_id_str = self.buyer_id.as_ref().unwrap().to_string();
|
||||
let sale_price_val = self.sale_price.unwrap();
|
||||
|
||||
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() == buyer_id_str && bid.amount == sale_price_val {
|
||||
bid.status = BidStatus::Accepted;
|
||||
} else {
|
||||
bid.status = BidStatus::Rejected;
|
||||
@@ -279,34 +332,11 @@ impl Listing {
|
||||
self
|
||||
}
|
||||
|
||||
/// Add tags to the listing
|
||||
pub fn add_tags(mut self, tags: Vec<impl ToString>) -> Self {
|
||||
for tag in tags {
|
||||
self.tags.push(tag.to_string());
|
||||
}
|
||||
/// Add a single tag to the listing
|
||||
pub fn add_tag(mut self, tag: impl ToString) -> Self {
|
||||
self.tags.push(tag.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
/// Update the listing details
|
||||
pub fn update_details(
|
||||
mut self,
|
||||
title: Option<impl ToString>,
|
||||
description: Option<impl ToString>,
|
||||
price: Option<f64>,
|
||||
image_url: Option<impl ToString>,
|
||||
) -> Self {
|
||||
if let Some(title) = title {
|
||||
self.title = title.to_string();
|
||||
}
|
||||
if let Some(description) = description {
|
||||
self.description = description.to_string();
|
||||
}
|
||||
if let Some(price) = price {
|
||||
self.price = price;
|
||||
}
|
||||
if let Some(image_url) = image_url {
|
||||
self.image_url = Some(image_url.to_string());
|
||||
}
|
||||
self
|
||||
}
|
||||
// update_details method removed as we now have individual setter methods for each field
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user