Exemplo n.º 1
0
        public ActionResult ModifyRecipeView(RecipeViewModel model, HttpPostedFileBase recipeImage)
        {
            if (model != null && model.QuantityMeasurementIngredient != null && model.PrepSteps != null)
            {
                if (recipeImage != null && recipeImage.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(recipeImage.FileName);
                    var fullPath = Path.Combine(Server.MapPath("~/Recipe-Images"), fileName);
                    recipeImage.SaveAs(fullPath);
                    model.RecipeImageName = fileName;
                }
                else
                {
                    model.RecipeImageName = model.RecipeImageName;
                }

                recipeIngredientDAL.DeleteFromRecipeIngredient(model.RecipeId);
                preparationStepsDAL.DeleteFromPreparationSteps(model.RecipeId);

                List <RecipeIngredient> recipeIngredients = new List <RecipeIngredient>();
                PreparationSteps        pS = new PreparationSteps();
                model.PrepSteps[0].Replace('\n', ' ');
                List <string> prepSteps = new List <string>();

                foreach (string step in model.PrepSteps[0].Split('\r').ToList())
                {
                    if (step.Contains('\n') && step.Length > 2 && step != "")
                    {
                        string newStep = step.Remove(0, 1);
                        prepSteps.Add(newStep);
                    }
                    else if (!step.Contains('\n'))
                    {
                        prepSteps.Add(step);
                    }
                }

                foreach (var item in model.QuantityMeasurementIngredient)
                {
                    if (item != "")
                    {
                        RecipeIngredient recipeIngredient = new RecipeIngredient()
                        {
                            Quantity        = "",
                            Measurement     = "",
                            Ingredient_Name = item,
                        };

                        recipeIngredients.Add(recipeIngredient);
                    }
                }
                string ingredient1 = recipeIngredients[0].Ingredient_Name;

                Recipe r = new Recipe();
                r.RecipeId          = model.RecipeId;
                r.Name              = model.RecipeName;
                r.Description       = model.RecipeDescription;
                r.ImageName         = model.RecipeImageName;
                r.CookTimeInMinutes = model.RecipeCookTimeInMinutes;
                var recipeType = "";
                int counter    = 0;
                foreach (var item in model.RecipeType)
                {
                    if (model.RecipeType.Count == counter)
                    {
                        recipeType += item;
                    }
                    else
                    {
                        recipeType += item + ", ";
                        counter++;
                    }
                }
                r.RecipeType = recipeType;

                if (userDAL.GetUser((string)Session[SessionKeys.EmailAddress]) != null)
                {
                    r.UserId = (int)Session[SessionKeys.UserId];
                    recipeDAL.UpdateRecipe(r);
                    recipeIngredientDAL.SaveRecipeIngredients(recipeIngredients, r.RecipeId);
                    preparationStepsDAL.SavePreparationSteps(r.RecipeId, prepSteps, pS); //might need to get RECIPEID from DAL
                    TempData["action"] = "update";
                    return(RedirectToAction("Detail", new { recipeId = r.RecipeId }));
                }
            }
            return(RedirectToAction("ModifyRecipeView", "Recipe"));
        }