public static RecipeManagerData LoadData(string path) { using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) { var serializer = new XmlSerializer(typeof(RecipeManagerData)); var starage = (RecipeManagerData)serializer.Deserialize(fileStream); List <Recipe> tmpRecipes = new List <Recipe>(); foreach (var itemRecipe in starage.Recipes) { IngredientStorage ingredients = new IngredientStorage(); Ingredient tmpIngredient; foreach (var itemIngredient in itemRecipe.Ingredients) { tmpIngredient = starage.Ingredients.FindLast(t => t.Name == itemIngredient.Name); ingredients.Add(tmpIngredient); } DishCategory group = starage.Groups.FindLast(t => t.Name == itemRecipe.Group.Name); Recipe recipe = Recipe.Create(itemRecipe.Description, group, ingredients, itemRecipe.RecipeSteps).Value; tmpRecipes.Add(recipe); } starage.Recipes = tmpRecipes; return(starage); } }
public static Result <Recipe> Create(string description, DishCategory group, IngredientStorage ingredients, string recipeSteps) { var errors = new List <string>(); if (group is null) { errors.Add("Рецепт должен содержать категорию"); } ; if (string.IsNullOrEmpty(description)) { errors.Add("Описание не может быть пустым"); } if (string.IsNullOrEmpty(recipeSteps)) { errors.Add("Укажите шаги рецепта"); } if (ingredients.Count() == 0) { errors.Add("Рецепт должен содержать ингредиенты"); } if (errors.Any()) { return(Result <Recipe> .Fail(errors)); } var context = Regex.Replace(description, @"\s+", " ").Trim(); return(Result <Recipe> .Success(new Recipe(context, group, ingredients, recipeSteps))); }
protected Recipe(string description, DishCategory group, IngredientStorage ingredients, string recipeSteps) { Description = description; Group = group; Ingredients = ingredients; RecipeSteps = recipeSteps; }
private void btnAddGrou_Click(object sender, System.EventArgs e) { var createResult = DishCategory.Create(txtNameGrou.Text); if (!createResult.Succeeded) { MessageBox.Show(string.Join(Environment.NewLine, createResult.Errors), "Ошибка создания категории", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { _storage.GetGroups().Add(createResult.Value); } }
private bool CheckingRecipeForAnGroup(DishCategory group) { return(_storage.GetRecipe().Any(r => r.Group.Name == group.Name)); }