You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.1 KiB
92 lines
2.1 KiB
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{TypedValue, ID};
|
|
|
|
/// Update or insert a value
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct UpsertValue {
|
|
/// PK, null if creating
|
|
pub id: Option<ID>,
|
|
/// Property template ID
|
|
pub model: ID,
|
|
/// Property value
|
|
pub value: TypedValue,
|
|
}
|
|
|
|
/// Update or insert a relation
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct UpsertRelation {
|
|
/// PK, null if creating
|
|
pub id: Option<ID>,
|
|
/// Relation template ID
|
|
pub model: ID,
|
|
/// Related model ID
|
|
pub related: ID,
|
|
/// Relation values to update or create
|
|
pub values: Vec<UpsertValue>,
|
|
}
|
|
|
|
/// Update an existing object
|
|
#[derive(Deserialize, Debug)]
|
|
pub struct UpdateObj {
|
|
/// Updated object's ID
|
|
pub id: ID,
|
|
/// Property to use as the object's name
|
|
pub name_property: Option<ID>,
|
|
/// Values to update or create
|
|
pub values: Vec<UpsertValue>,
|
|
/// Relations to update or create
|
|
pub relations: Vec<UpsertRelation>,
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use crate::update::UpdateObj;
|
|
|
|
#[test]
|
|
#[cfg(not(feature = "uuid-ids"))]
|
|
fn deserialize() {
|
|
let json = json!({
|
|
"model_id": 0,
|
|
"id": 10,
|
|
"name": "Recipe1",
|
|
"values": [
|
|
{
|
|
"id": 11,
|
|
"model": 3,
|
|
"object": 10,
|
|
"value": {
|
|
"String": "Bla bla bla"
|
|
}
|
|
},
|
|
{
|
|
"value": {
|
|
"String": "sdfsdfsdf"
|
|
},
|
|
"model": 3
|
|
}
|
|
],
|
|
"relations": [
|
|
{
|
|
"id": 12,
|
|
"model": 5,
|
|
"object": 10,
|
|
"related": 8,
|
|
"values": [
|
|
{
|
|
"id": 13,
|
|
"model": 6,
|
|
"object": 12,
|
|
"value": {
|
|
"Integer": 15
|
|
},
|
|
"model": 6
|
|
}
|
|
]
|
|
}
|
|
]
|
|
});
|
|
|
|
let _obj: UpdateObj = serde_json::from_value(json).unwrap();
|
|
}
|
|
}
|
|
|