コード例 #1
0
 partial void DeleteBlogPost(BlogPost instance);
コード例 #2
0
 partial void UpdateBlogPost(BlogPost instance);
コード例 #3
0
 partial void InsertBlogPost(BlogPost instance);
コード例 #4
0
        public ActionResult UploadPost(UploadBlogPostModel newPost, HttpPostedFileBase file)
        {
            BlogPost post = new BlogPost();
            post.Title = newPost.Title;
            post.Post = newPost.Post;
            post.DateCreated = DateTime.Now;
            post.DateModified = DateTime.Now;
            post.UserId = WebSecurity.CurrentUserId;

            db.BlogPosts.InsertOnSubmit(post);
            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 = "posts/" + (int)Membership.GetUser().ProviderUserKey + "/" + post.BlogPostId;
                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 BlogPost " +
                                "SET BlogPost.ImageUrl='" + imageUrl + "'" +
                                "WHERE BlogPost.BlogPostId='" + post.BlogPostId + "'", param);
            }

            var tags = newPost.Tags.Split(',').ToList();
            foreach (var tag in tags)
            {
                BlogPost_Tag newTag = new BlogPost_Tag();
                newTag.BlogPostId = post.BlogPostId;
                newTag.Tag = tag.Trim();
                db.BlogPost_Tags.InsertOnSubmit(newTag);
            }
            db.SubmitChanges();

            return RedirectToAction("Index");
        }
コード例 #5
0
 public ActionResult EditPost(BlogPost post)
 {
     return View();
 }
コード例 #6
0
 public ActionResult DeletePost(BlogPost post)
 {
     return View();
 }