This commit is contained in:
JMARyA 2024-10-07 20:53:58 +02:00
parent 584ffb6b11
commit 1faa3b9668
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
19 changed files with 1058 additions and 1244 deletions

View file

@ -1,18 +1,14 @@
use futures::FutureExt;
use mongod::{
derive::{Model, Referencable},
Model, ToAPI, Validate,
};
use mongodb::bson::doc;
use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::{get_locations, routes::ToAPI};
/// A Storage Location
#[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Location {
/// UUID
#[serde(default)]
pub _id: String,
pub id: String,
/// Name
pub name: String,
/// Parent
@ -27,73 +23,64 @@ pub struct StorageConditions {
pub temperature: i64,
}
impl Validate for Location {
async fn validate(&self) -> Result<(), String> {
Ok(())
}
}
impl Location {
/// Recursively get the conditions of a location. This inherits from parent locations.
pub fn conditions_rec(&self) -> futures::future::BoxFuture<'_, Option<StorageConditions>> {
async move {
if let Some(cond) = &self.conditions {
return Some(cond.clone());
}
pub fn conditions_rec(&self) -> Option<StorageConditions> {
let locations = get_locations!();
if let Some(parent) = &self.parent {
if let Some(parent_loc) = Location::get(parent).await {
if let Some(cond) = parent_loc.conditions_rec().await {
return Some(cond);
}
if let Some(cond) = &self.conditions {
return Some(cond.clone());
}
if let Some(parent) = &self.parent {
if let Some(parent_loc) = locations.get(parent) {
if let Some(cond) = parent_loc.conditions_rec() {
return Some(cond);
}
}
None
}
.boxed()
None
}
// Get direct children
pub async fn children_direct(&self) -> Vec<Location> {
Location::find(doc! { "parent": self._id.clone()}, None, None)
.await
.unwrap()
pub fn children_direct(&self) -> Vec<Location> {
let mut ret = Vec::new();
let locations = get_locations!();
for loc in locations.keys() {
let loc = locations.get(loc).unwrap();
if *loc.parent.as_ref().unwrap_or(&String::new()) == self.id {
ret.push(loc.clone());
}
}
ret
}
// Get all children locations
pub fn children_recursive(&self) -> futures::future::BoxFuture<'_, Vec<Location>> {
async move {
let mut all = Vec::new();
pub fn children_recursive(&self) -> Vec<Location> {
let mut all = Vec::new();
let direct = self.children_direct().await;
all.extend_from_slice(&direct);
let direct = self.children_direct();
all.extend_from_slice(&direct);
for loc in direct {
let sub = loc.children_recursive().await;
all.extend_from_slice(&sub);
}
all
for loc in direct {
let sub = loc.children_recursive();
all.extend_from_slice(&sub);
}
.boxed()
all
}
}
impl ToAPI for Location {
async fn api(&self) -> serde_json::Value {
json!({
"id": self._id,
"id": self.id,
"name": self.name,
"parent": self.parent,
"conditions": self.conditions_rec().await
"conditions": self.conditions_rec()
})
}
}
impl Location {
pub async fn add(&mut self, id: &str) {
self._id = id.to_string();
self.insert_overwrite().await.unwrap();
}
}