workflows

This commit is contained in:
JMARyA 2024-09-12 10:34:14 +02:00
parent 6f9048e5b1
commit a8dfe5f0e9
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
7 changed files with 119 additions and 8 deletions

View file

@ -9,6 +9,7 @@ services:
volumes: volumes:
- ./itemdb/items:/itemdb - ./itemdb/items:/itemdb
- ./locations:/locations - ./locations:/locations
- ./flows:/flows
- ./config.toml:/config.toml - ./config.toml:/config.toml
environment: environment:
- "DB_URI=mongodb://user:pass@mongodb:27017" - "DB_URI=mongodb://user:pass@mongodb:27017"

View file

@ -2,13 +2,40 @@ use std::collections::HashMap;
use mongod::{assert_reference_of, Reference, Validate}; use mongod::{assert_reference_of, Reference, Validate};
#[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)]
pub struct FlowInfo { pub struct FlowInfo {
pub _id: String,
pub name: String, pub name: String,
pub depends: HashMap<String, i64>, pub depends: HashMap<String, i64>,
pub next: Option<String>, pub next: Option<String>,
pub produces: Option<Vec<String>>, pub produces: Option<Vec<String>>,
} }
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::{ use mongod::{
derive::{Model, Referencable}, derive::{Model, Referencable},
Model, Referencable, ToAPI, Model, Referencable, ToAPI,

View file

@ -1,5 +1,6 @@
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use flow::FlowInfo;
use json_store::JSONStore; use json_store::JSONStore;
use location::Location; use location::Location;
@ -55,6 +56,12 @@ async fn rocket() -> _ {
location.1.add(location.0).await; location.1.add(location.0).await;
} }
let mut flows: JSONStore<FlowInfo> = JSONStore::new("./flows");
for flow in flows.deref_mut() {
flow.1.add(flow.0).await;
}
let config = config::get_config(); let config = config::get_config();
rocket::build() rocket::build()
@ -75,11 +82,15 @@ async fn rocket() -> _ {
routes::item::unique_field_route, routes::item::unique_field_route,
routes::item::location_info, routes::item::location_info,
routes::item::locations_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(itemdb)
.manage(locations) .manage(locations)
.manage(flows)
.manage(config) .manage(config)
.attach(cors) .attach(cors)
} }

46
src/routes/flow.rs Normal file
View file

@ -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/<id>/info")]
pub async fn flow_info(
id: &str,
flows: &State<JSONStore<FlowInfo>>,
t: Token,
c: &State<Config>,
) -> 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<JSONStore<FlowInfo>>,
t: Token,
c: &State<Config>,
) -> FallibleApiResponse {
check_auth!(t, c);
let mut ret = HashMap::<String, serde_json::Value>::new();
for l in flows.iter() {
ret.insert(l.0.clone(), l.1.api().await);
}
Ok(json!(ret))
}

View file

@ -1,6 +1,6 @@
use std::{collections::HashMap, ops::Deref}; use std::{collections::HashMap, ops::Deref};
use mongod::ToAPI; use mongod::{vec_to_api, ToAPI};
use rocket::{get, State}; use rocket::{get, State};
use serde_json::json; use serde_json::json;
@ -10,6 +10,7 @@ use crate::{
json_store::JSONStore, json_store::JSONStore,
location::Location, location::Location,
routes::{api_error, FallibleApiResponse, Token}, routes::{api_error, FallibleApiResponse, Token},
transaction::Transaction,
}; };
#[get("/location/<id>")] #[get("/location/<id>")]
@ -99,15 +100,27 @@ pub async fn locations_info(
Ok(serde_json::to_value(location_api).unwrap()) Ok(serde_json::to_value(location_api).unwrap())
} }
#[get("/location/<location>/inventory")] #[get("/location/<location>/inventory?<recursive>")]
pub async fn location_inventory( pub async fn location_inventory(
location: &str, location: &str,
t: Token, t: Token,
locations: &State<JSONStore<Location>>,
c: &State<Config>, c: &State<Config>,
recursive: Option<&str>,
) -> FallibleApiResponse { ) -> FallibleApiResponse {
check_auth!(t, c); check_auth!(t, c);
// todo : inventory if let Some(rec) = recursive {
unimplemented!() 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
))
} }

View file

@ -3,6 +3,7 @@ use rocket::{
}; };
use serde_json::json; use serde_json::json;
pub mod flow;
pub mod item; pub mod item;
type ApiError = BadRequest<serde_json::Value>; type ApiError = BadRequest<serde_json::Value>;

View file

@ -117,8 +117,20 @@ impl Transaction {
} }
pub async fn in_location_recursive(l: &str) -> Vec<Self> { pub async fn in_location_recursive(l: &str) -> Vec<Self> {
// todo : search and merge sub locations let locations = Location::find(doc! { "parent": l}, None, None)
Self::find(doc! { "location": l}, None, None).await.unwrap() .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
} }
} }