Example #1
0
        public ActionResult CommentModal(int postId, int?commentId)
        {
            // postId + no comment id => add it to main comment thread
            // postId + commentId, but comment has no parent comment id => add it as a child
            // postId + commentId, comment has parent comment id => add it as a child under same parent

            // Find post
            var post = ctx.Posts.FirstOrDefault(p => p.Id == postId);

            if (post == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // If we have commentId, find comment
            Comment comment = null;

            if (commentId != null)
            {
                comment = ctx.Comments.FirstOrDefault(c => c.Id == commentId);

                if (comment == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }

                return(PartialView("_CommentModal", new CommentModalViewModel
                {
                    PostId = post.Id,
                    CommentParentId = comment.CommentParentId ?? comment.Id
                }));
            }

            // if we have comment and/or post, create viewmodel for comment modal
            var vm = new CommentModalViewModel
            {
                PostId          = post.Id,
                CommentParentId = null,
            };

            return(PartialView("_CommentModal", vm));
        }
Example #2
0
        public ActionResult AddComment(CommentModalViewModel model)
        {
            var comment = new Comment
            {
                AuthorId        = User.Identity.GetUserId(),
                CommentBody     = model.CommentBody,
                CreatedOn       = DateTime.Now,
                PostId          = model.PostId,
                CommentParentId = model.CommentParentId,
            };

            try
            {
                ctx.Comments.Add(comment);
                ctx.SaveChanges();
            }
            catch (Exception e)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
            }
            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }