public ActionResult <RecipeModel> GetRecipe(long id) { if (id == 0) { return(BadRequest($"Id was 0")); } var recipe = _productManagement.LoadRecipe(id); if (recipe == null) { return(NotFound()); } return(ProductConverter.ConvertRecipe(recipe)); }
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 = (IProductRecipe)_productManagement.LoadRecipe(recipeModel.Id); } ConvertRecipeBack(recipeModel, productRecipe, converted); recipes.Add(productRecipe); } if (recipes.Any()) { foreach (var r in recipes) { _productManagement.SaveRecipe(r); } } // 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); }