From d913772f3b1d657ae1d591540b1510815e4b5db3 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Mon, 29 Apr 2024 16:05:09 +0200 Subject: [PATCH] work --- docs/Item.md | 3 +++ src/item.rs | 51 ++++++++++++++++++++++++------------------ src/main.rs | 1 + src/routes/item/mod.rs | 31 ++++++++++++++----------- 4 files changed, 51 insertions(+), 35 deletions(-) create mode 100644 docs/Item.md diff --git a/docs/Item.md b/docs/Item.md new file mode 100644 index 0000000..542da9f --- /dev/null +++ b/docs/Item.md @@ -0,0 +1,3 @@ +# Item +An item is the base concept for CDB. Everything is an Item. An Item describes a general product or object. An item can have multiple variants, beeing the concrete instances of the Item like brand products or different flavors, etc. + diff --git a/src/item.rs b/src/item.rs index 8d15583..72f3c09 100644 --- a/src/item.rs +++ b/src/item.rs @@ -89,6 +89,23 @@ impl ItemEntry { .to_string(); Self { name, category } } + + pub async fn init_db(&self, mongodb: &mongodb::Client) { + log::info!("Adding item {} to DB", self.name); + let items: Collection = + mongodb.database("cdb").collection("items"); + items + .insert_one( + mongodb::bson::doc! { + "oid": self.name.clone(), + "name": self.name.clone(), + "category": self.category.clone() + }, + None, + ) + .await + .unwrap(); + } } pub struct Item { @@ -181,13 +198,14 @@ impl Item { } pub async fn add_variant(&self, name: &str) { - let variants: Collection = - self.db.database("cdb").collection("item_variants"); + let variants: Collection = + self.db.database("cdb").collection("variants"); variants .insert_one( - ItemVariantEntry { - item_name: self.item.name.clone(), - variant_name: name.to_owned(), + mongodb::bson::doc! { + "oid": format!("{}-{}", self.item.name.clone(), name.clone()), + "item": self.item.name.clone(), + "variant": name }, None, ) @@ -312,7 +330,7 @@ impl Transaction { fn as_doc(self) -> mongodb::bson::Document { mongodb::bson::doc! { - "uuid": self.uuid, + "oid": self.uuid, "item": self.item, "variant": self.variant, "price": self.price.as_bson(), @@ -322,7 +340,7 @@ impl Transaction { } fn from(b: mongodb::bson::Document) -> Self { - let uuid = b.get_str("uuid").unwrap().to_string(); + let uuid = b.get_str("oid").unwrap().to_string(); let item = b.get_str("item").unwrap().to_string(); let variant = b.get_str("variant").unwrap().to_string(); let origin = b.get_str("origin").unwrap().to_string(); @@ -373,23 +391,12 @@ impl ItemDB { pub async fn new(dir: &str, mongodb: &str) -> Self { // scan for markdown item entries let index = mdq::Index::new(dir, true); - let mongodb = mongodb::Client::with_uri_str(mongodb).await.unwrap(); - /* + let mongodb = get_mongo!(); + for item in &index.documents { let item = ItemEntry::new(item.clone()); - log::info!("Adding item {} to DB", item.name); - let items: Collection = mongodb.database("cdb").collection("items"); - if let None = - items - .find_one(doc! { "name": item.name.clone()}, None) - .await - .unwrap() - { - items.insert_one(item, None).await.unwrap(); - } else { - // todo : update values - } - }*/ + item.init_db(&mongodb).await; + } Self { index, mongodb } } diff --git a/src/main.rs b/src/main.rs index 94b72b4..1a68411 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,6 +38,7 @@ async fn main() -> std::io::Result<()> { .service(index) .service(routes::item::supply_route) .service(routes::item::item_variants_page) + .service(routes::item::get_items_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 33c7af3..1bc948b 100644 --- a/src/routes/item/mod.rs +++ b/src/routes/item/mod.rs @@ -1,6 +1,5 @@ use actix_web::post; use actix_web::{get, HttpRequest, HttpResponse, Responder}; -use maud::html; use serde::Deserialize; use crate::item; @@ -25,14 +24,14 @@ pub struct SupplyForm { pub async fn supply_route( req: HttpRequest, form: actix_web::web::Form, -) -> impl Responder { +) -> actix_web::Result { let itemdb = get_itemdb!(req); println!("{form:?}"); let variant = itemdb .get_item(&form.item) - .unwrap() + .ok_or_else(|| actix_web::error::ErrorBadRequest("The item does not exist"))? .variant(&form.variant) - .unwrap(); + .ok_or_else(|| actix_web::error::ErrorBadRequest("The variant does not exist"))?; let transaction_id = variant .supply( form.amount.unwrap_or(1), @@ -40,21 +39,27 @@ pub async fn supply_route( &form.origin, ) .await; - actix_web::HttpResponse::Ok().json(serde_json::json!({"uuid": transaction_id})) + Ok(actix_web::HttpResponse::Ok().json(serde_json::json!({"uuid": transaction_id}))) +} + +#[get("/items")] +pub async fn get_items_route(r: HttpRequest) -> impl Responder { + let itemdb = get_itemdb!(r); + let items = itemdb.items(); + actix_web::HttpResponse::Ok().json(serde_json::json!({"items": items})) } #[get("/item/{item_id}/variants")] -pub async fn item_variants_page(r: HttpRequest) -> impl Responder { +pub async fn item_variants_page(r: HttpRequest) -> 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(|| actix_web::error::ErrorBadRequest("The item does not exist"))?; + let variants = item.get_variants().await; - let item = itemdb.get_item(id); - - let variants = item.unwrap().get_variants().await; - - HttpResponse::Ok().json(serde_json::json!({ + Ok(HttpResponse::Ok().json(serde_json::json!({ "item": id, "variants": variants - })) + }))) }