private void btnAddCategory_Click(object sender, EventArgs e) { string categoryName = txtCategory.Text; if (Validator.CheckCategoryIsNotEmpty(txtCategory) && Validator.CheckDuplicateCategory(categoryName)) { Category category = new Category(categoryName); categoryController.AddNewCategory(category); FillCategoryList(); FillCategoryComboBox(); txtCategory.Text = string.Empty; MessageBox.Show("New category added!"); } }
public async Task Read() { var ingredients = await ingredientController.GetAllIngredients(); while (true) { var categories = await categoryController.GetAllCategories(); if (categories.Count == 0) { var name = CreateString("Название категории"); await categoryController.AddNewCategory(name); categories = await categoryController.GetAllCategories(); } Console.WriteLine("Выберите категорию рецептов:"); for (int i = 0; i < categories.Count; i++) { Console.WriteLine("{0}. {1}", i + 1, categories[i].Name); } Console.WriteLine((categories.Count + 1) + ". Создать новую категорию\n" + (categories.Count + 2) + ". Выход"); if (int.TryParse(Console.ReadLine(), out int numberCategory) && numberCategory > 0 && numberCategory <= categories.Count + 2) { Console.Clear(); if (numberCategory == categories.Count + 2) { break; } else if (numberCategory == categories.Count + 1) { var name = CreateString("Название категории"); await categoryController.AddNewCategory(name); categories = await categoryController.GetAllCategories(); continue; } var categoryRecipes = recipeController.GetAllRecipes().Result.Where(x => x.CategoryId == categories[numberCategory - 1].Id).ToList(); if (categoryRecipes.Count == 0) { await CreateRecipeAsync(numberCategory); categoryRecipes = recipeController.GetAllRecipes().Result.Where(x => x.CategoryId == categories[numberCategory - 1].Id).ToList(); } while (true) { Console.WriteLine("Выберите рецепт"); for (int i = 0; i < categoryRecipes.Count; i++) { Console.WriteLine("{0}. {1}", i + 1, categoryRecipes[i].Name); } Console.WriteLine((categoryRecipes.Count + 1) + ". Создать новый\n" + (categoryRecipes.Count + 2) + ". Вернутся к категориям"); var choiseRecipe = Console.ReadLine(); Console.Clear(); if (int.TryParse(choiseRecipe, out int number) && number > 0 && number <= categoryRecipes.Count + 2) { if (number == categoryRecipes.Count + 2) { break; } else if (number == categoryRecipes.Count + 1) { await CreateRecipeAsync(numberCategory); categoryRecipes = recipeController.GetAllRecipes().Result.Where(x => x.CategoryId == categories[numberCategory - 1].Id).ToList(); continue; } var recipe = categoryRecipes[number - 1]; Console.WriteLine("{0}\n\nОписание:\n{1}\n\nИнгридиенты:\n{2}\n\nШаги приготовления:\n", recipe.Name, recipe.Description, string.Join(", ", recipe.Ingredients.Select(x => x.Name))); for (int i = 0; i < recipe.CookingSteps.Count; i++) { Console.WriteLine(i + 1 + " " + recipe.CookingSteps[i].Step); } Console.ReadKey(); Console.Clear(); } } } Console.Clear(); } }