Ensure we only save the "cleaned" model to the DB

* Extract the model cleaning logic to a method for re-use
* Call cleaning method from ConvertEditorToDb  (we were only calling on ConvertDbToString (cache))
* Update ConvertDbToEditor to route through new deserialization helper, so that it gets the "virtual" properties it needs that were lost in the cleaning
This commit is contained in:
Tom Fulton
2014-04-29 18:18:14 -06:00
parent 86f2f180bf
commit 0547c4c07e
2 changed files with 20 additions and 12 deletions
@@ -1,6 +1,9 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Archetype.Umbraco.Models
{
@@ -24,5 +27,18 @@ namespace Archetype.Umbraco.Models
{
return this.GetEnumerator();
}
internal string SerializeForPersistence()
{
var json = JObject.Parse(JsonConvert.SerializeObject(this));
var propertiesToRemove = new String[] { "propertyEditorAlias", "dataTypeId", "dataTypeGuid" };
json.Descendants().OfType<JProperty>()
.Where(p => propertiesToRemove.Contains(p.Name))
.ToList()
.ForEach(x => x.Remove());
return json.ToString();
}
}
}
@@ -72,15 +72,7 @@ namespace Archetype.Umbraco.PropertyEditors
}
}
var json = JObject.Parse(JsonConvert.SerializeObject(archetype));
var propertiesToRemove = new String[] { "propertyEditorAlias", "dataTypeId", "dataTypeGuid" };
json.Descendants().OfType<JProperty>()
.Where(p => propertiesToRemove.Contains(p.Name))
.ToList()
.ForEach(x => x.Remove());
return json.ToString();
return archetype.SerializeForPersistence();
}
public override object ConvertDbToEditor(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
@@ -88,7 +80,7 @@ namespace Archetype.Umbraco.PropertyEditors
if (property.Value == null || property.Value.ToString() == "")
return string.Empty;
var archetype = JsonConvert.DeserializeObject<Models.Archetype>(property.Value.ToString(), _jsonSettings);
var archetype = new ArchetypeHelper().DeserializeJsonToArchetype(property.Value.ToString(), propertyType.DataTypeDefinitionId);
foreach (var fieldset in archetype.Fieldsets)
{
@@ -123,7 +115,7 @@ namespace Archetype.Umbraco.PropertyEditors
}
}
return JsonConvert.SerializeObject(archetype);
return archetype.SerializeForPersistence();
}
}