public ActionResult AddComment(FormCollection formCollection) { PostComment newComment = new PostComment(); TryUpdateModel<INewPostComment>(newComment, formCollection); newComment.PostedDate = DateTime.UtcNow; newComment.IPAddress = Request.UserHostAddress; if (ModelState.IsValid) { newComment.Body = Server.HtmlEncode(newComment.Body); _postCommentRepository.AddPostComment(newComment); _postCommentRepository.SaveChanges(); if (!User.Identity.IsAuthenticated || (User.Identity.IsAuthenticated && _userRepository.GetUser(User.Identity.Name).Email.ToUpper() != newComment.Email.ToUpper())) { try { SmtpClient smtpClient = new SmtpClient(); MailMessage notificationEmail = new MailMessage { IsBodyHtml = true, Subject = string.Format("{0} posted a comment on CodeTunnel!", newComment.Author), Body = string.Format( "{0} posted a comment on your blog post titled <a href='{2}'>{1}</a>.<br /><br />{3}", newComment.Author, newComment.BlogPost.Title, ConfigurationManager.AppSettings["FullUrl"] + Url.Action("Post", new { Id = newComment.PostID }), newComment.Body) }; notificationEmail.To.Add(newComment.BlogPost.User.Email); smtpClient.Send(notificationEmail); } catch { } } if (Request.IsAjaxRequest()) return Json(new { Valid = true, Html = MvcUtilities.Utilities.CommonUtils.RenderViewToString(this, "PostComments", newComment.BlogPost.PostComments.OrderBy(x => x.PostedDate), true) }); else return RedirectToAction("Post", new { Id = newComment.PostID }); } else { if (ModelState.ContainsKey("spam")) { Variable spamCaught = this._variableRepository.GetVariable("spamCaught"); bool addNew = (spamCaught == null); int spamCount = 0; if (addNew) spamCaught = new Variable { Name = "spamCaught" }; else spamCount = int.Parse(spamCaught.Value); spamCount++; spamCaught.Value = spamCount.ToString(); if (addNew) _variableRepository.AddVariable(spamCaught); _variableRepository.SaveChanges(); } if (!Request.IsAjaxRequest()) { var blogPost = _blogPostRepository.GetBlogPost(newComment.PostID); var viewModel = new BlogPostViewModel { BlogPost = blogPost, NewComment = newComment }; return View("Post", viewModel); } else { return Json(new { Valid = false, Html = MvcUtilities.Utilities.CommonUtils.RenderViewToString(this, "NewComment", newComment, true) }); } } }
/// <summary> /// Displays a single blog post. /// </summary> /// <param name="Id">The ID of the blog post.</param> /// <param name="postTitle">The friendly-url version of the blog title.</param> public ActionResult Post(int Id, string postTitle) { var blogPost = _blogPostRepository.GetBlogPost(Id); // Create friendly URL from blog post title and compare it to the friendly URL that was passed in. // If they do not match then redirect to the same action but with the correct title in the URL. string friendlyUrl = MvcUtilities.Utilities.CommonUtils.CreateFriendlyUrl(blogPost.Title); if (postTitle != friendlyUrl) return RedirectToAction("Post", new { Id = Id, postTitle = friendlyUrl }); // Create a new post comment and pre-populate it with the current blog post ID, and if the user is // logged in then populate it with values from their profile to reduce typing when logged in users // comment on blog posts. PostComment newComment = new PostComment { PostID = blogPost.PostID }; if (User.Identity.IsAuthenticated) { User currentUser = _userRepository.GetUser(User.Identity.Name); newComment.Author = currentUser.Username; newComment.Email = currentUser.Email; newComment.Website = currentUser.Website; } // Construct a view model and attach the current blog post and post comment. var viewModel = new BlogPostViewModel { BlogPost = blogPost, NewComment = newComment }; // Render the "Post" view. return View(viewModel); }