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.
|
||||||
|
|
49
src/item.rs
49
src/item.rs
|
@ -89,6 +89,23 @@ impl ItemEntry {
|
||||||
.to_string();
|
.to_string();
|
||||||
Self { name, category }
|
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 {
|
pub struct Item {
|
||||||
|
@ -181,13 +198,14 @@ impl Item {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn add_variant(&self, name: &str) {
|
pub async fn add_variant(&self, name: &str) {
|
||||||
let variants: Collection<ItemVariantEntry> =
|
let variants: Collection<mongodb::bson::Document> =
|
||||||
self.db.database("cdb").collection("item_variants");
|
self.db.database("cdb").collection("variants");
|
||||||
variants
|
variants
|
||||||
.insert_one(
|
.insert_one(
|
||||||
ItemVariantEntry {
|
mongodb::bson::doc! {
|
||||||
item_name: self.item.name.clone(),
|
"oid": format!("{}-{}", self.item.name.clone(), name.clone()),
|
||||||
variant_name: name.to_owned(),
|
"item": self.item.name.clone(),
|
||||||
|
"variant": name
|
||||||
},
|
},
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
|
@ -312,7 +330,7 @@ impl Transaction {
|
||||||
|
|
||||||
fn as_doc(self) -> mongodb::bson::Document {
|
fn as_doc(self) -> mongodb::bson::Document {
|
||||||
mongodb::bson::doc! {
|
mongodb::bson::doc! {
|
||||||
"uuid": self.uuid,
|
"oid": self.uuid,
|
||||||
"item": self.item,
|
"item": self.item,
|
||||||
"variant": self.variant,
|
"variant": self.variant,
|
||||||
"price": self.price.as_bson(),
|
"price": self.price.as_bson(),
|
||||||
|
@ -322,7 +340,7 @@ impl Transaction {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from(b: mongodb::bson::Document) -> Self {
|
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 item = b.get_str("item").unwrap().to_string();
|
||||||
let variant = b.get_str("variant").unwrap().to_string();
|
let variant = b.get_str("variant").unwrap().to_string();
|
||||||
let origin = b.get_str("origin").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 {
|
pub async fn new(dir: &str, mongodb: &str) -> Self {
|
||||||
// scan for markdown item entries
|
// scan for markdown item entries
|
||||||
let index = mdq::Index::new(dir, true);
|
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 {
|
for item in &index.documents {
|
||||||
let item = ItemEntry::new(item.clone());
|
let item = ItemEntry::new(item.clone());
|
||||||
log::info!("Adding item {} to DB", item.name);
|
item.init_db(&mongodb).await;
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}*/
|
|
||||||
|
|
||||||
Self { index, mongodb }
|
Self { index, mongodb }
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ async fn main() -> std::io::Result<()> {
|
||||||
.service(index)
|
.service(index)
|
||||||
.service(routes::item::supply_route)
|
.service(routes::item::supply_route)
|
||||||
.service(routes::item::item_variants_page)
|
.service(routes::item::item_variants_page)
|
||||||
|
.service(routes::item::get_items_route)
|
||||||
})
|
})
|
||||||
.bind(("0.0.0.0".to_string(), 8080))?
|
.bind(("0.0.0.0".to_string(), 8080))?
|
||||||
.run()
|
.run()
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use actix_web::post;
|
use actix_web::post;
|
||||||
use actix_web::{get, HttpRequest, HttpResponse, Responder};
|
use actix_web::{get, HttpRequest, HttpResponse, Responder};
|
||||||
use maud::html;
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::item;
|
use crate::item;
|
||||||
|
@ -25,14 +24,14 @@ pub struct SupplyForm {
|
||||||
pub async fn supply_route(
|
pub async fn supply_route(
|
||||||
req: HttpRequest,
|
req: HttpRequest,
|
||||||
form: actix_web::web::Form<SupplyForm>,
|
form: actix_web::web::Form<SupplyForm>,
|
||||||
) -> impl Responder {
|
) -> actix_web::Result<impl Responder> {
|
||||||
let itemdb = get_itemdb!(req);
|
let itemdb = get_itemdb!(req);
|
||||||
println!("{form:?}");
|
println!("{form:?}");
|
||||||
let variant = itemdb
|
let variant = itemdb
|
||||||
.get_item(&form.item)
|
.get_item(&form.item)
|
||||||
.unwrap()
|
.ok_or_else(|| actix_web::error::ErrorBadRequest("The item does not exist"))?
|
||||||
.variant(&form.variant)
|
.variant(&form.variant)
|
||||||
.unwrap();
|
.ok_or_else(|| actix_web::error::ErrorBadRequest("The variant does not exist"))?;
|
||||||
let transaction_id = variant
|
let transaction_id = variant
|
||||||
.supply(
|
.supply(
|
||||||
form.amount.unwrap_or(1),
|
form.amount.unwrap_or(1),
|
||||||
|
@ -40,21 +39,27 @@ pub async fn supply_route(
|
||||||
&form.origin,
|
&form.origin,
|
||||||
)
|
)
|
||||||
.await;
|
.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")]
|
#[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 id = r.match_info().query("item_id");
|
||||||
|
|
||||||
let itemdb = get_itemdb!(r);
|
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);
|
Ok(HttpResponse::Ok().json(serde_json::json!({
|
||||||
|
|
||||||
let variants = item.unwrap().get_variants().await;
|
|
||||||
|
|
||||||
HttpResponse::Ok().json(serde_json::json!({
|
|
||||||
"item": id,
|
"item": id,
|
||||||
"variants": variants
|
"variants": variants
|
||||||
}))
|
})))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue