2024-05-03 16:22:59 +00:00
|
|
|
pub enum TransactionType {
|
|
|
|
Batch(BatchTransaction),
|
|
|
|
Normal(Transaction),
|
|
|
|
}
|
|
|
|
|
2024-06-21 19:14:45 +00:00
|
|
|
impl From<mongodb::bson::Document> for TransactionType {
|
|
|
|
fn from(value: mongodb::bson::Document) -> Self {
|
|
|
|
if let Ok(kind) = value.get_str("kind") {
|
2024-05-03 16:22:59 +00:00
|
|
|
if kind == "batch" {
|
2024-06-21 19:14:45 +00:00
|
|
|
return Self::Batch(value.into());
|
2024-05-03 16:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-21 19:14:45 +00:00
|
|
|
Self::Normal(value.into())
|
2024-05-03 16:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_doc(&self) -> mongodb::bson::Document {
|
|
|
|
mongodb::bson::doc! {
|
|
|
|
"_id": &self.uuid,
|
|
|
|
"item": &self.item,
|
|
|
|
"variant": &self.variant,
|
2024-06-21 19:14:45 +00:00
|
|
|
"price": Into::<mongodb::bson::Bson>::into(self.price.clone()),
|
2024-05-03 16:22:59 +00:00
|
|
|
"origin": &self.origin,
|
|
|
|
"timestamp": self.timestamp
|
|
|
|
}
|
|
|
|
}
|
2024-06-21 19:14:45 +00:00
|
|
|
}
|
2024-05-03 16:22:59 +00:00
|
|
|
|
2024-06-21 19:14:45 +00:00
|
|
|
impl From<mongodb::bson::Document> for Transaction {
|
2024-05-03 16:22:59 +00:00
|
|
|
fn from(b: mongodb::bson::Document) -> Self {
|
|
|
|
let uuid = b.get_str("oid").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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-21 19:14:45 +00:00
|
|
|
impl From<Transaction> for mongodb::bson::Document {
|
|
|
|
fn from(value: Transaction) -> Self {
|
|
|
|
mongodb::bson::doc! {
|
|
|
|
"_id": &value.uuid,
|
|
|
|
"item": &value.item,
|
|
|
|
"variant": &value.variant,
|
|
|
|
"price": Into::<mongodb::bson::Bson>::into(value.price),
|
|
|
|
"origin": &value.origin,
|
|
|
|
"timestamp": value.timestamp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-03 16:22:59 +00:00
|
|
|
pub struct BatchTransaction {
|
|
|
|
pub uuid: String,
|
|
|
|
pub transactions: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BatchTransaction {
|
|
|
|
pub fn new(transactions: Vec<String>) -> Self {
|
|
|
|
Self {
|
|
|
|
uuid: uuid::Uuid::new_v4().to_string(),
|
|
|
|
transactions,
|
|
|
|
}
|
|
|
|
}
|
2024-06-21 19:14:45 +00:00
|
|
|
}
|
2024-05-03 16:22:59 +00:00
|
|
|
|
2024-06-21 19:14:45 +00:00
|
|
|
impl From<mongodb::bson::Document> for BatchTransaction {
|
|
|
|
fn from(value: mongodb::bson::Document) -> Self {
|
|
|
|
let uuid = value.get_str("_id").unwrap().to_string();
|
|
|
|
let transactions = value
|
2024-05-03 16:22:59 +00:00
|
|
|
.get_array("transactions")
|
|
|
|
.unwrap()
|
2024-06-21 19:14:45 +00:00
|
|
|
.iter()
|
2024-05-03 16:22:59 +00:00
|
|
|
.map(|x| x.as_str().unwrap().to_string())
|
|
|
|
.collect();
|
2024-06-21 19:14:45 +00:00
|
|
|
|
2024-05-03 16:22:59 +00:00
|
|
|
Self { uuid, transactions }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-21 19:14:45 +00:00
|
|
|
impl From<BatchTransaction> for mongodb::bson::Document {
|
|
|
|
fn from(value: BatchTransaction) -> Self {
|
|
|
|
mongodb::bson::doc! {
|
|
|
|
"_id": value.uuid,
|
|
|
|
"kind": "batch",
|
|
|
|
"transactions": value.transactions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-03 16:22:59 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Price {
|
|
|
|
pub value: f64,
|
|
|
|
pub currency: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Price {
|
|
|
|
pub fn new(value: f64, currency: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
value,
|
|
|
|
currency: currency.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse(price: &str) -> Option<Self> {
|
|
|
|
let (value, currency) = price.split_once(' ')?;
|
|
|
|
|
|
|
|
Some(Self {
|
|
|
|
value: value.parse().ok()?,
|
|
|
|
currency: currency.to_string(),
|
|
|
|
})
|
|
|
|
}
|
2024-06-21 19:14:45 +00:00
|
|
|
}
|
2024-05-03 16:22:59 +00:00
|
|
|
|
2024-06-21 19:14:45 +00:00
|
|
|
impl From<Price> for mongodb::bson::Bson {
|
|
|
|
fn from(value: Price) -> Self {
|
2024-05-03 16:22:59 +00:00
|
|
|
mongodb::bson::bson!({
|
2024-06-21 19:14:45 +00:00
|
|
|
"value": value.value,
|
|
|
|
"currency": value.currency
|
2024-05-03 16:22:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<String> for Price {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
|
|
|
Self::parse(&value).ok_or(())
|
|
|
|
}
|
|
|
|
}
|