Пример #1
0
        public async Task <IActionResult> EditReplyComment(int postId, int commentId, [FromBody] CommentReplyDTO commentReplyDTO)
        {
            try
            {
                var postFromDb = await _repo.GetOneWithCondition <Post>(post => post.Id == postId, "Comments");

                if (postFromDb == null)
                {
                    return(NotFound(new
                    {
                        Error = "Không tìm thấy bài viết"
                    }));
                }
                if (!postFromDb.Comments.Any(c => c.Id == commentId))
                {
                    return(NotFound(new
                    {
                        Error = "Không tìm thấy bình luận"
                    }));
                }
                if (await ValidateString(commentReplyDTO.Content))
                {
                    return(BadRequest(new
                    {
                        validate = "Comment có chứa quá nhiều kí tự nhạy cảm"
                    }));
                }
                var userId        = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
                var accountFromDb = await _repo.GetOneWithCondition <Account>(acc => acc.Id == userId);

                if (accountFromDb.Locked > DateTime.Now)
                {
                    return(BadRequest(new
                    {
                        Error = "Tài khoản của bạn đang bị khóa chức năng thảo luận"
                    }));
                }
                var commentFromDb = await _repo.GetOneWithConditionTracking <Comment>(c => c.Id == commentId);

                if (userId != commentFromDb.AccountId && (!User.IsInRole("forumadmin") || !User.IsInRole("manager") || !User.IsInRole("superadmin")))
                {
                    return(Unauthorized());
                }
                commentFromDb.Content = commentReplyDTO.Content;
                var replyCommentFromDb = await _repo.GetOneWithConditionTracking <CommentReply>(cr => cr.ReplyId == commentId);

                if (replyCommentFromDb != null)
                {
                    replyCommentFromDb.IsEdited = true;
                }
                if (await _repo.SaveAll())
                {
                    return(Ok());
                }
                return(NoContent());
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }
Пример #2
0
        public async Task <IActionResult> ReplyComment(int postId, int commentId, [FromBody] CommentReplyDTO commentReplyDTO)
        {
            try
            {
                var postFromDb = await _repo.GetOneWithCondition <Post>(post => post.Id == postId, "Comments");

                if (postFromDb == null)
                {
                    return(NotFound(new
                    {
                        Error = "Không tìm thấy bài viết"
                    }));
                }
                if (!postFromDb.Comments.Any(c => c.Id == commentId))
                {
                    return(NotFound(new
                    {
                        Error = "Không tìm thấy bình luận"
                    }));
                }
                if (await ValidateString(commentReplyDTO.Content))
                {
                    return(BadRequest(new
                    {
                        validate = "Comment có chứa quá nhiều kí tự nhạy cảm"
                    }));
                }
                var userId        = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
                var accountFromDb = await _repo.GetOneWithCondition <Account>(acc => acc.Id == userId);

                if (accountFromDb.Locked > DateTime.Now)
                {
                    return(BadRequest(new
                    {
                        Error = "Tài khoản của bạn đang bị khóa chức năng thảo luận"
                    }));
                }
                var comment = new Comment()
                {
                    PostId         = postId,
                    AccountId      = userId,
                    Content        = commentReplyDTO.Content,
                    Date           = DateTime.Now,
                    Like           = 0,
                    Dislike        = 0,
                    IsReplyComment = true
                };
                _repo.Create(comment);
                await _repo.SaveAll();

                var replyComment = new CommentReply()
                {
                    CommentId = commentId,
                    ReplyId   = comment.Id,
                    Date      = DateTime.Now
                };
                _repo.Create(replyComment);
                if (await _repo.SaveAll())
                {
                    return(Ok());
                }
                return(NoContent());
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }