Exemplo n.º 1
0
        public ActionResult Save(RecipeModel model)
        {
            // If these seesions are null then it´s a new recipe to save
            if (SessionHandler.CurrentGuid == null && SessionHandler.CurrentId == null)
            {
                model.Recipe.Guid = Guid.NewGuid().ToString();
            }
            else
            {
                model.Recipe.Guid = SessionHandler.CurrentGuid;
                model.Recipe.Id   = Convert.ToInt32(SessionHandler.CurrentId);
            }

            model.Recipe.Name.HtmlEncode();
            model.Recipe.Description.HtmlEncode();
            model.Recipe.Ingredients.HtmlEncode();
            model.Recipe.CategoryId = Convert.ToInt32(model.SelectedCategory);
            model.Recipe.DishTypeId = Convert.ToInt32(model.SelectedDishType);
            model.Recipe.Portions   = Convert.ToInt32(model.SelectedPortions);

            var specials = GetSelectedSpecials(model.PostedSpecials);

            var result = RecipeRepository.Save(model.Recipe);

            if (result)
            {
                // Delete any specials saved to the recipe first
                var deleted = RecipeRepository.DeleteSpecials(model.Recipe.Guid);

                // Then save the new specials added to the recipe
                foreach (var special in specials)
                {
                    var saved = RecipeRepository.SaveSpecial(model.Recipe.Guid, special.Id);
                }

                if (model.Recipe.Id == 0) // This means that the recipe is a new one and we need to fetch the id for further use
                {
                    model.Recipe.Id = RecipeRepository.GetByGuid(model.Recipe.Guid).Id;
                }
                model.RecipeSaved          = true;
                ViewBag.Response           = Globals.InfoRecipeSaved;
                SessionHandler.CurrentGuid = model.Recipe.Guid;
                SessionHandler.CurrentId   = model.Recipe.Id.ToString();
                SessionHandler.ForceReload = true;
            }
            else
            {
                model.RecipeSaved = false;
                ViewBag.Response  = Globals.ErrorSavingRecipe;
            }

            ViewBag.Title = model.Recipe.Name;
            return(View("Detail", model));
        }
Exemplo n.º 2
0
        public ActionResult Delete(int id)
        {
            var recipe = RecipeRepository.GetById(id);

            RecipeRepository.DeleteSpecials(recipe.Guid);
            RecipeRepository.Delete(recipe.Guid);
            RecipeRepository.DeleteVotes(recipe.Guid);
            ViewBag.Response           = Globals.InfoRecipeDeleted;
            SessionHandler.ForceReload = true;
            return(View("Response"));
        }