From 0be7fff77dbac7222d23278567a2d73363c9d91a Mon Sep 17 00:00:00 2001 From: JMARyA Date: Tue, 13 Aug 2024 04:13:38 +0200 Subject: [PATCH] inventory --- src/db.rs | 2 -- src/item.rs | 5 ++++- src/main.rs | 4 ++-- src/routes/item/supply.rs | 20 ++++++++++++++++++++ src/transaction.rs | 5 ++++- src/variant.rs | 22 +++++++++++++++++++--- 6 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/db.rs b/src/db.rs index 92afd0e..cff5906 100644 --- a/src/db.rs +++ b/src/db.rs @@ -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 } diff --git a/src/item.rs b/src/item.rs index 32fc8f7..5b55fe5 100644 --- a/src/item.rs +++ b/src/item.rs @@ -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; diff --git a/src/main.rs b/src/main.rs index f32c5bc..5d2b746 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) diff --git a/src/routes/item/supply.rs b/src/routes/item/supply.rs index a7e06aa..08c3d51 100644 --- a/src/routes/item/supply.rs +++ b/src/routes/item/supply.rs @@ -56,3 +56,23 @@ pub async fn supply_log_route( Ok(json!(transactions)) } + +#[get("/item///inventory")] +pub async fn inventory_route( + item_id: &str, + variant_id: &str, + itemdb: &State, +) -> 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::>())) +} diff --git a/src/transaction.rs b/src/transaction.rs index 08bddba..92c675c 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -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; diff --git a/src/variant.rs b/src/variant.rs index a73e0cb..2f3ba6e 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -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 { + 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 { 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(()) }