Esempio n. 1
0
        public JsonResult AddComment(string text, int post, int?idUpperComment)
        {
            if ((text.Length > 0) && (text.Length < 251))
            {
                var commentaryService = new CommentaryService();
                var userService       = new UserService();
                var commentaryId      = 0;
                var idUser            = userService.GetUserByAccountId(new GetUserByAccountIdRequest()
                {
                    AccountId = User.Identity.GetUserId()
                }).User.Id;

                if (ModelState.IsValid)
                {
                    var request = new CreateCommentaryRequest
                    {
                        CommentaryText = text,
                        IdPost         = post,
                        IdUser         = idUser,
                        IdUpperComment = (idUpperComment == 0) ? null : idUpperComment
                    };

                    var result = commentaryService.CreateCommentary(request);
                    commentaryId = result.CommentaryId;
                }

                var htmlComment = "";
                var userName    = User.Identity.GetUserName();
                var date        = DateTime.Now.ToString("dd/MM/yyyy");

                if (idUpperComment == 0)
                {
                    //Es un comentario nuevo.
                    htmlComment = "<article class=\"row\" id=\"" + commentaryId + "\"><div class=\"col-md-10 col-sm-10\"><div class=\"panel panel-default arrow left\"><div class=\"panel-body\"><header class=\"text-left\"><div class=\"comment-user\"><i class=\"fa fa-user\"></i> <a href=\"/Profile/Details/" + idUser + "\">" + userName + "</a>   <time class=\"comment-date\"><i class=\"fa fa-clock-o\"></i> " + date + "</time></div></header><div class=\"comment-post\"><p style=\"margin-bottom: 5px;\">" + text + "<span>&nbsp;</span><small><a class=\"deleteCommentary text-danger\" href=\"#\" id=\"" + commentaryId + "\">Eliminar comentario</a></small></p></div><p class=\"text-right\"><a id=\"replyCommentary\" href=\"#\" class=\"btn btn-default btn-sm\" replyTo=\"" + userName + "\" commentary-id=\"" + commentaryId + "\"><i class=\"fa fa-reply\"></i> Responder</a></p></div></div></div></article><div id=\"newComment" + commentaryId + "\" class=\"media\"></div>";
                }
                else
                {
                    //Es una respuesta.
                    var upperCommentWriterUserName = commentaryService.GetCommentaryById(new GetCommentaryByIdRequest()
                    {
                        Id = idUpperComment.Value
                    }).Commentary.WriterUserName;
                    htmlComment = "<article class=\"row\" id=\"" + commentaryId + "\">	<div class=\"col-md-9 col-sm-9 col-md-offset-1 col-sm-offset-0 hidden-xs\"><div class=\"panel panel-default arrow left\"><div class=\"panel-heading right\">En respuesta a <i>"+ upperCommentWriterUserName + "</i></div><div class=\"panel-body\"><header class=\"text-left\"><div class=\"comment-user\"><i class=\"fa fa-user\"></i> <a href=\"/Profile/Details/" + idUser + "\">" + userName + "</a>   <time class=\"comment-date\"><i class=\"fa fa-clock-o\"></i> " + date + "</time></div></header><div class=\"comment-post\"><p>" + text + "<span>&nbsp;</span><small><a class=\"deleteCommentary text-danger\" href=\"#\" id=\"" + commentaryId + "\">Eliminar comentario</a></small></p></div></div></div></div></article>";
                }

                return(Json(new { success = htmlComment }));
            }

            return(Json(false, JsonRequestBehavior.AllowGet));
        }
Esempio n. 2
0
        public IHttpActionResult PostCommentary(CreateCommentaryModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var commentaryService = new CommentaryService();
                    var postService       = new PostService();
                    var userService       = new UserService();
                    var currentUserId     = userService.GetUserByAccountId(new GetUserByAccountIdRequest()
                    {
                        AccountId = User.Identity.GetUserId()
                    }).User.Id;

                    // Controla que exista la publicación asociada.
                    var post = postService.GetPostById(new GetPostByIdRequest()
                    {
                        Id = model.IdPost
                    }).Post;
                    if (post == null)
                    {
                        return(BadRequest("Invalid post"));
                    }
                    if (post.NullDate.HasValue)
                    {
                        return(BadRequest("Invalid post"));
                    }

                    if (model.IdUpperComment.HasValue)
                    {
                        // Controla que exista el comentario padre.
                        var upperCommentary = commentaryService.GetCommentaryById(new GetCommentaryByIdRequest()
                        {
                            Id = model.IdUpperComment.Value
                        }).Commentary;
                        if (upperCommentary == null)
                        {
                            return(BadRequest("Invalid upper commentary"));
                        }
                        if (upperCommentary.NullDate.HasValue)
                        {
                            return(BadRequest("Invalid upper commentary"));
                        }

                        if (upperCommentary.IdUpperComment.HasValue)
                        {
                            return(BadRequest("You can't respond an answer"));
                        }
                    }

                    var request = new CreateCommentaryRequest()
                    {
                        CommentaryText = model.TextComment,
                        IdPost         = model.IdPost,
                        IdUpperComment = model.IdUpperComment,
                        IdUser         = currentUserId
                    };

                    var result = commentaryService.CreateCommentary(request);

                    return(Ok());
                }
                catch (Exception)
                {
                    return(BadRequest());
                }
            }

            return(BadRequest(ModelState));
        }