cdb/src/transaction.rs

114 lines
2.9 KiB
Rust
Raw Normal View History

2024-08-13 02:13:38 +00:00
use mongod::{
derive::{Model, Referencable},
2024-08-28 07:14:33 +00:00
Model, Validate,
2024-08-13 02:13:38 +00:00
};
2024-06-22 00:05:22 +00:00
use mongodb::bson::doc;
2024-07-24 14:38:56 +00:00
use serde::{Deserialize, Serialize};
2024-06-22 00:05:22 +00:00
use serde_json::json;
2024-08-28 07:14:33 +00:00
/// A Transaction of an Item Variant
2024-07-24 14:38:56 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)]
pub struct Transaction {
2024-08-28 07:14:33 +00:00
/// UUID
2024-07-24 14:38:56 +00:00
pub _id: String,
2024-08-28 07:14:33 +00:00
/// Associated Item
2024-07-24 14:38:56 +00:00
pub item: String,
2024-08-28 07:14:33 +00:00
/// Associated Variant
2024-07-24 14:38:56 +00:00
pub variant: String,
2024-08-28 07:14:33 +00:00
/// Price of obtaining the Item
2024-07-24 14:38:56 +00:00
pub price: Price,
2024-08-28 07:14:33 +00:00
/// Origin of the Item
2024-07-24 14:38:56 +00:00
pub origin: Option<String>,
2024-08-28 07:14:33 +00:00
/// Info on consumption of the Item
2024-07-24 14:38:56 +00:00
pub consumed: Option<Consumed>,
2024-08-28 07:14:33 +00:00
/// Timestamp of the Transaction
2024-07-24 14:38:56 +00:00
pub timestamp: i64,
2024-05-03 16:22:59 +00:00
}
2024-07-24 14:38:56 +00:00
impl Validate for Transaction {
async fn validate(&self) -> Result<(), String> {
Ok(())
2024-05-03 16:22:59 +00:00
}
}
2024-08-28 07:14:33 +00:00
/// Information about consumed Items
2024-07-24 14:38:56 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Consumed {
2024-08-28 07:14:33 +00:00
/// Destination of the Item or who consumed it
2024-07-24 14:38:56 +00:00
pub destination: String,
2024-08-28 07:14:33 +00:00
/// Price the Item was exported or consumed at
2024-05-03 16:22:59 +00:00
pub price: Price,
2024-08-28 07:14:33 +00:00
/// Timestamp of Consumption
pub timestamp: i64,
2024-05-03 16:22:59 +00:00
}
impl Transaction {
2024-07-24 14:38:56 +00:00
pub fn new(item: &str, variant: &str, price: Price, origin: Option<&str>) -> Self {
2024-05-03 16:22:59 +00:00
Self {
2024-07-24 14:38:56 +00:00
_id: uuid::Uuid::new_v4().to_string(),
2024-05-03 16:22:59 +00:00
item: item.to_string(),
variant: variant.to_string(),
price,
2024-07-24 14:38:56 +00:00
consumed: None,
2024-08-28 07:38:10 +00:00
origin: origin.map(std::string::ToString::to_string),
2024-05-03 16:22:59 +00:00
timestamp: chrono::Utc::now().timestamp(),
}
}
2024-08-28 07:14:33 +00:00
/// Consumes the Item with `price` and `destination`
pub async fn consume(&mut self, price: Price, destination: &str) {
self.update(&json!({
"consumed": Consumed{destination: destination.to_string(),price, timestamp: chrono::Utc::now().timestamp() }
}))
.await
2024-08-28 07:38:10 +00:00
.ok().unwrap();
2024-08-28 07:14:33 +00:00
}
2024-06-22 00:05:22 +00:00
pub fn api_json(&self) -> serde_json::Value {
json!({
2024-07-24 14:38:56 +00:00
"uuid": self._id,
2024-06-22 00:05:22 +00:00
"item": self.item,
"variant": self.variant,
"price": self.price,
"origin": self.origin,
2024-08-28 07:14:33 +00:00
"timestamp": self.timestamp,
"consumed": self.consumed
2024-06-22 00:05:22 +00:00
})
}
2024-06-21 19:14:45 +00:00
}
2024-05-03 16:22:59 +00:00
2024-08-28 07:14:33 +00:00
/// Economic Price
2024-07-24 14:38:56 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
2024-05-03 16:22:59 +00:00
pub struct Price {
2024-08-28 07:14:33 +00:00
/// Value of the currency
2024-05-03 16:22:59 +00:00
pub value: f64,
2024-08-28 07:14:33 +00:00
/// Kind of currency
2024-05-03 16:22:59 +00:00
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
impl TryFrom<String> for Price {
type Error = ();
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::parse(&value).ok_or(())
}
}