inventory

This commit is contained in:
JMARyA 2024-08-13 04:13:38 +02:00
parent 72586df67f
commit 0be7fff77d
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
6 changed files with 49 additions and 9 deletions

View file

@ -16,8 +16,6 @@ impl ItemDB {
for item in &index.documents {
let item = Item::new(item);
log::info!("Adding item {} to DB", item.name);
}
Self { index }

View file

@ -1,6 +1,9 @@
use std::collections::HashMap;
use mongod::{derive::{Model, Referencable}, Validate};
use mongod::{
derive::{Model, Referencable},
Validate,
};
use mongodb::bson::doc;
use serde::{Deserialize, Serialize};
use serde_json::json;

View file

@ -1,7 +1,6 @@
use rocket::routes as route;
use rocket::{http::Method, launch};
mod db;
mod item;
mod routes;
@ -51,7 +50,8 @@ async fn rocket() -> _ {
routes::item::demand_log_route,
routes::item::supply_route,
routes::item::demand_route,
routes::item::transaction_route
routes::item::transaction_route,
routes::item::inventory_route
],
)
.manage(itemdb)

View file

@ -56,3 +56,23 @@ pub async fn supply_log_route(
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<_>>()))
}

View file

@ -1,4 +1,7 @@
use mongod::{derive::{Model, Referencable}, Validate};
use mongod::{
derive::{Model, Referencable},
Validate,
};
use mongodb::bson::doc;
use serde::{Deserialize, Serialize};
use serde_json::json;

View file

@ -55,7 +55,9 @@ impl 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();
@ -66,6 +68,16 @@ impl Variant {
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> {
let filter = doc! {
"item": &self.item,
@ -73,7 +85,9 @@ impl Variant {
"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();
@ -89,7 +103,9 @@ impl Variant {
let mut t = Transaction::get(uuid).await?;
t.update(&json!({
"consumed": Consumed{ destination: destination.to_string(), price }
})).await.ok()?;
}))
.await
.ok()?;
Some(())
}