コード例 #1
0
        //For POST Edit Action
        private Recipe ParseRecipeVMToRecipeModel_Update(Recipe recipeModel, AdminRecipeViewModel recipeVM)
        {
            Recipe            recipeForEdit         = recipeModel;
            ImageUploadResult recipeImgUploadResult = new ImageUploadResult();
            //var recipeForEdit = this.db.Recipes.All()
            //            .Include(r => r.Categories)
            //            .Include(r => r.RecipeItems)
            //            .FirstOrDefault(r => r.RecipeID == recipeVM.RecipeID);
            HMLToRTFConverter converter = new HMLToRTFConverter();

            recipeForEdit.Title = recipeVM.Title;

            //recipeForEdit.ImageURL = "null_image.png";
            Category newRecipeParentCategory = this.db.Categories.GetById(recipeVM.SelectedParentCategory);

            recipeForEdit.Categories.Clear();
            recipeForEdit.Categories.Add(newRecipeParentCategory);
            Category newRecipeChildrenCategory = null;

            if (recipeVM.SelectedChildrenCategory != null)
            {
                newRecipeChildrenCategory = this.db.Categories.GetById((int)(recipeVM.SelectedChildrenCategory));
                recipeForEdit.Categories.Add(newRecipeChildrenCategory);
            }
            if (recipeVM.Image != null && (recipeVM.Image.FileName != "null_image.png" || recipeVM.Image.FileName != "null_image"))
            {
                if (newRecipeChildrenCategory == null)
                {
                    DeleteRecipeImage(recipeVM.ImageURL);
                    recipeImgUploadResult = UploadRecipeImage(recipeVM.Image, recipeVM.Title, newRecipeParentCategory.Name, null);
                }
                else
                {
                    DeleteRecipeImage(recipeVM.ImageURL);
                    recipeImgUploadResult = UploadRecipeImage(recipeVM.Image, recipeVM.Title, newRecipeParentCategory.Name, newRecipeChildrenCategory.Name);
                }
                recipeForEdit.ImageVersion = "v" + recipeImgUploadResult.Version;
                recipeForEdit.ImageURL     = "http://res.cloudinary.com" + recipeImgUploadResult.Uri.AbsolutePath;
                //newRecipe.ImageURL = recipeVM.Title.Replace(" ", "-") + ".jpg";
            }
            else
            {
                RenameResult renameResult = null;
                if (newRecipeChildrenCategory != null)
                {
                    renameResult = RenameRecipeImage(recipeVM.ImageURL, newRecipeParentCategory.Name, newRecipeChildrenCategory.Name);
                }
                else
                {
                    renameResult = RenameRecipeImage(recipeVM.ImageURL, newRecipeParentCategory.Name);
                }
                if (renameResult != null)
                {
                    recipeForEdit.ImageURL = HttpUtility.UrlDecode(renameResult.Url);
                }
            }
            recipeForEdit.PreparationTime = recipeVM.SelectedPrepTimeHours * 60 + recipeVM.SelectedPrepTimeMinutes;
            recipeForEdit.CookingTime     = recipeVM.SelectedCookTimeHours * 60 + recipeVM.SelectedCookTimeMinutes;
            recipeForEdit.Serves          = recipeVM.SelectedServes;
            recipeForEdit.Rating          = recipeVM.SelectedRating;
            recipeForEdit.Source          = this.db.Sources.GetById(recipeVM.SelectedSource);
            if (recipeVM.SelectedRecommendation == "Да")
            {
                recipeForEdit.Recommended = (bool?)true;
            }
            //if (recipeVM.SelectedRecommendation == "Не")
            else
            {
                recipeForEdit.Recommended = (bool?)false;
            }
            recipeForEdit.Date        = recipeVM.PublishedDate;
            recipeForEdit.Description = converter.ConvertHTMLToRTF(recipeVM.Description);
            //recipeForEdit.RecipeItems = recipeVM.RecipeItems;
            foreach (var recipeItem in recipeVM.RecipeItems)
            {
                RecipeItem newRecipeItem = new RecipeItem();
                newRecipeItem.RecipeItemName = recipeItem.RecipeItemName;
                foreach (var recipeItem_ingredient in recipeItem.RecipeItems_Ingredients)
                {
                    RecipeItems_Ingredients newRecipeItem_ingredient = new RecipeItems_Ingredients();
                    Unit       newUnit       = this.db.Units.GetById(recipeItem_ingredient.Unit.UnitID);
                    Ingredient newIngredient = this.db.Ingredients.GetById(recipeItem_ingredient.Ingredient.IngredientID);
                    newRecipeItem_ingredient.Quantity   = recipeItem_ingredient.Quantity;
                    newRecipeItem_ingredient.Unit       = newUnit;
                    newRecipeItem_ingredient.Ingredient = newIngredient;
                    newRecipeItem.RecipeItems_Ingredients.Add(newRecipeItem_ingredient);
                }
                foreach (var step in recipeItem.Steps)
                {
                    newRecipeItem.Steps.Add(step);
                }

                recipeForEdit.RecipeItems.Add(newRecipeItem);
            }

            //newRecipe.RecipeItems = recipeVM.RecipeItems;

            return(recipeForEdit);
        }
コード例 #2
0
        //For Create Action
        private Recipe ParseRecipeVMToRecipeModel(AdminRecipeViewModel recipeVM)
        {
            Recipe            newRecipe             = new Recipe();
            HMLToRTFConverter converter             = new HMLToRTFConverter();
            ImageUploadResult recipeImgUploadResult = new ImageUploadResult();

            newRecipe.Title = recipeVM.Title;
            Category newRecipeParentCategory = this.db.Categories.GetById(recipeVM.SelectedParentCategory);

            newRecipe.Categories.Add(newRecipeParentCategory);
            Category newRecipeChildrenCategory = null;

            if (recipeVM.SelectedChildrenCategory != null)
            {
                newRecipeChildrenCategory = this.db.Categories.GetById((int)(recipeVM.SelectedChildrenCategory));
                newRecipe.Categories.Add(newRecipeChildrenCategory);
            }
            if ((recipeVM.Image != null && (recipeVM.Image.FileName != "null_image.png" || recipeVM.Image.FileName != "null_image")))
            {
                if (newRecipeChildrenCategory == null)
                {
                    recipeImgUploadResult = UploadRecipeImage(recipeVM.Image, recipeVM.Title, newRecipeParentCategory.Name, null);
                }
                else
                {
                    recipeImgUploadResult = UploadRecipeImage(recipeVM.Image, recipeVM.Title, newRecipeParentCategory.Name, newRecipeChildrenCategory.Name);
                }
                newRecipe.ImageVersion = "v" + recipeImgUploadResult.Version;
                newRecipe.ImageURL     = "http://res.cloudinary.com/" + recipeImgUploadResult.Uri.AbsolutePath;
                //newRecipe.ImageURL = recipeVM.Title.Replace(" ", "-") + ".jpg";
            }
            else
            {
                newRecipe.ImageURL = "http://res.cloudinary.com/hqz5ohs1v/image/upload/v1413825708/null_image.png";
            }
            newRecipe.PreparationTime = recipeVM.SelectedPrepTimeHours * 60 + recipeVM.SelectedPrepTimeMinutes;
            newRecipe.CookingTime     = recipeVM.SelectedCookTimeHours * 60 + recipeVM.SelectedCookTimeMinutes;
            newRecipe.Serves          = recipeVM.SelectedServes;
            newRecipe.Rating          = recipeVM.SelectedRating;
            newRecipe.Source          = this.db.Sources.GetById(recipeVM.SelectedSource);
            if (recipeVM.SelectedRecommendation == "Да")
            {
                newRecipe.Recommended = true;
            }
            //if (recipeVM.SelectedRecommendation == "Не")
            else
            {
                newRecipe.Recommended = false;
            }
            DateTime dt = recipeVM.PublishedDate;

            dt.AddHours(DateTime.Now.Hour);
            dt.AddMinutes(DateTime.Now.Minute);
            dt.AddSeconds(DateTime.Now.Second);
            newRecipe.Date = dt;
            //newRecipe.Date = recipeVM.PublishedDate;
            newRecipe.Description   = converter.ConvertHTMLToRTF(recipeVM.Description);
            newRecipe.NumberOfHits  = 1;
            newRecipe.NumberOfLikes = 1;

            foreach (var recipeItem in recipeVM.RecipeItems)
            {
                RecipeItem newRecipeItem = new RecipeItem();
                newRecipeItem.RecipeItemName = recipeItem.RecipeItemName;
                foreach (var recipeItem_ingredient in recipeItem.RecipeItems_Ingredients)
                {
                    RecipeItems_Ingredients newRecipeItem_ingredient = new RecipeItems_Ingredients();
                    Unit       newUnit       = this.db.Units.GetById(recipeItem_ingredient.Unit.UnitID);
                    Ingredient newIngredient = this.db.Ingredients.GetById(recipeItem_ingredient.Ingredient.IngredientID);
                    newRecipeItem_ingredient.Quantity   = recipeItem_ingredient.Quantity;
                    newRecipeItem_ingredient.Unit       = newUnit;
                    newRecipeItem_ingredient.Ingredient = newIngredient;
                    newRecipeItem.RecipeItems_Ingredients.Add(newRecipeItem_ingredient);
                }
                foreach (var step in recipeItem.Steps)
                {
                    newRecipeItem.Steps.Add(step);
                }

                newRecipe.RecipeItems.Add(newRecipeItem);
            }

            //newRecipe.RecipeItems = recipeVM.RecipeItems;

            return(newRecipe);
        }