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

@ -1,3 +1,5 @@
use mongod::Model;
use crate::item::Item;
/// Item database
@ -15,6 +17,7 @@ impl ItemDB {
for item in &index.documents {
let item = Item::new(item);
item.insert_overwrite().await.unwrap();
log::info!("Adding item {} to DB", item.name);
}
@ -23,13 +26,11 @@ impl ItemDB {
/// Retrieves an item by name
pub fn get_item(&self, item: &str) -> Option<Item> {
Some(
self.index
.documents
.iter()
.map(Item::new) // <-- todo : performance?
.find(|x| x.name == item)?,
)
self.index
.documents
.iter()
.map(Item::new) // <-- todo : performance?
.find(|x| x.name == item)
}
/// Get all items

View file

@ -27,14 +27,14 @@ use crate::variant::Variant;
/// the name of the item and its category.
#[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)]
pub struct Item {
/// The ID is the same as the name
/// The ID
pub _id: String,
/// The name of the Item
pub name: String,
/// Category of the Item
pub category: String,
pub category: Option<String>,
/// The variants of an Item.
/// Each key of the `HashMap<_>` is the name of a variant and contains a `Variant`
/// Each key of the `HashMap<_>` is the ID of a variant and contains a `Variant`
pub variants: HashMap<String, Variant>,
}
@ -47,17 +47,25 @@ impl Validate for Item {
impl Item {
/// Creates a new `Item` from a parsed markdown document
pub fn new(doc: &mdq::Document) -> Self {
let name = std::path::Path::new(&doc.path)
let id = std::path::Path::new(&doc.path)
.file_stem()
.unwrap()
.to_str()
.unwrap()
.to_string();
let category = doc
.frontmatter
.as_mapping()
.unwrap()
.get("category")
.map(|x| x.as_str().unwrap().to_string());
let name = doc
.frontmatter
.as_mapping()
.unwrap()
.get("name")
.unwrap()
.as_str()
.unwrap()
@ -65,22 +73,28 @@ impl Item {
let mut variants = HashMap::new();
for (variant_name, variant) in doc
let variants_lst = doc
.frontmatter
.as_mapping()
.unwrap()
.get("variants")
.map(|x| x.as_mapping().unwrap().clone())
.unwrap_or_default()
{
.unwrap();
assert!(
!variants_lst.is_empty(),
"Item {id} needs at least one variant."
);
for (variant_id, variant) in variants_lst {
variants.insert(
variant_name.as_str().unwrap().to_string(),
Variant::from_yml(&variant, variant_name.as_str().unwrap(), &name),
variant_id.as_str().unwrap().to_string(),
Variant::from_yml(&variant, variant_id.as_str().unwrap(), &id),
);
}
Self {
_id: name.clone(),
_id: id,
name,
category,
variants,

View file

@ -18,9 +18,10 @@ pub struct DemandForm {
price: String,
}
/// Consumes a Transaction with Price and Destination
#[post("/demand", data = "<f>")]
pub async fn demand_route(f: Json<DemandForm>) -> FallibleApiResponse {
let uuid = Variant::demand(
Variant::demand(
&f.uuid,
f.price
.clone()
@ -31,9 +32,10 @@ pub async fn demand_route(f: Json<DemandForm>) -> FallibleApiResponse {
.await
.ok_or_else(|| api_error("Demand failed"))?;
Ok(json!({"uuid": uuid}))
Ok(json!({"ok": 1}))
}
/// Returns all used transactions for Item Variant
#[get("/item/<item_id>/<variant_id>/demand")]
pub async fn demand_log_route(
item_id: &str,

View file

@ -18,6 +18,7 @@ pub struct SupplyForm {
origin: Option<String>,
}
/// Route for supply action. Creates a new Transaction for the specified Item Variant.
#[post("/supply", data = "<form>")]
pub async fn supply_route(form: Json<SupplyForm>, itemdb: &State<ItemDB>) -> FallibleApiResponse {
println!("{form:?}");
@ -40,6 +41,7 @@ pub async fn supply_route(form: Json<SupplyForm>, itemdb: &State<ItemDB>) -> Fal
Ok(json!({"uuid": transaction_id}))
}
/// Returns a list of Transaction UUIDs for the Item Variant
#[get("/item/<item_id>/<variant_id>/supply")]
pub async fn supply_log_route(
item_id: &str,
@ -57,6 +59,7 @@ pub async fn supply_log_route(
Ok(json!(transactions))
}
/// Returns current active Transactions for Item Variant
#[get("/item/<item_id>/<variant_id>/inventory")]
pub async fn inventory_route(
item_id: &str,
@ -77,6 +80,7 @@ pub async fn inventory_route(
.collect::<Vec<_>>()))
}
/// Returns statistics for the Item Variant
#[get("/item/<item_id>/<variant_id>/stat")]
pub async fn variant_stat_route(
item_id: &str,

View file

@ -50,7 +50,7 @@ impl Transaction {
variant: variant.to_string(),
price,
consumed: None,
origin: origin.map(|x| x.to_string()),
origin: origin.map(std::string::ToString::to_string),
timestamp: chrono::Utc::now().timestamp(),
}
}
@ -61,7 +61,7 @@ impl Transaction {
"consumed": Consumed{destination: destination.to_string(),price, timestamp: chrono::Utc::now().timestamp() }
}))
.await
.ok().unwrap()
.ok().unwrap();
}
pub fn api_json(&self) -> serde_json::Value {

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
})
}
}