merge branches and cleanup db

This commit is contained in:
timurgordon
2025-06-27 12:11:04 +03:00
parent 5563d7e27e
commit 1f9ec01934
177 changed files with 1202 additions and 174 deletions

View File

@@ -1,7 +1,7 @@
use chrono::{Duration, Utc};
use heromodels::db::{Collection, Db};
use heromodels::models::finance::marketplace::{Bid, Listing, ListingType};
use heromodels::models::finance::asset::AssetType;
use heromodels::models::finance::marketplace::{Bid, Listing, ListingType};
use heromodels_core::Model;
// Helper function to print listing details
@@ -16,32 +16,32 @@ fn print_listing_details(listing: &Listing) {
println!("Price: {} {}", listing.price, listing.currency);
println!("Listing Type: {:?}", listing.listing_type);
println!("Status: {:?}", listing.status);
if let Some(expires_at) = listing.expires_at {
println!("Expires At: {}", expires_at);
} else {
println!("Expires At: Never");
}
if let Some(sold_at) = listing.sold_at {
println!("Sold At: {}", sold_at);
}
if let Some(buyer_id) = &listing.buyer_id {
println!("Buyer ID: {}", buyer_id);
}
if let Some(sale_price) = listing.sale_price {
println!("Sale Price: {} {}", sale_price, listing.currency);
}
println!("Bids: {}", listing.bids.len());
println!("Tags: {:?}", listing.tags);
if let Some(image_url) = &listing.image_url {
println!("Image URL: {}", image_url);
}
println!("Created At: {}", listing.base_data.created_at);
println!("Modified At: {}", listing.base_data.modified_at);
}
@@ -77,7 +77,11 @@ fn main() {
"USD",
ListingType::FixedPrice,
Some(Utc::now() + Duration::days(30)), // Expires in 30 days
vec!["music".to_string(), "instrument".to_string(), "vintage".to_string()],
vec![
"music".to_string(),
"instrument".to_string(),
"vintage".to_string(),
],
Some("https://example.com/images/vintage_guitar.jpg"),
);
@@ -93,7 +97,11 @@ fn main() {
"USD",
ListingType::Auction,
Some(Utc::now() + Duration::days(7)), // Auction ends in 7 days
vec!["art".to_string(), "painting".to_string(), "antique".to_string()],
vec![
"art".to_string(),
"painting".to_string(),
"antique".to_string(),
],
Some("https://example.com/images/rare_painting.jpg"),
);
@@ -114,9 +122,21 @@ fn main() {
);
// Save all listings to database and get their assigned IDs and updated models
let (fixed_price_id, db_fixed_price) = db.collection().expect("can open listing collection").set(&fixed_price_listing).expect("can set listing");
let (auction_id, db_auction) = db.collection().expect("can open listing collection").set(&auction_listing).expect("can set listing");
let (exchange_id, db_exchange) = db.collection().expect("can open listing collection").set(&exchange_listing).expect("can set listing");
let (fixed_price_id, db_fixed_price) = db
.collection()
.expect("can open listing collection")
.set(&fixed_price_listing)
.expect("can set listing");
let (auction_id, db_auction) = db
.collection()
.expect("can open listing collection")
.set(&auction_listing)
.expect("can set listing");
let (exchange_id, db_exchange) = db
.collection()
.expect("can open listing collection")
.set(&exchange_listing)
.expect("can set listing");
println!("Fixed Price Listing assigned ID: {}", fixed_price_id);
println!("Auction Listing assigned ID: {}", auction_id);
@@ -138,17 +158,13 @@ fn main() {
// Create bids for the auction listing
let bid1 = Bid::new(
auction_id,
101, // Bidder ID
5200.0,
"USD",
auction_id, 101, // Bidder ID
5200.0, "USD",
);
let bid2 = Bid::new(
auction_id,
102, // Bidder ID
5500.0,
"USD",
auction_id, 102, // Bidder ID
5500.0, "USD",
);
// Print the bids
@@ -166,7 +182,8 @@ fn main() {
.expect("can add second bid");
// Save the updated auction listing
let (_, db_updated_auction) = db.collection()
let (_, db_updated_auction) = db
.collection()
.expect("can open listing collection")
.set(&updated_auction)
.expect("can set updated auction");
@@ -189,7 +206,8 @@ fn main() {
.expect("can complete sale");
// Save the updated listing
let (_, db_completed_fixed_price) = db.collection()
let (_, db_completed_fixed_price) = db
.collection()
.expect("can open listing collection")
.set(&completed_fixed_price)
.expect("can set completed listing");
@@ -204,14 +222,15 @@ fn main() {
// Store the bidder_id and amount before moving db_updated_auction
let bidder_id = db_updated_auction.highest_bid().unwrap().bidder_id;
let amount = db_updated_auction.highest_bid().unwrap().amount;
// Now complete the sale
let completed_auction = db_updated_auction
.complete_sale(bidder_id.to_string(), amount)
.expect("can complete auction");
// Save the updated auction listing
let (_, db_completed_auction) = db.collection()
let (_, db_completed_auction) = db
.collection()
.expect("can open listing collection")
.set(&completed_auction)
.expect("can set completed auction");
@@ -223,12 +242,11 @@ fn main() {
println!("\n--- Cancelling a Listing ---");
// Cancel the exchange listing
let cancelled_exchange = db_exchange
.cancel()
.expect("can cancel listing");
let cancelled_exchange = db_exchange.cancel().expect("can cancel listing");
// Save the updated listing
let (_, db_cancelled_exchange) = db.collection()
let (_, db_cancelled_exchange) = db
.collection()
.expect("can open listing collection")
.set(&cancelled_exchange)
.expect("can set cancelled listing");
@@ -256,7 +274,8 @@ fn main() {
);
// Save the expired listing
let (expired_id, db_expired) = db.collection()
let (expired_id, db_expired) = db
.collection()
.expect("can open listing collection")
.set(&expired_listing)
.expect("can set expired listing");
@@ -267,7 +286,8 @@ fn main() {
let checked_expired = db_expired.check_expiration();
// Save the checked listing
let (_, db_checked_expired) = db.collection()
let (_, db_checked_expired) = db
.collection()
.expect("can open listing collection")
.set(&checked_expired)
.expect("can set checked listing");
@@ -295,7 +315,8 @@ fn main() {
);
// Save the listing
let (update_id, db_to_update) = db.collection()
let (update_id, db_to_update) = db
.collection()
.expect("can open listing collection")
.set(&listing_to_update)
.expect("can set listing to update");
@@ -315,7 +336,8 @@ fn main() {
.add_tags(vec!["updated".to_string(), "premium".to_string()]);
// Save the updated listing
let (_, db_updated_listing) = db.collection()
let (_, db_updated_listing) = db
.collection()
.expect("can open listing collection")
.set(&updated_listing)
.expect("can set updated listing");