inventory
This commit is contained in:
parent
72586df67f
commit
0be7fff77d
6 changed files with 49 additions and 9 deletions
|
@ -16,8 +16,6 @@ impl ItemDB {
|
||||||
for item in &index.documents {
|
for item in &index.documents {
|
||||||
let item = Item::new(item);
|
let item = Item::new(item);
|
||||||
log::info!("Adding item {} to DB", item.name);
|
log::info!("Adding item {} to DB", item.name);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Self { index }
|
Self { index }
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use mongod::{derive::{Model, Referencable}, Validate};
|
use mongod::{
|
||||||
|
derive::{Model, Referencable},
|
||||||
|
Validate,
|
||||||
|
};
|
||||||
use mongodb::bson::doc;
|
use mongodb::bson::doc;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use rocket::routes as route;
|
use rocket::routes as route;
|
||||||
use rocket::{http::Method, launch};
|
use rocket::{http::Method, launch};
|
||||||
|
|
||||||
|
|
||||||
mod db;
|
mod db;
|
||||||
mod item;
|
mod item;
|
||||||
mod routes;
|
mod routes;
|
||||||
|
@ -51,7 +50,8 @@ async fn rocket() -> _ {
|
||||||
routes::item::demand_log_route,
|
routes::item::demand_log_route,
|
||||||
routes::item::supply_route,
|
routes::item::supply_route,
|
||||||
routes::item::demand_route,
|
routes::item::demand_route,
|
||||||
routes::item::transaction_route
|
routes::item::transaction_route,
|
||||||
|
routes::item::inventory_route
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
.manage(itemdb)
|
.manage(itemdb)
|
||||||
|
|
|
@ -56,3 +56,23 @@ pub async fn supply_log_route(
|
||||||
|
|
||||||
Ok(json!(transactions))
|
Ok(json!(transactions))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/item/<item_id>/<variant_id>/inventory")]
|
||||||
|
pub async fn inventory_route(
|
||||||
|
item_id: &str,
|
||||||
|
variant_id: &str,
|
||||||
|
itemdb: &State<ItemDB>,
|
||||||
|
) -> FallibleApiResponse {
|
||||||
|
let variant = itemdb
|
||||||
|
.get_item(item_id)
|
||||||
|
.ok_or_else(item_does_not_exist_error)?
|
||||||
|
.variant(variant_id)
|
||||||
|
.ok_or_else(variant_does_not_exist_error)?;
|
||||||
|
|
||||||
|
let transactions = variant.inventory().await;
|
||||||
|
|
||||||
|
Ok(json!(transactions
|
||||||
|
.into_iter()
|
||||||
|
.map(|x| x.api_json())
|
||||||
|
.collect::<Vec<_>>()))
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
use mongod::{derive::{Model, Referencable}, Validate};
|
use mongod::{
|
||||||
|
derive::{Model, Referencable},
|
||||||
|
Validate,
|
||||||
|
};
|
||||||
use mongodb::bson::doc;
|
use mongodb::bson::doc;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
|
@ -55,7 +55,9 @@ impl Variant {
|
||||||
"variant": &self.variant
|
"variant": &self.variant
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = Transaction::find_partial(filter, json!({}), None).await.unwrap();
|
let result = Transaction::find_partial(filter, json!({}), None)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let mut ret = Vec::new();
|
let mut ret = Vec::new();
|
||||||
|
|
||||||
|
@ -66,6 +68,16 @@ impl Variant {
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn inventory(&self) -> Vec<Transaction> {
|
||||||
|
let filter = doc! {
|
||||||
|
"item": &self.item,
|
||||||
|
"variant": &self.variant,
|
||||||
|
"consumed": { "$exists": false }
|
||||||
|
};
|
||||||
|
|
||||||
|
Transaction::find(filter, None).await.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn demand_log(&self) -> Vec<String> {
|
pub async fn demand_log(&self) -> Vec<String> {
|
||||||
let filter = doc! {
|
let filter = doc! {
|
||||||
"item": &self.item,
|
"item": &self.item,
|
||||||
|
@ -73,7 +85,9 @@ impl Variant {
|
||||||
"consumed": { "$exists": true }
|
"consumed": { "$exists": true }
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = Transaction::find_partial(filter, json!({}), None).await.unwrap();
|
let result = Transaction::find_partial(filter, json!({}), None)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let mut ret = Vec::new();
|
let mut ret = Vec::new();
|
||||||
|
|
||||||
|
@ -89,7 +103,9 @@ impl Variant {
|
||||||
let mut t = Transaction::get(uuid).await?;
|
let mut t = Transaction::get(uuid).await?;
|
||||||
t.update(&json!({
|
t.update(&json!({
|
||||||
"consumed": Consumed{ destination: destination.to_string(), price }
|
"consumed": Consumed{ destination: destination.to_string(), price }
|
||||||
})).await.ok()?;
|
}))
|
||||||
|
.await
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
Some(())
|
Some(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue