71 lines
1.7 KiB
Rust
71 lines
1.7 KiB
Rust
|
use mongod::{
|
||
|
derive::{Model, Referencable},
|
||
|
Model, ToAPI, Validate,
|
||
|
};
|
||
|
use mongodb::bson::doc;
|
||
|
use serde::{Deserialize, Serialize};
|
||
|
use serde_json::json;
|
||
|
|
||
|
/// A Storage Location
|
||
|
#[derive(Debug, Clone, Serialize, Deserialize, Model, Referencable)]
|
||
|
pub struct Location {
|
||
|
/// UUID
|
||
|
pub _id: String,
|
||
|
/// Name
|
||
|
pub name: String,
|
||
|
/// Parent
|
||
|
pub parent: Option<String>,
|
||
|
/// Storage Conditions
|
||
|
pub conditions: Option<StorageConditions>,
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||
|
pub struct StorageConditions {
|
||
|
/// Median temperature
|
||
|
pub temperature: i64,
|
||
|
}
|
||
|
|
||
|
impl Validate for Location {
|
||
|
async fn validate(&self) -> Result<(), String> {
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl ToAPI for Location {
|
||
|
async fn api(&self) -> serde_json::Value {
|
||
|
json!({
|
||
|
"id": self._id,
|
||
|
"name": self.name,
|
||
|
"parent": self.parent,
|
||
|
"conditions": self.conditions
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Location {
|
||
|
pub async fn add(id: &str, o: serde_json::Value) {
|
||
|
let l = Location {
|
||
|
_id: id.to_string(),
|
||
|
name: o
|
||
|
.as_object()
|
||
|
.unwrap()
|
||
|
.get("name")
|
||
|
.unwrap()
|
||
|
.as_str()
|
||
|
.unwrap()
|
||
|
.to_string(),
|
||
|
parent: o
|
||
|
.as_object()
|
||
|
.unwrap()
|
||
|
.get("parent")
|
||
|
.map(|x| x.as_str().unwrap().to_string()),
|
||
|
conditions: serde_json::from_value(
|
||
|
o.as_object().unwrap().get("conditions").unwrap().clone(),
|
||
|
)
|
||
|
.unwrap(),
|
||
|
};
|
||
|
|
||
|
l.insert_overwrite().await.unwrap();
|
||
|
}
|
||
|
}
|