Represents messages. This messages are written by users and references Posts
        public ActionResult AddComment(Comment comment)
        {
            // Spam is not an error
            if (ModelState["isspam"] != null)
            {
                ModelState.Remove("isspam");
                comment.IsSpam = true;
            }

               if (TryValidateModel(comment))
               {
               comment.Ip = Request.UserHostAddress;
               comment.UserAgent = Request.UserAgent;
               if (!CodeFirstSecurity.IsAuthenticated)
               {
                   AnonymousCommentViewModel user;
                   if (comment.AnonymousUser != null)
                   {
                       user = AutoMapper.Mapper.Map<AnonymousUser, AnonymousCommentViewModel>(comment.AnonymousUser);
                   }
                   else
                   {
                       user = new AnonymousCommentViewModel();
                   }
                   if (TryValidateModel(user))
                   {
                       BlogServices.CreateComment(comment, null);
                       if (BgResources.Send_Mail_When_Comment_Received)
                       {
                           Utilities.SendMail(BgResources.Email_UserName,
                          String.Format(Resources.AppMessages.Comment_Received, Request.UrlReferrer.AbsoluteUri),
                          "<h2>" + Resources.AppMessages.User + "</h2>" +
                          "<p>" + comment.AnonymousUser.Username + "</p>" +
                          "<p>" + comment.AnonymousUser.Email + "</p>" +
                          "<p>" + comment.AnonymousUser.Web + "</p>" +
                          "<p>" + comment.Ip + "</p>" +
                          "<p>" + comment.UserAgent + "</p>" +
                          "<p>" + (comment.IsSpam ? Resources.AppMessages.MarkedAsSpam : Resources.AppMessages.MarkedAsHam) + "</p>" +
                          "<p>" + Request.UrlReferrer.AbsoluteUri + "</p>" +
                          "<h2>" + Resources.AppMessages.Comment_Message + "</h2>" +
                          "<div>" + comment.Message + "</div>", true);
                       }
                       if (comment.IsSpam)
                       {
                           return Json(new { result = "warnings", warnings = new KeyValuePair<string, string>("spamdetected", Resources.AppMessages.SpamDetected) });
                       }
                       else
                       {
                           return Json(new { result = "ok" });
                       }
                   }
                   else
                   {
                       return Json(new { result = "error", errors = ModelState.Where(s => s.Value.Errors.Count > 0).Select(s => new KeyValuePair<string, string>(s.Key, s.Value.Errors.First().ErrorMessage)).ToArray() });
                   }
               }
               else
               {
                   User user = UserServices.FindEntityByIdentity(CodeFirstSecurity.CurrentUserId);
                   BlogServices.CreateComment(comment, user);
                   if (BgResources.Send_Mail_When_Comment_Received)
                   {
                       Utilities.SendMail(BgResources.Email_UserName,
                           String.Format(Resources.AppMessages.Comment_Received, Request.UrlReferrer.AbsoluteUri),
                           "<h2>" + Resources.AppMessages.User + "</h2>" +
                           "<p>" + user.Username + "</p>" +
                           "<p>" + user.Email + "</p>" +
                           "<p>" + comment.UserAgent + "</p>" +
                           "<p>" + Request.UrlReferrer.AbsoluteUri + "</p>" +
                           "<h2>" + Resources.AppMessages.Comment_Message + "</h2>" +
                           "<div>" + comment.Message + "</div>", true);
                   }

                   if (comment.IsSpam)
                   {
                       return Json(new { result = "warnings", warnings = new KeyValuePair<string, string>("spamdetected", Resources.AppMessages.SpamDetected) });
                   }
                   else
                   {
                       return Json(new { result = "ok" });
                   }
               }
               }
               return Json(new { result = "error", errors = ModelState.Where(s => s.Value.Errors.Count > 0).Select(s => new KeyValuePair<string, string>(s.Key, s.Value.Errors.First().ErrorMessage)).ToArray() });
        }
 public ActionResult Edit(Comment comment)
 {
     if (ModelState.IsValid)
     {
         BlogServices.SaveComment(comment);
         return RedirectToAction("Index");
     }
     ViewBag.PostId = new SelectList(PostServices.FindAllEntities(null, null, null), "PostId", "Title", comment.PostId);
     return View(comment);
 }
Example #3
0
 /// <summary>
 /// Removes a related Comment from the Colecction 
 /// </summary>
 /// <param name="relatedComment"></param>
 public void DeleteFromRelatedCollection(Comment relatedComment)
 {
     this.RelatedComments.Remove(relatedComment);
 }
 public ActionResult Create(Comment comment)
 {
     if (ModelState.IsValid)
     {
         BlogServices.CreateComment(comment,UserServices.FindEntityByIdentity(CodeFirstSecurity.CurrentUserId));
         return RedirectToAction("Index");
     }
     ViewBag.PostId = new SelectList(PostServices.FindAllEntities(null, null, null), "PostId", "Title", comment.PostId);
     return View(comment);
 }
Example #5
0
 /// <summary>
 /// Adds a related Comment
 /// </summary>
 /// <param name="comment"></param>
 public void AddRelatedComment(Comment comment)
 {
     this.RelatedComments.Add(comment);
 }