Esempio n. 1
0
        public IActionResult CreateRecipe([Bind("Id,Name,RecipeDescription,RecipeCategories,RecipeCategory,GlobalError")] RecipeModel recipe_model)
        {
            if (ModelState.IsValid && HttpContext.Request.Form.Files.Count > 0)
            {
                var recipe = _recipeRepository.GetByName(recipe_model.Name);
                if (recipe == null)
                {
                    recipe = CastModelToRecipe(recipe_model);

                    var pic = HttpContext.Request.Form.Files[0];
                    using (var ms = new MemoryStream())
                    {
                        pic.CopyTo(ms);
                        recipe.Picture = ms.ToArray();
                    }

                    recipe.Creator    = GetCurrentUser();
                    recipe.AddDate    = DateTime.Now;
                    recipe.ChangeDate = DateTime.Now;

                    var recipe_id = _recipeRepository.Create(recipe);

                    return(RedirectToRecipes(recipe_id));
                }
                ModelState.AddModelError("GlobalError", "Такой рецепт уже существует.");
            }
            recipe_model.RecipeCategories = _recipeCategoryRepository.Get();
            return(View(recipe_model));
        }