예제 #1
0
        public ActionResult CreateRecipe(AddRecipeViewModel model)
        {
            var errors = ModelState.Values.SelectMany(v => v.Errors);

            if (ModelState.IsValid)
            {
                var recipe = new Entities.Recipe
                {
                    Name            = model.Name,
                    Description     = model.Description,
                    Vegetarian      = model.Vegetarian,
                    PreparationTime = model.PreparationTime,
                    Added           = DateTime.Now,
                    Author          = null
                };

                List <Ingredient> ingredients = new List <Ingredient>();
                ICollection <IngredientCreateViewModel> ingredientsInfo;
                if (CookieHelper.TryReadCookie <ICollection <IngredientCreateViewModel> >(
                        currentRecipeCookie, createdRecipeIngredients, out ingredientsInfo))
                {
                    foreach (var ingr in ingredientsInfo)
                    {
                        ingredients.Add(new Ingredient {
                            Name      = ingr.Alias,
                            ProductId = ingr.ProductId,
                            Product   = ProductManager.FindById(ingr.ProductId),
                            Quantity  = ingr.Quantity,
                            Recipe    = recipe
                        });
                    }
                }
                recipe.Ingredients = ingredients;

                RecipeManager.AddToCategory(recipe, RecipeManager.FindCategoryById(model.CategoryId));
                RecipeManager.Save();

                CookieHelper.DeleteCookie(currentRecipeCookie);

                return(RedirectToAction("CreateResult"));
            }

            ViewBag.Categories        = RecipeManager.RecipeCategories.ToSelectList <Entities.RecipeCategory, RecipeCategoryInfo>();
            ViewBag.OrderedCategories = ProductManager.ProductCategories.ToOrderedSelectList();

            return(View("Create"));
        }