mongod/src/model/update.rs
2024-07-18 14:10:29 +02:00

160 lines
6.4 KiB
Rust

/// Updates the value of an entity off JSON data.
///
/// This macro updates the given field of the entity with the parsed value from the JSON object.
/// If the key is present in the JSON, it will be updated. Otherwise, it will not be updated.
///
/// # Parameters
///
/// * `$entity`: The entity that contains the field to be updated.
/// * `$json`: The JSON object containing the update values to be parsed.
/// * `$key`: The literal key to search for in the JSON object.
/// * `$field`: The field name of the entity that should be updated.
/// * `$update`: A BSON Document for updating the database
#[macro_export]
macro_rules! update_historic_str {
($entity:ident, $json:ident, $key:literal, $field:ident, $update:ident) => {
if let Some(val) = $json.get($key) {
if let Some(val_str) = val.as_str() {
let mut field = $entity.$field.clone();
if !(&field.current == val_str) {
field.update(val_str.to_owned());
$entity.$field = field;
$update.insert(
$key,
mongod::mongodb::bson::to_bson(&$entity.$field).unwrap(),
);
}
}
}
};
}
/// Updates the value of an entity off JSON data.
///
/// This macro updates the given field of the entity with the parsed value from the JSON object.
/// If the key is present in the JSON, it will be updated. Otherwise, it will not be updated.
///
/// # Parameters
///
/// * `$entity`: The entity that contains the field to be updated.
/// * `$json`: The JSON object containing the update values to be parsed.
/// * `$key`: The literal key to search for in the JSON object.
/// * `$field`: The field name of the entity that should be updated.
/// * `$update`: A BSON Document for updating the database
#[macro_export]
macro_rules! update_historic_ref_option {
($entity:ident, $json:ident, $key:literal, $field:ident, $update:ident) => {
if let Some(val) = $json.get($key) {
if let Some(val_str) = val.as_str() {
let value = Historic::new(Reference::new_raw(val_str).await.unwrap());
let field = $entity.$field.clone().unwrap_or_else(|| value);
$entity.$field = Some(field);
$update.insert(
$key,
mongod::mongodb::bson::to_bson(&$entity.$field).unwrap(),
);
}
}
};
}
/// Updates the value of an entity off JSON data.
///
/// This macro updates the given field of the entity with the parsed value from the JSON object.
/// If the key is present in the JSON, it will be updated. Otherwise, it will not be updated.
///
/// # Parameters
///
/// * `$entity`: The entity that contains the field to be updated.
/// * `$json`: The JSON object containing the update values to be parsed.
/// * `$key`: The literal key to search for in the JSON object.
/// * `$field`: The field name of the entity that should be updated.
/// * `$update`: A BSON Document for updating the database
#[macro_export]
macro_rules! update_historic_vec {
($entity:ident, $json:ident, $key:literal, $field:ident, $update:ident) => {
if let Some(val) = $json.get($key) {
if let Some(val) = val.as_array() {
let mut field = $entity.$field.clone();
field.update(
val.into_iter()
.map(|x| serde_json::from_value(x.clone()).unwrap())
.collect(),
);
$entity.$field = field;
$update.insert(
$key,
mongod::mongodb::bson::to_bson(&$entity.$field).unwrap(),
);
}
}
};
}
/// Updates the value of an entity off JSON data.
///
/// This macro updates the given field of the entity with the parsed value from the JSON object.
/// If the key is present in the JSON, it will be updated. Otherwise, it will not be updated.
///
/// # Parameters
///
/// * `$entity`: The entity that contains the field to be updated.
/// * `$json`: The JSON object containing the update values to be parsed.
/// * `$key`: The literal key to search for in the JSON object.
/// * `$field`: The field name of the entity that should be updated.
/// * `$update`: A BSON Document for updating the database
/// * `$t`: The type of the value
#[macro_export]
macro_rules! update_value {
($entity:ident, $json:ident, $key:literal, $field:ident, $update:ident, $t:ty) => {
if let Some(val) = $json.get($key) {
if let Ok(val) = serde_json::from_value::<$t>(val.clone()) {
if val != $entity.$field {
$entity.$field = val;
$update.insert(
$key,
mongod::mongodb::bson::to_bson(&$entity.$field).unwrap(),
);
}
}
}
};
}
/// Updates the value of an entity off JSON data.
///
/// This macro updates the given field of the entity with the parsed value from the JSON object.
/// If the key is present in the JSON, it will be updated. Otherwise, it will not be updated.
///
/// # Parameters
///
/// * `$entity`: The entity that contains the field to be updated.
/// * `$json`: The JSON object containing the update values to be parsed.
/// * `$key`: The literal key to search for in the JSON object.
/// * `$field`: The field name of the entity that should be updated.
/// * `$update`: A BSON Document for updating the database
/// * `$t`: The type of the value
#[macro_export]
macro_rules! update_value_option {
($entity:ident, $json:ident, $key:literal, $field:ident, $update:ident, $t:ty) => {
if let Some(val) = $json.get($key) {
if let Ok(val) = serde_json::from_value::<$t>(val.clone()) {
if let Some(v) = &$entity.$field {
if val != *v {
$entity.$field = Some(val);
$update.insert(
$key,
mongod::mongodb::bson::to_bson(&$entity.$field).unwrap(),
);
}
} else {
$entity.$field = Some(val);
$update.insert(
$key,
mongod::mongodb::bson::to_bson(&$entity.$field).unwrap(),
);
}
}
}
};
}