work
This commit is contained in:
parent
d913772f3b
commit
6d43193959
7 changed files with 382 additions and 323 deletions
648
Cargo.lock
generated
648
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -10,7 +10,7 @@ env_logger = "0.10.1"
|
||||||
futures = "0.3.30"
|
futures = "0.3.30"
|
||||||
log = "0.4.20"
|
log = "0.4.20"
|
||||||
maud = "0.25.0"
|
maud = "0.25.0"
|
||||||
mdq = { git = "https://git.hydrar.de/mdtools/mdq", version = "0.1.0" }
|
mdq = { git = "https://git.hydrar.de/mdtools/mdq", version = "0.3.0" }
|
||||||
mongodb = "2.8.0"
|
mongodb = "2.8.0"
|
||||||
serde = { version = "1.0.195", features = ["derive"] }
|
serde = { version = "1.0.195", features = ["derive"] }
|
||||||
serde_json = "1.0.111"
|
serde_json = "1.0.111"
|
||||||
|
|
|
@ -8,6 +8,8 @@ services:
|
||||||
- mongodb
|
- mongodb
|
||||||
volumes:
|
volumes:
|
||||||
- ./itemdb:/itemdb
|
- ./itemdb:/itemdb
|
||||||
|
environment:
|
||||||
|
- "DB_URI=mongodb://user:pass@mongodb:27017"
|
||||||
|
|
||||||
mongodb:
|
mongodb:
|
||||||
image: mongo:latest
|
image: mongo:latest
|
||||||
|
|
24
src/item.rs
24
src/item.rs
|
@ -4,6 +4,7 @@ use futures::TryStreamExt;
|
||||||
use mongodb::{bson::doc, Collection};
|
use mongodb::{bson::doc, Collection};
|
||||||
|
|
||||||
// todo : api key auth
|
// todo : api key auth
|
||||||
|
// todo : oid identifiers
|
||||||
|
|
||||||
// case: itemset
|
// case: itemset
|
||||||
// items describe generic top level structure
|
// items describe generic top level structure
|
||||||
|
@ -189,23 +190,37 @@ impl Item {
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn variant(&self, variant: &str) -> Option<Variant> {
|
pub async fn variant(&self, variant: &str) -> Option<Variant> {
|
||||||
// todo : check if avail
|
// todo : check if avail
|
||||||
|
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 {
|
Some(Variant {
|
||||||
item: self.item.name.clone(),
|
item: self.item.name.clone(),
|
||||||
variant: variant.to_string(),
|
variant: variant.to_string(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn add_variant(&self, name: &str) {
|
pub async fn add_variant(&self, name: &str, amount: usize) {
|
||||||
let variants: Collection<mongodb::bson::Document> =
|
let variants: Collection<mongodb::bson::Document> =
|
||||||
self.db.database("cdb").collection("variants");
|
self.db.database("cdb").collection("variants");
|
||||||
variants
|
variants
|
||||||
.insert_one(
|
.insert_one(
|
||||||
mongodb::bson::doc! {
|
mongodb::bson::doc! {
|
||||||
"oid": format!("{}-{}", self.item.name.clone(), name.clone()),
|
"oid": format!("{}-{}", self.item.name.clone(), name),
|
||||||
"item": self.item.name.clone(),
|
"item": self.item.name.clone(),
|
||||||
"variant": name
|
"variant": name,
|
||||||
|
"amount": amount as i64
|
||||||
},
|
},
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
|
@ -413,6 +428,7 @@ impl ItemDB {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get all items
|
||||||
pub fn items(&self) -> Vec<String> {
|
pub fn items(&self) -> Vec<String> {
|
||||||
let mut ret = vec![];
|
let mut ret = vec![];
|
||||||
for item in &self.index.documents {
|
for item in &self.index.documents {
|
||||||
|
|
|
@ -39,6 +39,7 @@ async fn main() -> std::io::Result<()> {
|
||||||
.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)
|
.service(routes::item::get_items_route)
|
||||||
|
.service(routes::item::add_variant_route)
|
||||||
})
|
})
|
||||||
.bind(("0.0.0.0".to_string(), 8080))?
|
.bind(("0.0.0.0".to_string(), 8080))?
|
||||||
.run()
|
.run()
|
||||||
|
|
|
@ -3,6 +3,7 @@ use actix_web::{get, HttpRequest, HttpResponse, Responder};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::item;
|
use crate::item;
|
||||||
|
use crate::routes::bad_req;
|
||||||
|
|
||||||
macro_rules! get_itemdb {
|
macro_rules! get_itemdb {
|
||||||
($req:expr) => {{
|
($req:expr) => {{
|
||||||
|
@ -31,6 +32,7 @@ pub async fn supply_route(
|
||||||
.get_item(&form.item)
|
.get_item(&form.item)
|
||||||
.ok_or_else(|| actix_web::error::ErrorBadRequest("The item does not exist"))?
|
.ok_or_else(|| actix_web::error::ErrorBadRequest("The item does not exist"))?
|
||||||
.variant(&form.variant)
|
.variant(&form.variant)
|
||||||
|
.await
|
||||||
.ok_or_else(|| actix_web::error::ErrorBadRequest("The variant does not exist"))?;
|
.ok_or_else(|| actix_web::error::ErrorBadRequest("The variant does not exist"))?;
|
||||||
let transaction_id = variant
|
let transaction_id = variant
|
||||||
.supply(
|
.supply(
|
||||||
|
@ -49,13 +51,33 @@ pub async fn get_items_route(r: HttpRequest) -> impl Responder {
|
||||||
actix_web::HttpResponse::Ok().json(serde_json::json!({"items": items}))
|
actix_web::HttpResponse::Ok().json(serde_json::json!({"items": items}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
pub struct AddVariantForm {
|
||||||
|
pub variant: String,
|
||||||
|
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);
|
||||||
|
Ok(HttpResponse::Ok())
|
||||||
|
}
|
||||||
|
|
||||||
#[get("/item/{item_id}/variants")]
|
#[get("/item/{item_id}/variants")]
|
||||||
pub async fn item_variants_page(r: HttpRequest) -> actix_web::Result<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
|
let item = itemdb
|
||||||
.get_item(id)
|
.get_item(id)
|
||||||
.ok_or_else(|| actix_web::error::ErrorBadRequest("The item does not exist"))?;
|
.ok_or_else(|| bad_req("The item does not exist"))?;
|
||||||
let variants = item.get_variants().await;
|
let variants = item.get_variants().await;
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().json(serde_json::json!({
|
Ok(HttpResponse::Ok().json(serde_json::json!({
|
||||||
|
|
|
@ -1 +1,5 @@
|
||||||
pub mod item;
|
pub mod item;
|
||||||
|
|
||||||
|
pub fn bad_req(msg: &'static str) -> actix_web::Error {
|
||||||
|
actix_web::error::ErrorBadRequest(msg)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue