public bool Save(DefinedContentModel model) { try { if (ValidateModel(model).Count == 0) { var item = TypeConverter.ViewModelToCore(model); string filePath = HttpContext.Current.Server.MapPath("~/") + Constants.CONFIG_DIRECTORY; //if its not empty, it means this was not created at the root if (model.DefinedContentParent != "-1") { var parent = DefinedContent.Cache.GetDefinedContentItem(model.DefinedContentParent); filePath = Path.GetDirectoryName(parent.FilePath); } filePath = filePath.TrimEnd(new[] { '\\' }) + "\\" + item.Key + "\\" + Constants.CONFIG_FILE_NAME; string xml = Serialiser.Serialize <DefinedContentItem>(item).OuterXml; System.IO.FileInfo file = new System.IO.FileInfo(filePath); file.Directory.Create(); File.WriteAllText(filePath, xml); DefinedContent.Cache.FullRefresh(); return(true); } } catch (Exception) { } return(false); }
public List <string> ValidateModel([FromBody] DefinedContentModel model, [FromUri] bool addMode = true) { List <string> errors = new List <string>(); if (string.IsNullOrEmpty(model.Key)) { errors.Add("You must specify a unique Key for this Defined Content Item."); } else if (DefinedContent.TryGetId(model.Key).HasValue&& addMode) { errors.Add("The Key you specified is not unique, Keys must be unique."); } if (string.IsNullOrEmpty(model.ResolveType)) { errors.Add("You must specify a Resolution so we know how to resolve this Key."); } if (model.CreateConfig.Enabled) { if (string.IsNullOrEmpty(model.CreateConfig.ContentTypeAlias)) { errors.Add("In order for creation to work, you must specify the ContentTypeAlias to create."); } if (string.IsNullOrEmpty(model.CreateConfig.Name)) { errors.Add("In order for creation to work, you must specify the Name of the content item to create."); } if (string.IsNullOrEmpty(model.DefinedContentParent) && string.IsNullOrEmpty(model.ParentKey)) { errors.Add("In order for creation to work, you must specify a parent node, either by Key, Id or XPath."); } if (string.IsNullOrEmpty(model.ParentResolveType)) { errors.Add("In order for creation to work, you must specify how we should resolve the parent node."); } if (model.ResolveValue.Contains("$currentPage")) { errors.Add("In order for creation to work, you cannot use a relative resolve XPath. Please re-write your resolve XPath without $currentPage"); } } return(errors); }
public static DefinedContentItem ViewModelToCore(DefinedContentModel model) { return(new DefinedContentItem() { Key = model.Key, Parent = model.ParentKey, ParentType = GetCoreResolutionType(model.ParentResolveType), //TODO: needs to get parent xpath, contentid, or key in the editor ResolveType = GetCoreResolutionType(model.ResolveType), ResolveValue = model.ResolveValue, ItemType = model.CreateConfig.Enabled ? DefinedContentItemType.CreateAndResolve : DefinedContentItemType.Resolve, ContentTypeAlias = model.CreateConfig.ContentTypeAlias, Name = model.CreateConfig.Name, PropertyDefaults = model.CreateConfig.PropertyMapping.Select(p => new PropertyDefault() { PropertyAlias = p.Alias, Value = p.Value, ValueType = p.IsKey ? PropertyDefaultValueType.Key : PropertyDefaultValueType.StaticValue }).ToList() }); }