work
This commit is contained in:
parent
21adfaee13
commit
d913772f3b
4 changed files with 51 additions and 35 deletions
3
docs/Item.md
Normal file
3
docs/Item.md
Normal file
|
@ -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.
|
||||
|
51
src/item.rs
51
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::bson::Document> =
|
||||
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<ItemVariantEntry> =
|
||||
self.db.database("cdb").collection("item_variants");
|
||||
let variants: Collection<mongodb::bson::Document> =
|
||||
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<ItemEntry> = 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 }
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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<SupplyForm>,
|
||||
) -> impl Responder {
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
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<impl Responder> {
|
||||
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
|
||||
}))
|
||||
})))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue