partial void DeleteIngredient(Ingredient instance);
 partial void UpdateIngredient(Ingredient instance);
 partial void InsertIngredient(Ingredient instance);
		private void detach_Ingredients(Ingredient entity)
		{
			this.SendPropertyChanging();
			entity.Recipe = null;
		}
		private void attach_Ingredients(Ingredient entity)
		{
			this.SendPropertyChanging();
			entity.Recipe = this;
		}
Пример #6
0
        public ActionResult UploadRecipe(UploadRecipeModel newRecipe, HttpPostedFileBase file)
        {
            Recipe recipeEntry = new Recipe();
            recipeEntry.Title = newRecipe.Title;
            recipeEntry.Instructions = newRecipe.Instructions;
            recipeEntry.DateCreated = DateTime.Now;
            recipeEntry.DateModified = DateTime.Now;
            recipeEntry.UserID = WebSecurity.CurrentUserId;

            db.Recipes.InsertOnSubmit(recipeEntry);

            db.SubmitChanges();

            //If there is an image, upload it to S3
            if (file != null && file.ContentLength > 0)
            {
                //make sure the file is less than 5 MB
                if (file.ContentLength > 5 * 1024 * 1024)
                {
                    ViewBag.Error = "Image must be less than 5 MB";
                    return View("Error");
                }

                //make sure the file is an image
                string[] acceptedExtensions = { ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff" };
                bool isImage = false;
                foreach (string extension in acceptedExtensions)
                {
                    if (file.FileName.ToLower().EndsWith(extension))
                    {
                        isImage = true;
                        break;
                    }
                }
                if (!isImage)
                {
                    ViewBag.Error = "Image format not supported.  Accepted formats are: " +
                             ".jpg, .jpeg, .png, .gif, .bmp, and .tiff";
                    return View("Error");
                }

                //upload the image to S3
                string imageKey = "recipes/" + WebSecurity.CurrentUserId + "/" + recipeEntry.RecipeID;
                PutObjectRequest request = new PutObjectRequest
                {
                    BucketName = "Cookbook_Images",
                    Key = imageKey,
                    InputStream = file.InputStream,
                };

                try
                {
                    s3client.PutObject(request);
                }
                catch (AmazonS3Exception e)
                {
                    ViewBag.Error = "An error occurred storing the image:\n" + e.Message;
                    return View("Error");
                }

                string imageUrl = "https://s3.amazonaws.com/Cookbook_Images/" + imageKey;
                object[] param = { };
                db.ExecuteQuery<Object>(@"UPDATE Recipe " +
                                "SET Recipe.ImageUrl='" + imageUrl + "'" +
                                "WHERE Recipe.RecipeId='" + recipeEntry.RecipeID + "'", param);
            }

            var ingredients = newRecipe.Ingredients.Split(',').ToList();
            foreach (var ingredient in ingredients)
            {
                Ingredient entry = new Ingredient();
                entry.Name = ingredient.Trim();
                entry.RecipeId = recipeEntry.RecipeID;
                db.Ingredients.InsertOnSubmit(entry);
            }

            var tags = newRecipe.Tags.Split(',').ToList();
            foreach (var tag in tags)
            {
                Recipe_Tag newTag = new Recipe_Tag();
                newTag.RecipeID = recipeEntry.RecipeID;
                newTag.Tag = tag.Trim();
                db.Recipe_Tags.InsertOnSubmit(newTag);
            }

            db.SubmitChanges();

            return RedirectToAction("Index");
        }