Esempio n. 1
0
        protected void SaveRecipe(object sender, EventArgs e)
        {
            bool   isFileExtensionSupported = false;
            string imagesPath = Server.MapPath("~/Recipes/Images/");

            if (ImageUpload.HasFile)
            {
                string   fileExtension       = System.IO.Path.GetExtension(ImageUpload.FileName).ToLower();
                string[] supportedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
                isFileExtensionSupported = supportedExtensions.Contains(fileExtension);
            }

            if (isFileExtensionSupported)
            {
                try
                {
                    var physicalImageUrl = imagesPath + ImageUpload.FileName;

                    // Save to Images folder.
                    ImageUpload.PostedFile.SaveAs(physicalImageUrl);
                    // Save to Images/Thumbs folder.
                    //ImageUpload.PostedFile.SaveAs(imagesPath + "Thumbs/" + ImageUpload.FileName);

                    var imageTitle = System.IO.Path.GetFileNameWithoutExtension(ImageUpload.FileName);
                    var imageUrl   = System.IO.Path.GetFileName(ImageUpload.FileName);
                    var image      = new BonApetit.Models.Image()
                    {
                        Title       = imageTitle,
                        AltText     = imageTitle,
                        Caption     = imageTitle,
                        CreatedDate = DateTime.Now,
                        ImageUrl    = imageUrl
                    };

                    List <Ingredient> ingredients = this.Ingredients.GetAllValues().Select(s => (Ingredient)s).ToList();

                    List <Category> categories = new List <Category>();
                    foreach (var index in this.CategoriesList.GetSelectedIndices())
                    {
                        var categoryId = new Guid(this.CategoriesList.Items[index].Value);
                        var category   = db.GetCategory(categoryId);
                        categories.Add(category);
                    }

                    var recipe = new Recipe(this.Name.Text)
                    {
                        Description         = HttpUtility.HtmlDecode(this.Description.Text),
                        PrepareInstructions = HttpUtility.HtmlDecode(this.PreparationInstructions.Text),
                        Image       = image,
                        Ingredients = ingredients,
                        Categories  = categories
                    };

                    db.AddRecipe(recipe);
                    db.SaveChanges();

                    Response.Redirect("~/Recipes/RecipeDetails?recipeId=" + recipe.Id);
                }
                catch (Exception ex)
                {
                    FailureText.Text = ex.Message;
                }
            }
            else
            {
                FailureText.Text = "Unsupported file type.";
            }
        }
Esempio n. 2
0
        protected void SaveRecipe(object sender, EventArgs e)
        {
            BonApetit.Models.Image image = null;

            if (ImageUpload.HasFile)
            {
                string   fileExtension            = System.IO.Path.GetExtension(ImageUpload.FileName).ToLower();
                string[] supportedExtensions      = { ".gif", ".png", ".jpeg", ".jpg" };
                bool     isFileExtensionSupported = supportedExtensions.Contains(fileExtension);

                if (isFileExtensionSupported)
                {
                    string imagesPath       = Server.MapPath("~/Recipes/Images/");
                    var    physicalImageUrl = imagesPath + ImageUpload.FileName;

                    // Save to Images folder.
                    ImageUpload.PostedFile.SaveAs(physicalImageUrl);

                    var imageTitle = System.IO.Path.GetFileNameWithoutExtension(ImageUpload.FileName);
                    var imageUrl   = System.IO.Path.GetFileName(ImageUpload.FileName);
                    image = new BonApetit.Models.Image()
                    {
                        Title       = imageTitle,
                        AltText     = imageTitle,
                        Caption     = imageTitle,
                        CreatedDate = DateTime.Now,
                        ImageUrl    = imageUrl
                    };
                }
                else
                {
                    FailureText.Text = "Unsupported file type.";
                    return;
                }
            }

            Guid recipeId;

            if (Guid.TryParse(Request.QueryString["recipeId"], out recipeId))
            {
                try
                {
                    this.recipe = db.Recipes.Find(recipeId);

                    //IEnumerable<string> ingredientValues = this.Ingredients.GetAllValues();
                    //var newIngredients = ingredientValues.Where(newi => this.recipe.Ingredients.Any(i => i.Content != newi)).Select(i => (Ingredient)i);
                    //var ingredients = this.recipe.Ingredients.Where(i => ingredientValues.Contains(i.Content)).ToList();
                    //ingredients.AddRange(newIngredients);

                    //var removedIngredients = this.recipe.Ingredients.Where(i => !ingredientValues.Contains(i.Content));

                    this.recipe.Name                = this.Name.Text;
                    this.recipe.Description         = HttpUtility.HtmlDecode(this.Description.Text);
                    this.recipe.PrepareInstructions = HttpUtility.HtmlDecode(this.PreparationInstructions.Text);
                    this.recipe.Image               = image ?? this.recipe.Image;

                    this.EditIngredients();
                    this.EditCategories();

                    db.Entry(recipe).State = EntityState.Modified;
                    db.SaveChanges();

                    Response.Redirect("~/Recipes/RecipeDetails?recipeId=" + recipe.Id);
                }
                catch (Exception ex)
                {
                    FailureText.Text = ex.Message;
                }
            }
        }