This commit is contained in:
JMARyA 2024-04-29 13:37:36 +02:00
parent e4ec306277
commit 21adfaee13
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 121 additions and 11 deletions

7
Cargo.lock generated
View file

@ -474,6 +474,7 @@ name = "cdb"
version = "0.1.0"
dependencies = [
"actix-web",
"chrono",
"env_logger",
"futures",
"log",
@ -496,16 +497,16 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.31"
version = "0.4.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38"
checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-targets 0.48.5",
"windows-targets 0.52.0",
]
[[package]]

View file

@ -5,6 +5,7 @@ edition = "2021"
[dependencies]
actix-web = "4.4.1"
chrono = "0.4.38"
env_logger = "0.10.1"
futures = "0.3.30"
log = "0.4.20"

View file

@ -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,
}
}
}