public RecipeModel SaveRecipe(RecipeModel recipe) { var productionRecipe = ConvertRecipeBack(recipe, null); var savedId = RecipeManagement.Save(productionRecipe); recipe.Id = savedId; return(recipe); }
private IProductType ConvertProductBack(ProductModel product) { // Fetch instance and copy base values ProductType converted; if (product.Id == 0) { converted = (ProductType)ProductManager.CreateType(product.Type); } else { converted = (ProductType)ProductManager.LoadType(product.Id); } converted.Identity = new ProductIdentity(product.Identifier, product.Revision); converted.Name = product.Name; converted.State = product.State; // Copy extended properties var properties = converted.GetType().GetProperties(); EntryConvert.UpdateInstance(converted, product.Properties, ProductSerialization); ConvertFilesBack(converted, product, properties); // Save recipes var recipes = product.Recipes.Select(r => ConvertRecipeBack(r, converted)).ToList(); RecipeManagement.Save(product.Id, recipes); // Convert parts foreach (var partConnector in product.Parts) { var prop = properties.First(p => p.Name == partConnector.Name); var value = prop.GetValue(converted); if (partConnector.IsCollection) { UpdateCollection((IList)value, partConnector.Parts); } else if (partConnector.Parts.Length == 1) { if (value == null) { value = Activator.CreateInstance(prop.PropertyType); prop.SetValue(converted, value); } UpdateReference((IProductPartLink)value, partConnector.Parts[0]); } else if (partConnector.Parts.Length == 0) { prop.SetValue(converted, null); } } return(converted); }
public IProductType ConvertProductBack(ProductModel source, ProductType converted) { // Copy base values converted.Identity = new ProductIdentity(source.Identifier, source.Revision); converted.Name = source.Name; converted.State = source.State; // Save recipes var recipes = new List <IProductRecipe>(source.Recipes?.Length ?? 0); foreach (var recipeModel in source.Recipes ?? Enumerable.Empty <RecipeModel>()) { IProductRecipe productRecipe; if (recipeModel.Id == 0) { var type = ReflectionTool.GetPublicClasses <IProductRecipe>(t => t.Name == recipeModel.Type).First(); productRecipe = (IProductRecipe)Activator.CreateInstance(type); } else { productRecipe = RecipeManagement.Get(recipeModel.Id); } ConvertRecipeBack(recipeModel, productRecipe, converted); recipes.Add(productRecipe); } if (recipes.Any()) { RecipeManagement.Save(source.Id, recipes); } // Product is flat if (source.Properties is null) { return(converted); } // Copy extended properties var properties = converted.GetType().GetProperties(); EntryConvert.UpdateInstance(converted, source.Properties, ProductSerialization); // Copy Files ConvertFilesBack(converted, source, properties); // Convert parts foreach (var partConnector in source.Parts ?? Enumerable.Empty <PartConnector>()) { if (partConnector.Parts is null) { continue; } var prop = properties.First(p => p.Name == partConnector.Name); var value = prop.GetValue(converted); if (partConnector.IsCollection) { if (value == null) { value = Activator.CreateInstance(typeof(List <>) .MakeGenericType(prop.PropertyType.GetGenericArguments().First())); prop.SetValue(converted, value); } UpdateCollection((IList)value, partConnector.Parts); } else if (partConnector.Parts.Length == 1) { if (value == null) { value = Activator.CreateInstance(prop.PropertyType); prop.SetValue(converted, value); } UpdateReference((IProductPartLink)value, partConnector.Parts[0]); } else if (partConnector.Parts.Length == 0) { prop.SetValue(converted, null); } } return(converted); }