bson
This commit is contained in:
parent
e4ec306277
commit
21adfaee13
3 changed files with 121 additions and 11 deletions
124
src/item.rs
124
src/item.rs
|
@ -197,13 +197,35 @@ impl Item {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Price(String);
|
||||
pub struct Price {
|
||||
pub value: f64,
|
||||
pub currency: String,
|
||||
}
|
||||
|
||||
impl Price {
|
||||
fn new(price: &str) -> Self {
|
||||
// todo : implement
|
||||
|
||||
Self(price.to_string())
|
||||
Self {
|
||||
value: 0.0,
|
||||
currency: "€".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn as_bson(&self) -> mongodb::bson::Bson {
|
||||
mongodb::bson::bson!({
|
||||
"value": self.value,
|
||||
"currency": self.currency.clone()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<mongodb::bson::Document> for Price {
|
||||
fn from(value: mongodb::bson::Document) -> Self {
|
||||
Self {
|
||||
value: value.get_f64("value").unwrap(),
|
||||
currency: value.get_str("currency").unwrap().to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -218,16 +240,102 @@ pub struct Variant {
|
|||
pub variant: String,
|
||||
}
|
||||
|
||||
pub struct Transaction {}
|
||||
pub struct BatchTransaction {
|
||||
pub uuid: String,
|
||||
pub transactions: Vec<String>,
|
||||
}
|
||||
|
||||
impl Transaction {
|
||||
pub fn new(item: &str, variant: &str, price: Price, origin: &str) -> Self {
|
||||
// todo : implement
|
||||
Self {}
|
||||
impl BatchTransaction {
|
||||
pub fn new(transactions: Vec<String>) -> Self {
|
||||
Self {
|
||||
uuid: uuid::Uuid::new_v4().to_string(),
|
||||
transactions,
|
||||
}
|
||||
}
|
||||
|
||||
fn as_doc(self) -> mongodb::bson::Document {
|
||||
mongodb::bson::doc! {}
|
||||
mongodb::bson::doc! {
|
||||
"uuid": self.uuid,
|
||||
"kind": "batch",
|
||||
"transactions": self.transactions
|
||||
}
|
||||
}
|
||||
|
||||
fn from(b: mongodb::bson::Document) -> Self {
|
||||
let uuid = b.get_str("uuid").unwrap().to_string();
|
||||
let transactions = b
|
||||
.get_array("transactions")
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|x| x.as_str().unwrap().to_string())
|
||||
.collect();
|
||||
Self { uuid, transactions }
|
||||
}
|
||||
}
|
||||
|
||||
pub enum TransactionType {
|
||||
Batch(BatchTransaction),
|
||||
Normal(Transaction),
|
||||
}
|
||||
|
||||
impl TransactionType {
|
||||
pub fn from(b: mongodb::bson::Document) -> Self {
|
||||
if let Ok(kind) = b.get_str("kind") {
|
||||
if kind == "batch" {
|
||||
return Self::Batch(BatchTransaction::from(b));
|
||||
}
|
||||
}
|
||||
Self::Normal(Transaction::from(b))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Transaction {
|
||||
pub uuid: String,
|
||||
pub item: String,
|
||||
pub variant: String,
|
||||
pub price: Price,
|
||||
pub origin: String,
|
||||
pub timestamp: i64,
|
||||
}
|
||||
|
||||
impl Transaction {
|
||||
pub fn new(item: &str, variant: &str, price: Price, origin: &str) -> Self {
|
||||
Self {
|
||||
uuid: uuid::Uuid::new_v4().to_string(),
|
||||
item: item.to_string(),
|
||||
variant: variant.to_string(),
|
||||
price,
|
||||
origin: origin.to_string(),
|
||||
timestamp: chrono::Utc::now().timestamp(),
|
||||
}
|
||||
}
|
||||
|
||||
fn as_doc(self) -> mongodb::bson::Document {
|
||||
mongodb::bson::doc! {
|
||||
"uuid": self.uuid,
|
||||
"item": self.item,
|
||||
"variant": self.variant,
|
||||
"price": self.price.as_bson(),
|
||||
"origin": self.origin,
|
||||
"timestamp": self.timestamp
|
||||
}
|
||||
}
|
||||
|
||||
fn from(b: mongodb::bson::Document) -> Self {
|
||||
let uuid = b.get_str("uuid").unwrap().to_string();
|
||||
let item = b.get_str("item").unwrap().to_string();
|
||||
let variant = b.get_str("variant").unwrap().to_string();
|
||||
let origin = b.get_str("origin").unwrap().to_string();
|
||||
let timestamp = b.get_i64("timestamp").unwrap();
|
||||
let price = Price::from(b);
|
||||
Self {
|
||||
uuid,
|
||||
item,
|
||||
variant,
|
||||
price,
|
||||
origin,
|
||||
timestamp,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue