145 lines
3.8 KiB
Rust
145 lines
3.8 KiB
Rust
use crate::db::{Listing, ListingDbId, ListingType, MoneyAmount, UserDbId};
|
|
use chrono::{DateTime, Utc};
|
|
|
|
/// New listing data for insertion
|
|
#[derive(Debug, Clone)]
|
|
pub struct DraftListing {
|
|
pub base: DraftListingBase,
|
|
pub fields: DraftListingFields,
|
|
}
|
|
|
|
impl DraftListing {
|
|
pub fn listing_type(&self) -> ListingType {
|
|
match &self.fields {
|
|
DraftListingFields::BasicAuction { .. } => ListingType::BasicAuction,
|
|
DraftListingFields::MultiSlotAuction { .. } => ListingType::MultiSlotAuction,
|
|
DraftListingFields::FixedPriceListing { .. } => ListingType::FixedPriceListing,
|
|
DraftListingFields::BlindAuction { .. } => ListingType::BlindAuction,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct DraftListingBase {
|
|
pub seller_id: UserDbId,
|
|
pub title: String,
|
|
pub description: Option<String>,
|
|
pub starts_at: DateTime<Utc>,
|
|
pub ends_at: DateTime<Utc>,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
#[allow(unused)]
|
|
pub enum DraftListingFields {
|
|
BasicAuction {
|
|
starting_bid: MoneyAmount,
|
|
buy_now_price: Option<MoneyAmount>,
|
|
min_increment: MoneyAmount,
|
|
anti_snipe_minutes: Option<i32>,
|
|
},
|
|
MultiSlotAuction {
|
|
starting_bid: MoneyAmount,
|
|
buy_now_price: MoneyAmount,
|
|
min_increment: Option<MoneyAmount>,
|
|
slots_available: i32,
|
|
anti_snipe_minutes: i32,
|
|
},
|
|
FixedPriceListing {
|
|
buy_now_price: MoneyAmount,
|
|
slots_available: i32,
|
|
},
|
|
BlindAuction {
|
|
starting_bid: MoneyAmount,
|
|
},
|
|
}
|
|
|
|
impl From<Listing> for DraftListing {
|
|
fn from(listing: Listing) -> Self {
|
|
Self {
|
|
id: Some(listing.base.id),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[allow(unused)]
|
|
impl DraftListingBase {
|
|
pub fn new(
|
|
seller_id: UserDbId,
|
|
title: String,
|
|
description: Option<String>,
|
|
starts_at: DateTime<Utc>,
|
|
ends_at: DateTime<Utc>,
|
|
) -> Self {
|
|
Self {
|
|
seller_id,
|
|
title,
|
|
description,
|
|
starts_at,
|
|
ends_at,
|
|
}
|
|
}
|
|
|
|
/// Create a new basic auction listing
|
|
pub fn new_basic_auction(
|
|
self,
|
|
starting_bid: MoneyAmount,
|
|
buy_now_price: Option<MoneyAmount>,
|
|
min_increment: MoneyAmount,
|
|
anti_snipe_minutes: Option<i32>,
|
|
) -> DraftListing {
|
|
DraftListing {
|
|
base: self,
|
|
fields: DraftListingFields::BasicAuction {
|
|
starting_bid,
|
|
buy_now_price,
|
|
min_increment,
|
|
anti_snipe_minutes,
|
|
},
|
|
}
|
|
}
|
|
|
|
/// Create a new multi-slot auction listing
|
|
pub fn new_multi_slot_auction(
|
|
self,
|
|
starting_bid: MoneyAmount,
|
|
buy_now_price: MoneyAmount,
|
|
min_increment: Option<MoneyAmount>,
|
|
slots_available: i32,
|
|
anti_snipe_minutes: i32,
|
|
) -> DraftListing {
|
|
DraftListing {
|
|
base: self,
|
|
fields: DraftListingFields::MultiSlotAuction {
|
|
starting_bid,
|
|
buy_now_price,
|
|
min_increment,
|
|
slots_available,
|
|
anti_snipe_minutes,
|
|
},
|
|
}
|
|
}
|
|
|
|
/// Create a new fixed price listing
|
|
pub fn new_fixed_price_listing(
|
|
self,
|
|
buy_now_price: MoneyAmount,
|
|
slots_available: i32,
|
|
) -> DraftListing {
|
|
DraftListing {
|
|
base: self,
|
|
fields: DraftListingFields::FixedPriceListing {
|
|
buy_now_price,
|
|
slots_available,
|
|
},
|
|
}
|
|
}
|
|
|
|
/// Create a new blind auction listing
|
|
pub fn new_blind_auction(self, starting_bid: MoneyAmount) -> DraftListing {
|
|
DraftListing {
|
|
base: self,
|
|
fields: DraftListingFields::BlindAuction { starting_bid },
|
|
}
|
|
}
|
|
}
|