From e50b51c829b6ef2b3f2c6af87644361ccb803e6f Mon Sep 17 00:00:00 2001 From: JMARyA Date: Mon, 6 May 2024 08:24:23 +0200 Subject: [PATCH] variants --- Cargo.lock | 1 + Cargo.toml | 1 + src/item.rs | 83 ++++++++++++++---------------------------- src/main.rs | 1 - src/routes/item/mod.rs | 14 ------- src/variant.rs | 15 ++++++++ 6 files changed, 44 insertions(+), 71 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 698091b..00e3231 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -484,6 +484,7 @@ dependencies = [ "mongodb", "serde", "serde_json", + "serde_yaml", "tokio", "toml", "uuid", diff --git a/Cargo.toml b/Cargo.toml index 5c66229..ae6c4ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ mdq = { git = "https://git.hydrar.de/mdtools/mdq", version = "0.3.0" } mongodb = "2.8.0" serde = { version = "1.0.195", features = ["derive"] } serde_json = "1.0.111" +serde_yaml = "0.9.34" tokio = { version = "1.35.1", features = ["full"] } toml = "0.8.8" uuid = { version = "1.8.0", features = ["v4"] } diff --git a/src/item.rs b/src/item.rs index 5ca39ed..3873378 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1,4 +1,4 @@ -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use crate::{collect_results, id_of}; use futures::TryStreamExt; @@ -30,10 +30,10 @@ pub struct ItemInventoryEntry { pub price: f64, } -#[derive(serde::Deserialize, serde::Serialize)] pub struct ItemEntry { pub name: String, pub category: String, + pub variants: HashMap, } impl ItemEntry { @@ -53,7 +53,29 @@ impl ItemEntry { .as_str() .unwrap() .to_string(); - Self { name, category } + + let mut variants = HashMap::new(); + + for (variant_name, variant) in doc + .frontmatter + .as_mapping() + .unwrap() + .get("variants") + .unwrap() + .as_mapping() + .unwrap() + { + variants.insert( + variant_name.as_str().unwrap().to_string(), + Variant::from_yml(variant, variant_name.as_str().unwrap(), &name), + ); + } + + Self { + name, + category, + variants, + } } pub async fn init_db(&self, mongodb: &mongodb::Client) { @@ -129,61 +151,10 @@ impl Item { } pub async fn get_variants(&self) -> Vec { - let variants: Collection = - self.db.database("cdb").collection("item_variants"); - let mut found_variants = variants - .find( - doc! { - "item_name": self.item.name.clone() - }, - None, - ) - .await - .unwrap(); - - let mut ret = vec!["Generic".to_string()]; - - while let Some(doc) = found_variants.try_next().await.unwrap() { - ret.push(doc.variant_name); - } - - ret + self.item.variants.keys().cloned().collect() } pub async fn variant(&self, variant: &str) -> Option { - let variants: Collection = - self.db.database("cdb").collection("variants"); - let res = variants - .find_one( - doc! { - "item": self.item.name.clone(), - "variant": variant - }, - None, - ) - .await - .unwrap()?; - - Some(Variant { - item: self.item.name.clone(), - variant: variant.to_string(), - }) - } - - pub async fn add_variant(&self, name: &str, amount: usize) { - let variants: Collection = - self.db.database("cdb").collection("variants"); - variants - .insert_one( - mongodb::bson::doc! { - "_id": format!("{}-{}", self.item.name.clone(), name), - "item": self.item.name.clone(), - "variant": name, - "amount": amount as i64 - }, - None, - ) - .await - .unwrap(); + self.item.variants.get(variant).map(|x| x.clone()) } } diff --git a/src/main.rs b/src/main.rs index 9ec2c23..0863f05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,7 +43,6 @@ async fn main() -> std::io::Result<()> { .service(routes::item::supply_route) .service(routes::item::item_variants_page) .service(routes::item::get_items_route) - .service(routes::item::add_variant_route) }) .bind(("0.0.0.0".to_string(), 8080))? .run() diff --git a/src/routes/item/mod.rs b/src/routes/item/mod.rs index 1ce731d..cf4ef16 100644 --- a/src/routes/item/mod.rs +++ b/src/routes/item/mod.rs @@ -61,20 +61,6 @@ pub struct AddVariantForm { pub amount: usize, } -#[post("/item/{item_id}/variant")] -pub async fn add_variant_route( - r: HttpRequest, - f: actix_web::web::Form, -) -> actix_web::Result { - let id = r.match_info().query("item_id"); - let itemdb = get_itemdb!(r); - let item = itemdb - .get_item(id) - .ok_or_else(|| bad_req("The item does not exist"))?; - item.add_variant(&f.variant, f.amount).await; - Ok(HttpResponse::Ok()) -} - #[get("/item/{item_id}/variants")] pub async fn item_variants_page(r: HttpRequest) -> actix_web::Result { let id = r.match_info().query("item_id"); diff --git a/src/variant.rs b/src/variant.rs index d8157c8..6ceb409 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -11,12 +11,27 @@ use crate::{ /// in the real world. It may include attributes or properties that deviate from /// the standard definition of the item. For example, different colors, sizes, or /// configurations. +#[derive(Debug, Clone)] pub struct Variant { pub item: String, pub variant: String, + pub amount: u64, } impl Variant { + pub fn from_yml(json: &serde_yaml::Value, variant: &str, item: &str) -> Self { + Self { + item: item.to_string(), + variant: variant.to_string(), + amount: json + .as_mapping() + .unwrap() + .get("amount") + .map(|x| x.as_u64().unwrap()) + .unwrap_or(1), + } + } + pub async fn demand(&self, uuid: &str, price: Price, destination: String) -> Option { let db = get_mongo!();