From 58c44afa30d166f6ee82d708c872bf5753540e58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sat, 20 Feb 2021 18:26:42 +0100 Subject: [PATCH] dfix the bufg] --- yopa-web/src/routes/models/property.rs | 2 +- yopa/src/lib.rs | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/yopa-web/src/routes/models/property.rs b/yopa-web/src/routes/models/property.rs index 91317bf..807b4d8 100644 --- a/yopa-web/src/routes/models/property.rs +++ b/yopa-web/src/routes/models/property.rs @@ -29,7 +29,7 @@ pub(crate) async fn create_form( context.insert("old", &PropertyModelCreateForm { object: Default::default(), name: "".to_string(), - optional: false, + optional: true, multiple: false, data_type: DataType::String, default: "".to_string() diff --git a/yopa/src/lib.rs b/yopa/src/lib.rs index 99d6782..9dd1e50 100644 --- a/yopa/src/lib.rs +++ b/yopa/src/lib.rs @@ -507,11 +507,15 @@ impl Storage { let mut relations_to_insert = vec![]; let mut relations_to_delete = vec![]; - let existing_relations_by_id = self.relations.values() + let mut existing_relations_by_id = self.relations.values() .filter(|v| v.object == updated_object_id) .into_group_map_by(|v| v.model); - for (relation_model_id, relation_model) in self.rel_models.iter().filter(|(_id, r)| r.object == updated_object_model_id) { + let rel_models_by_id = self.rel_models.iter().filter(|(_id, r)| r.object == updated_object_model_id); + + for (relation_model_id, relation_model) in rel_models_by_id { + let mut updated_ids = vec![]; + if let Some(instances) = relations_by_model.remove(relation_model_id) { if instances.len() > 1 && !relation_model.multiple { return Err(StorageError::ConstraintViolation(format!("{} of {} cannot be set multiply", relation_model, obj_model).into())); @@ -541,12 +545,17 @@ impl Storage { model: rel_instance.model, related: rel_instance.related, }); + + updated_ids.push(relation_id); } } else { if !relation_model.optional { return Err(StorageError::ConstraintViolation(format!("{} is required for {}", relation_model, obj_model).into())); } } + + relations_to_delete.extend(existing_relations_by_id.remove(&relation_model_id).unwrap_or_default().into_iter() + .filter(|rel| !updated_ids.contains(&rel.id)).map(|rel| rel.id)); } let obj_mut = self.objects.get_mut(&updated_object_id).unwrap();