condition inherit location
This commit is contained in:
parent
e1a755aaf2
commit
2149217dc2
1 changed files with 28 additions and 1 deletions
|
@ -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
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue