docs + locations
This commit is contained in:
parent
e1618b40ef
commit
48f00d8f6f
17 changed files with 390 additions and 33 deletions
70
src/location.rs
Normal file
70
src/location.rs
Normal file
|
@ -0,0 +1,70 @@
|
|||
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();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue