This commit is contained in:
JMARyA 2024-09-12 11:54:52 +02:00
parent 02966c0605
commit fb6a96ff55
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
6 changed files with 15 additions and 37 deletions

View file

@ -1,6 +1,6 @@
use serde::Deserialize; use serde::Deserialize;
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Default, Clone, Deserialize)]
pub struct Config { pub struct Config {
/// Allowed tokens for access /// Allowed tokens for access
pub allowed_tokens: Vec<String>, pub allowed_tokens: Vec<String>,
@ -8,15 +8,6 @@ pub struct Config {
pub webhook: Option<Webhook>, pub webhook: Option<Webhook>,
} }
impl Default for Config {
fn default() -> Self {
Self {
allowed_tokens: Vec::new(),
webhook: None,
}
}
}
pub fn get_config() -> Config { pub fn get_config() -> Config {
if let Ok(content) = std::fs::read_to_string("./config.toml") { if let Ok(content) = std::fs::read_to_string("./config.toml") {
return toml::from_str(&content).unwrap(); return toml::from_str(&content).unwrap();

View file

@ -1,5 +1,3 @@
use std::ops::{Deref, DerefMut};
use flow::FlowInfo; use flow::FlowInfo;
use json_store::JSONStore; use json_store::JSONStore;
use location::Location; use location::Location;
@ -52,13 +50,13 @@ async fn rocket() -> _ {
let itemdb = db::ItemDB::new("./itemdb").await; let itemdb = db::ItemDB::new("./itemdb").await;
let mut locations: JSONStore<Location> = JSONStore::new("./locations"); let mut locations: JSONStore<Location> = JSONStore::new("./locations");
for location in locations.deref_mut() { for location in &mut *locations {
location.1.add(location.0).await; location.1.add(location.0).await;
} }
let mut flows: JSONStore<FlowInfo> = JSONStore::new("./flows"); let mut flows: JSONStore<FlowInfo> = JSONStore::new("./flows");
for flow in flows.deref_mut() { for flow in &mut *flows {
flow.1.add(flow.0).await; flow.1.add(flow.0).await;
} }

View file

@ -1,6 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use mongod::{vec_to_api, ToAPI}; use mongod::ToAPI;
use rocket::{get, State}; use rocket::{get, State};
use serde_json::json; use serde_json::json;
@ -9,9 +9,7 @@ use crate::{
config::Config, config::Config,
flow::FlowInfo, flow::FlowInfo,
json_store::JSONStore, json_store::JSONStore,
location::Location,
routes::{api_error, FallibleApiResponse, Token}, routes::{api_error, FallibleApiResponse, Token},
transaction::Transaction,
}; };
#[get("/flow/<id>/info")] #[get("/flow/<id>/info")]

View file

@ -1,4 +1,4 @@
use std::{collections::HashMap, ops::Deref}; use std::collections::HashMap;
use mongod::{vec_to_api, ToAPI}; use mongod::{vec_to_api, ToAPI};
use rocket::{get, State}; use rocket::{get, State};
@ -42,7 +42,7 @@ async fn location_api_resolve(
if parent == id { if parent == id {
sub_l.insert( sub_l.insert(
sub_loc.0.clone(), sub_loc.0.clone(),
Box::pin(location_api_resolve(&sub_loc.0, &sub_loc.1, locations)).await, Box::pin(location_api_resolve(sub_loc.0, sub_loc.1, locations)).await,
); );
} }
} }
@ -52,7 +52,7 @@ async fn location_api_resolve(
.as_object_mut() .as_object_mut()
.unwrap() .unwrap()
.insert("sub".to_string(), serde_json::to_value(sub_l).unwrap()); .insert("sub".to_string(), serde_json::to_value(sub_l).unwrap());
return loc_api; loc_api
} }
#[get("/locations")] #[get("/locations")]
@ -82,7 +82,7 @@ pub async fn locations_info(
let mut root_locations = HashMap::new(); let mut root_locations = HashMap::new();
for loc in locations.inner().deref() { for loc in &**locations.inner() {
if loc.1.parent.is_none() { if loc.1.parent.is_none() {
root_locations.insert(loc.0, loc.1); root_locations.insert(loc.0, loc.1);
} }
@ -109,16 +109,11 @@ pub async fn location_inventory(
) -> FallibleApiResponse { ) -> FallibleApiResponse {
check_auth!(t, c); check_auth!(t, c);
if let Some(rec) = recursive { if let Some("true" | "yes" | "1") = recursive {
match rec {
"true" | "yes" | "1" => {
return Ok(json!( return Ok(json!(
vec_to_api(&Transaction::in_location_recursive(location).await).await vec_to_api(&Transaction::in_location_recursive(location).await).await
)); ));
} }
_ => {}
}
}
Ok(json!( Ok(json!(
vec_to_api(&Transaction::in_location(location).await).await vec_to_api(&Transaction::in_location(location).await).await

View file

@ -127,7 +127,7 @@ pub async fn min_items_route(
) -> FallibleApiResponse { ) -> FallibleApiResponse {
check_auth!(t, c); check_auth!(t, c);
let t: Vec<_> = get_items_without_min_satisfied(&itemdb) let t: Vec<_> = get_items_without_min_satisfied(itemdb)
.await .await
.into_iter() .into_iter()
.map(|x| { .map(|x| {

View file

@ -103,11 +103,7 @@ impl Transaction {
let expiration_ts = expiry * 24 * 60 * 60; let expiration_ts = expiry * 24 * 60 * 60;
if (date_added + expiration_ts) < current_time { return (date_added + expiration_ts) < current_time;
return true;
} else {
return false;
}
} }
false false
@ -128,7 +124,7 @@ impl Transaction {
Self::find(doc! { "location": loc.id() }, None, None) Self::find(doc! { "location": loc.id() }, None, None)
.await .await
.unwrap(), .unwrap(),
) );
} }
transactions transactions