variants
This commit is contained in:
parent
c6bcd9ca07
commit
e50b51c829
6 changed files with 44 additions and 71 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -484,6 +484,7 @@ dependencies = [
|
|||
"mongodb",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_yaml",
|
||||
"tokio",
|
||||
"toml",
|
||||
"uuid",
|
||||
|
|
|
@ -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"] }
|
||||
|
|
83
src/item.rs
83
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<String, Variant>,
|
||||
}
|
||||
|
||||
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<String> {
|
||||
let variants: Collection<ItemVariantEntry> =
|
||||
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<Variant> {
|
||||
let variants: Collection<mongodb::bson::Document> =
|
||||
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<mongodb::bson::Document> =
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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<AddVariantForm>,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
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<impl Responder> {
|
||||
let id = r.match_info().query("item_id");
|
||||
|
|
|
@ -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<String> {
|
||||
let db = get_mongo!();
|
||||
|
||||
|
|
Loading…
Reference in a new issue