diff --git a/docker-compose.yml b/docker-compose.yml index 9fd2fb4..de13c60 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,7 @@ services: volumes: - ./itemdb/items:/itemdb - ./locations:/locations + - ./flows:/flows - ./config.toml:/config.toml environment: - "DB_URI=mongodb://user:pass@mongodb:27017" diff --git a/src/flow.rs b/src/flow.rs index d177e6d..54a0b02 100644 --- a/src/flow.rs +++ b/src/flow.rs @@ -2,13 +2,40 @@ use std::collections::HashMap; use mongod::{assert_reference_of, Reference, Validate}; +#[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)] pub struct FlowInfo { + pub _id: String, pub name: String, pub depends: HashMap, pub next: Option, pub produces: Option>, } +impl Validate for FlowInfo { + async fn validate(&self) -> Result<(), String> { + Ok(()) + } +} + +impl FlowInfo { + pub async fn add(&mut self, id: &str) { + self._id = id.to_string(); + self.insert_overwrite().await.unwrap(); + } +} + +impl ToAPI for FlowInfo { + async fn api(&self) -> serde_json::Value { + json!({ + "id": self._id, + "name": self.name, + "depends": self.depends, + "next": self.next, + "produces": self.produces + }) + } +} + use mongod::{ derive::{Model, Referencable}, Model, Referencable, ToAPI, diff --git a/src/main.rs b/src/main.rs index 08ab151..bc74614 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use std::ops::{Deref, DerefMut}; +use flow::FlowInfo; use json_store::JSONStore; use location::Location; @@ -55,6 +56,12 @@ async fn rocket() -> _ { location.1.add(location.0).await; } + let mut flows: JSONStore = JSONStore::new("./flows"); + + for flow in flows.deref_mut() { + flow.1.add(flow.0).await; + } + let config = config::get_config(); rocket::build() @@ -75,11 +82,15 @@ async fn rocket() -> _ { routes::item::unique_field_route, routes::item::location_info, routes::item::locations_info, - routes::item::locations_list + routes::item::locations_list, + routes::item::location_inventory, + routes::flow::flow_info, + routes::flow::flows_list ], ) .manage(itemdb) .manage(locations) + .manage(flows) .manage(config) .attach(cors) } diff --git a/src/routes/flow.rs b/src/routes/flow.rs new file mode 100644 index 0000000..7b698f6 --- /dev/null +++ b/src/routes/flow.rs @@ -0,0 +1,46 @@ +use std::collections::HashMap; + +use mongod::{vec_to_api, ToAPI}; +use rocket::{get, State}; +use serde_json::json; + +use crate::{ + check_auth, + config::Config, + flow::FlowInfo, + json_store::JSONStore, + location::Location, + routes::{api_error, FallibleApiResponse, Token}, + transaction::Transaction, +}; + +#[get("/flow//info")] +pub async fn flow_info( + id: &str, + flows: &State>, + t: Token, + c: &State, +) -> FallibleApiResponse { + check_auth!(t, c); + + let flowinfo = flows.get(id).ok_or_else(|| api_error("Flow not found"))?; + + Ok(flowinfo.api().await) +} + +#[get("/flows")] +pub async fn flows_list( + flows: &State>, + t: Token, + c: &State, +) -> FallibleApiResponse { + check_auth!(t, c); + + let mut ret = HashMap::::new(); + + for l in flows.iter() { + ret.insert(l.0.clone(), l.1.api().await); + } + + Ok(json!(ret)) +} diff --git a/src/routes/item/location.rs b/src/routes/item/location.rs index 5cf78b8..0bcca4f 100644 --- a/src/routes/item/location.rs +++ b/src/routes/item/location.rs @@ -1,6 +1,6 @@ use std::{collections::HashMap, ops::Deref}; -use mongod::ToAPI; +use mongod::{vec_to_api, ToAPI}; use rocket::{get, State}; use serde_json::json; @@ -10,6 +10,7 @@ use crate::{ json_store::JSONStore, location::Location, routes::{api_error, FallibleApiResponse, Token}, + transaction::Transaction, }; #[get("/location/")] @@ -99,15 +100,27 @@ pub async fn locations_info( Ok(serde_json::to_value(location_api).unwrap()) } -#[get("/location//inventory")] +#[get("/location//inventory?")] pub async fn location_inventory( location: &str, t: Token, - locations: &State>, c: &State, + recursive: Option<&str>, ) -> FallibleApiResponse { check_auth!(t, c); - // todo : inventory - unimplemented!() + if let Some(rec) = recursive { + match rec { + "true" | "yes" | "1" => { + return Ok(json!( + vec_to_api(&Transaction::in_location_recursive(location).await).await + )); + } + _ => {} + } + } + + Ok(json!( + vec_to_api(&Transaction::in_location(location).await).await + )) } diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 4318ab4..f76b643 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -3,6 +3,7 @@ use rocket::{ }; use serde_json::json; +pub mod flow; pub mod item; type ApiError = BadRequest; diff --git a/src/transaction.rs b/src/transaction.rs index 08a5a8c..a43e273 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -117,8 +117,20 @@ impl Transaction { } pub async fn in_location_recursive(l: &str) -> Vec { - // todo : search and merge sub locations - Self::find(doc! { "location": l}, None, None).await.unwrap() + let locations = Location::find(doc! { "parent": l}, None, None) + .await + .unwrap(); + let mut transactions = Self::find(doc! { "location": l}, None, None).await.unwrap(); + + for loc in locations { + transactions.extend( + Self::find(doc! { "location": loc.id() }, None, None) + .await + .unwrap(), + ) + } + + transactions } }