Пример #1
0
        public ActionResult CreateComment(CreateCommentModel model)
        {
            var dateCreated = Common.CurrentTime();
            var response = _commandBus.Send<CreateComment, CreateCommentResponse>(new CreateComment
            {
                PostId = model.PostId,
                ParentId = model.ParentId,
                DateCreated = dateCreated,
                AuthorIpAddress = Request.UserHostAddress,
                AuthorUserName = _userContext.CurrentUser.UserName,
                Body = model.Body,
                SendReplies = model.SendReplies
            });

            if (!string.IsNullOrEmpty(response.Error))
                return CommonJsonResult(response.Error);

            if (!response.CommentId.HasValue)
                throw new Exception("No error was given, which indicates success, but no comment id was returned.");

            var node = _commentWrapper.Wrap(response.CommentId.Value, _userContext.CurrentUser);

            return Json(new
            {
                success = true,
                commentId = response.CommentId,
                html = RenderView("_CommentNode", new CommentNode(node))
            });
        }
Пример #2
0
        public ActionResult CreateComment(CreateCommentModel model)
        {
            if (!Request.IsAuthenticated)
            {
                return Json(new
                {
                    success = false,
                    error = "You must be logged in to comment."
                });
            }

            try
            {
                var dateCreated = Common.CurrentTime();
                var response = _commandBus.Send<CreateComment, CreateCommentResponse>(new CreateComment
                {
                    PostId = model.PostId,
                    ParentId = model.ParentId,
                    DateCreated = dateCreated,
                    AuthorIpAddress = Request.UserHostAddress,
                    AuthorUserName = _userContext.CurrentUser.UserName,
                    Body = model.Body,
                    SendReplies = model.SendReplies
                });

                if (!string.IsNullOrEmpty(response.Error))
                {
                    return Json(new
                    {
                        success = false,
                        error = response.Error
                    });
                }

                if (!response.CommentId.HasValue)
                    throw new Exception("No error was given, which indicates success, but no comment id was returned.");

                var node = _commentWrapper.Wrap(response.CommentId.Value, _userContext.CurrentUser);

                return Json(new
                {
                    success = true,
                    commentId = response.CommentId,
                    html = RenderView("_CommentNode", new CommentNode(node))
                });
            }
            catch (Exception ex)
            {
                // TODO: log
                return Json(new
                {
                    success = false,
                    error = "An unexpected error occured."
                });
            }
        }