condition inherit location

This commit is contained in:
JMARyA 2024-09-18 08:29:27 +02:00
parent e1a755aaf2
commit 2149217dc2
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263

View file

@ -1,3 +1,6 @@
use std::{future::Future, pin::Pin};
use futures::FutureExt;
use mongod::{
derive::{Model, Referencable},
Model, ToAPI, Validate,
@ -32,13 +35,37 @@ impl Validate for Location {
}
}
impl Location {
/// Recursively get the conditions of a location. This inherits from parent locations.
pub fn conditions_rec<'a>(
&'a self,
) -> futures::future::BoxFuture<'a, Option<StorageConditions>> {
async move {
if let Some(cond) = &self.conditions {
return Some(cond.clone());
}
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);
}
}
}
None
}
.boxed()
}
}
impl ToAPI for Location {
async fn api(&self) -> serde_json::Value {
json!({
"id": self._id,
"name": self.name,
"parent": self.parent,
"conditions": self.conditions
"conditions": self.conditions_rec().await
})
}
}