group
This commit is contained in:
parent
fb6a96ff55
commit
94459d5481
7 changed files with 90 additions and 14 deletions
21
src/item.rs
21
src/item.rs
|
@ -121,6 +121,27 @@ impl Item {
|
|||
Transaction::find(filter, None, None).await.unwrap()
|
||||
}
|
||||
|
||||
pub async fn inventory_by_origin(&self, origin: &str) -> Vec<Transaction> {
|
||||
let filter = doc! {
|
||||
"item": &self._id,
|
||||
"consumed": { "$not": { "$type": "object" } },
|
||||
"origin": origin
|
||||
};
|
||||
|
||||
Transaction::find(filter, None, None).await.unwrap()
|
||||
}
|
||||
|
||||
pub async fn consumed_by_destination(&self, destination: &str) -> Vec<Transaction> {
|
||||
let filter = doc! {
|
||||
"item": &self._id,
|
||||
"consumed": {
|
||||
"destination": destination
|
||||
}
|
||||
};
|
||||
|
||||
Transaction::find(filter, None, None).await.unwrap()
|
||||
}
|
||||
|
||||
pub fn api_json(&self) -> serde_json::Value {
|
||||
let variants: HashMap<String, serde_json::Value> = self
|
||||
.variants
|
||||
|
|
|
@ -85,7 +85,8 @@ async fn rocket() -> _ {
|
|||
routes::flow::flow_info,
|
||||
routes::flow::flows_list,
|
||||
routes::item::expired_items_route,
|
||||
routes::item::min_items_route
|
||||
routes::item::min_items_route,
|
||||
routes::item::variant_price_history_by_origin
|
||||
],
|
||||
)
|
||||
.manage(itemdb)
|
||||
|
|
|
@ -69,14 +69,15 @@ pub async fn demand_route(f: Json<DemandForm>, t: Token, c: &State<Config>) -> F
|
|||
Ok(json!({"ok": 1}))
|
||||
}
|
||||
|
||||
/// Returns all used transactions for Item Variant
|
||||
#[get("/item/<item_id>/<variant_id>/demand")]
|
||||
/// Returns all consumed transactions for Item Variant
|
||||
#[get("/item/<item_id>/<variant_id>/demand?<destination>")]
|
||||
pub async fn demand_log_route(
|
||||
item_id: &str,
|
||||
variant_id: &str,
|
||||
itemdb: &State<ItemDB>,
|
||||
t: Token,
|
||||
c: &State<Config>,
|
||||
destination: Option<&str>,
|
||||
) -> FallibleApiResponse {
|
||||
check_auth!(t, c);
|
||||
|
||||
|
@ -86,7 +87,7 @@ pub async fn demand_log_route(
|
|||
.variant(variant_id)
|
||||
.ok_or_else(variant_does_not_exist_error)?;
|
||||
|
||||
let transactions = variant.demand_log().await;
|
||||
let transactions = variant.demand_log(destination).await;
|
||||
|
||||
Ok(json!(transactions))
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
mod demand;
|
||||
mod error;
|
||||
mod location;
|
||||
mod stat;
|
||||
mod supply;
|
||||
|
||||
pub use demand::*;
|
||||
|
@ -8,6 +9,7 @@ pub use error::*;
|
|||
pub use location::*;
|
||||
use mongod::Model;
|
||||
use mongod::ToAPI;
|
||||
pub use stat::*;
|
||||
pub use supply::*;
|
||||
|
||||
use rocket::get;
|
||||
|
|
32
src/routes/item/stat.rs
Normal file
32
src/routes/item/stat.rs
Normal file
|
@ -0,0 +1,32 @@
|
|||
use rocket::{get, State};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::check_auth;
|
||||
use crate::config::Config;
|
||||
use crate::routes::Token;
|
||||
use crate::{
|
||||
db::ItemDB,
|
||||
routes::{api_error, FallibleApiResponse},
|
||||
};
|
||||
|
||||
use super::{item_does_not_exist_error, variant_does_not_exist_error};
|
||||
|
||||
#[get("/item/<item_id>/<variant_id>/price_history?<origin>")]
|
||||
pub async fn variant_price_history_by_origin(
|
||||
item_id: &str,
|
||||
variant_id: &str,
|
||||
itemdb: &State<ItemDB>,
|
||||
t: Token,
|
||||
c: &State<Config>,
|
||||
origin: &str,
|
||||
) -> FallibleApiResponse {
|
||||
check_auth!(t, c);
|
||||
|
||||
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)?;
|
||||
|
||||
Ok(json!(variant.price_history_by_origin(origin).await))
|
||||
}
|
|
@ -82,20 +82,25 @@ pub async fn supply_log_route(
|
|||
}
|
||||
|
||||
/// Returns current active Transactions for Item
|
||||
#[get("/item/<item_id>/inventory")]
|
||||
#[get("/item/<item_id>/inventory?<origin>")]
|
||||
pub async fn inventory_route(
|
||||
item_id: &str,
|
||||
itemdb: &State<ItemDB>,
|
||||
t: Token,
|
||||
c: &State<Config>,
|
||||
origin: Option<&str>,
|
||||
) -> FallibleApiResponse {
|
||||
check_auth!(t, c);
|
||||
|
||||
let variant = itemdb
|
||||
let item = itemdb
|
||||
.get_item(item_id)
|
||||
.ok_or_else(item_does_not_exist_error)?;
|
||||
|
||||
let transactions = variant.inventory().await;
|
||||
let transactions = if let Some(origin) = origin {
|
||||
item.inventory_by_origin(origin).await
|
||||
} else {
|
||||
item.inventory().await
|
||||
};
|
||||
|
||||
Ok(json!(mongod::vec_to_api(&transactions).await))
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use mongod::Model;
|
||||
use mongod::{Model, Sort};
|
||||
use mongodb::bson::doc;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
|
@ -76,6 +76,7 @@ impl Variant {
|
|||
}
|
||||
}
|
||||
|
||||
/// Returns the IDs of Transactions from this Item Variant.
|
||||
pub async fn supply_log(&self) -> Vec<String> {
|
||||
let filter = doc! {
|
||||
"item": &self.item,
|
||||
|
@ -95,6 +96,7 @@ impl Variant {
|
|||
ret
|
||||
}
|
||||
|
||||
/// Returns the active Transaction of this Item Variant which are not yet consumed.
|
||||
pub async fn inventory(&self) -> Vec<Transaction> {
|
||||
let filter = doc! {
|
||||
"item": &self.item,
|
||||
|
@ -105,11 +107,20 @@ impl Variant {
|
|||
Transaction::find(filter, None, None).await.unwrap()
|
||||
}
|
||||
|
||||
pub async fn demand_log(&self) -> Vec<String> {
|
||||
let filter = doc! {
|
||||
"item": &self.item,
|
||||
"variant": &self.variant,
|
||||
"consumed": { "$type": "object" }
|
||||
/// Returns the IDs of the Transactions from this Item Variant which are consumed.
|
||||
pub async fn demand_log(&self, destination: Option<&str>) -> Vec<String> {
|
||||
let filter = if let Some(dest) = destination {
|
||||
doc! {
|
||||
"item": &self.item,
|
||||
"variant": &self.variant,
|
||||
"consumed": { "destination": dest }
|
||||
}
|
||||
} else {
|
||||
doc! {
|
||||
"item": &self.item,
|
||||
"variant": &self.variant,
|
||||
"consumed": { "$type": "object" }
|
||||
}
|
||||
};
|
||||
|
||||
let result = Transaction::find_partial(filter, json!({}), None, None)
|
||||
|
@ -155,13 +166,16 @@ impl Variant {
|
|||
t
|
||||
}
|
||||
|
||||
/// Returns all Transactions of this Item Variant
|
||||
pub async fn get_all_transactions(&self) -> Vec<Transaction> {
|
||||
let filter = doc! {
|
||||
"item": &self.item,
|
||||
"variant": &self.variant
|
||||
};
|
||||
|
||||
Transaction::find(filter, None, None).await.unwrap()
|
||||
Transaction::find(filter, None, Some(doc! { "timestamp": Sort::Descending }))
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub async fn get_transaction_timeslice(&self, year: i32, month: u32) -> Vec<Transaction> {
|
||||
|
|
Loading…
Reference in a new issue