public ActionResult AddComment(CommentsVM comment, int postId)
        {
            var userId = User.Identity.GetUserId();
            //bool result = false;

            var Comments = new Comments();

            var user = dbContext.Users.FirstOrDefault(u => u.Id == userId);
            var post = dbContext.Posts.FirstOrDefault(p => p.Id == postId);

            if (comment != null)
            {
                Comments.CommentMsg = comment.CommentMsg;

                Comments.CommentedDate = comment.CommentedDate;

                if (user != null && post != null)
                {
                    post.Comment.Add(Comments);
                    user.Comments.Add(Comments);

                    dbContext.SaveChanges();
                    //result = true;
                }
                //Once the record is inserted , then notify all the subscribers (Clients)
                CommentsHub.NotifyCurrentUserInformationToAllClients();
            }

            return(RedirectToAction("GetComments", "Comments", new { postId = postId }));
        }
Exemplo n.º 2
0
        public ActionResult AddComment(CommentDto commentDto)
        {
            var list = new List <CommentViewModel>();

            try
            {
                var userId  = User.Identity.GetUserId();
                var comment = _repository.PostRepository.AddComment(commentDto.PostId, userId, commentDto.Text);
                list.Add(CommentViewModel.Create(comment, userId));
                _repository.Complete();
                list[0].Id = comment.Id;
                CommentsHub.Notify(commentDto.PostId, comment.Id);
            }
            catch (DbEntityValidationException exception)
            {
                list[0].Error = DbEntityValidationExceptionHandler.GetExceptionMessage(exception);
                return(PartialView("_CommentsList", list));
            }
            catch (Exception exception)
            {
                list[0].Error = exception.Message;
                return(PartialView("_CommentsList", list));
            }

            return(PartialView("_CommentsList", list));
        }