This commit is contained in:
JMARyA 2024-08-28 09:38:10 +02:00
parent 92e2da9cae
commit acf5ebae78
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
6 changed files with 56 additions and 47 deletions

View file

@ -3,10 +3,10 @@ use mongodb::bson::doc;
use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::transaction::{Consumed, Price, Transaction};
use crate::transaction::{Price, Transaction};
pub fn sort_by_timestamp() -> Option<mongodb::bson::Document> {
Some(doc! { "timestamp": mongod::Sort::Descending })
pub fn sort_by_timestamp() -> mongodb::bson::Document {
doc! { "timestamp": mongod::Sort::Descending }
}
pub fn timestamp_range(year: i32, month: u32) -> (i64, i64) {
@ -39,12 +39,10 @@ pub fn timestamp_range(year: i32, month: u32) -> (i64, i64) {
pub struct Variant {
/// Associated Item
pub item: String,
/// Variant Name
/// Variant ID
pub variant: String,
/// Amount of items this variant represents
pub amount: u64,
/// Dependencies for the item variant.
pub depends: Vec<String>,
/// Variant Name
pub name: String,
}
impl Variant {
@ -53,23 +51,14 @@ impl Variant {
Self {
item: item.to_string(),
variant: variant.to_string(),
amount: json
name: json
.as_mapping()
.unwrap()
.get("amount")
.map_or(1, |x| x.as_u64().unwrap()),
depends: json
.as_mapping()
.get("name")
.unwrap()
.get("depends")
.map(|x| {
x.as_sequence()
.unwrap()
.iter()
.map(|x| x.as_str().unwrap().to_string())
.collect()
})
.unwrap_or_default(),
.as_str()
.unwrap()
.to_string(),
}
}
@ -167,7 +156,7 @@ impl Variant {
}
},
None,
sort_by_timestamp(),
Some(sort_by_timestamp()),
)
.await
.unwrap()
@ -205,7 +194,7 @@ impl Variant {
filter.insert("origin", origin);
}
Transaction::find(filter, Some(1), sort_by_timestamp())
Transaction::find(filter, Some(1), Some(sort_by_timestamp()))
.await
.unwrap()
.first()
@ -230,8 +219,7 @@ impl Variant {
json!({
"item": self.item,
"variant": self.variant,
"amount": self.amount as u32,
"depends": self.depends
"name": self.name
})
}
}