This commit is contained in:
JMARyA 2024-09-05 10:33:34 +02:00
parent d70dabc271
commit 9e9c48e5dd
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
4 changed files with 39 additions and 3 deletions

View file

@ -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<Transaction> {
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<String, serde_json::Value> = self
.variants

View file

@ -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)

View file

@ -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<JSONStore<Location>>) -> 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<JSONStore<Location>>) -> FallibleApiResponse {
let mut root_locations = HashMap::new();

View file

@ -61,9 +61,21 @@ pub async fn supply_log_route(
Ok(json!(transactions))
}
/// Returns current active Transactions for Item
#[get("/item/<item_id>/inventory")]
pub async fn inventory_route(item_id: &str, itemdb: &State<ItemDB>) -> 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/<item_id>/<variant_id>/inventory")]
pub async fn inventory_route(
pub async fn inventory_route_variant(
item_id: &str,
variant_id: &str,
itemdb: &State<ItemDB>,