From 9e9c48e5ddfcd05ec48469caddceb6755126fe69 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Thu, 5 Sep 2024 10:33:34 +0200 Subject: [PATCH] update --- src/item.rs | 12 +++++++++++- src/main.rs | 4 +++- src/routes/item/location.rs | 12 ++++++++++++ src/routes/item/supply.rs | 14 +++++++++++++- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/item.rs b/src/item.rs index aee129f..d5437c3 100644 --- a/src/item.rs +++ b/src/item.rs @@ -2,12 +2,13 @@ use std::collections::HashMap; use mongod::{ derive::{Model, Referencable}, - Validate, + Model, Validate, }; use mongodb::bson::doc; use serde::{Deserialize, Serialize}; use serde_json::json; +use crate::transaction::Transaction; use crate::variant::Variant; // todo : api key auth @@ -113,6 +114,15 @@ impl Item { self.variants.get(variant).cloned() } + pub async fn inventory(&self) -> Vec { + let filter = doc! { + "item": &self._id, + "consumed": { "$not": { "$type": "object" } } + }; + + Transaction::find(filter, None, None).await.unwrap() + } + pub fn api_json(&self) -> serde_json::Value { let variants: HashMap = self .variants diff --git a/src/main.rs b/src/main.rs index defc5e1..319f5d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -67,10 +67,12 @@ async fn rocket() -> _ { routes::item::demand_route, routes::item::transaction_route, routes::item::inventory_route, + routes::item::inventory_route_variant, routes::item::variant_stat_route, routes::item::unique_field_route, routes::item::location_info, - routes::item::locations_info + routes::item::locations_info, + routes::item::locations_list ], ) .manage(itemdb) diff --git a/src/routes/item/location.rs b/src/routes/item/location.rs index cc1a7b3..99b7e0a 100644 --- a/src/routes/item/location.rs +++ b/src/routes/item/location.rs @@ -2,6 +2,7 @@ use std::{collections::HashMap, ops::Deref}; use mongod::ToAPI; use rocket::{get, State}; +use serde_json::json; use crate::{ json_store::JSONStore, @@ -48,6 +49,17 @@ async fn location_api_resolve( } #[get("/locations")] +pub async fn locations_list(locations: &State>) -> FallibleApiResponse { + let mut lst = Vec::with_capacity(locations.len()); + + for id in locations.deref().keys() { + lst.push(id); + } + + Ok(json!(lst)) +} + +#[get("/location_map")] pub async fn locations_info(locations: &State>) -> FallibleApiResponse { let mut root_locations = HashMap::new(); diff --git a/src/routes/item/supply.rs b/src/routes/item/supply.rs index ae1a96f..ad9537c 100644 --- a/src/routes/item/supply.rs +++ b/src/routes/item/supply.rs @@ -61,9 +61,21 @@ pub async fn supply_log_route( Ok(json!(transactions)) } +/// Returns current active Transactions for Item +#[get("/item//inventory")] +pub async fn inventory_route(item_id: &str, itemdb: &State) -> FallibleApiResponse { + let variant = itemdb + .get_item(item_id) + .ok_or_else(item_does_not_exist_error)?; + + let transactions = variant.inventory().await; + + Ok(json!(mongod::vec_to_api(&transactions).await)) +} + /// Returns current active Transactions for Item Variant #[get("/item///inventory")] -pub async fn inventory_route( +pub async fn inventory_route_variant( item_id: &str, variant_id: &str, itemdb: &State,