예제 #1
0
        public async Task <IActionResult> Edit(string content, int commentId)
        {
            var session = this.SessionService.Session;

            if (!session.IsLoggedIn)
            {
                return(this.BadRequest());
            }

            var commentPoco = await this.CommentService.GetCommentById(commentId);

            if (commentPoco == null || commentPoco.UserId != session.UserAccount.UserId)
            {
                return(this.BadRequest());
            }

            string sanitizedContent = Regex.Replace(content, @"(\n){2,}", Environment.NewLine);

            commentPoco.Content = sanitizedContent;
            commentPoco.Edited  = true;

            await this.CommentService.UpdateComment(commentPoco);

            var commentUser = await this.UserService.GetUserById(commentPoco.UserId);

            var resposeCommentDto = new CommentDto
            {
                CommentId = commentPoco.CommentId,
                Content   = sanitizedContent,
                Username  = commentUser.Name,
                AvatarUrl = commentUser.AvatarUrl,
                HumanDate = DateUtils.DateTimeToLongAgo(commentPoco.CreatedOn)
            };

            return(this.Ok(resposeCommentDto));
        }
예제 #2
0
        public async Task <IActionResult> Create(string content, int postId, int?parentId)
        {
            var session = this.SessionService.Session;

            if (!session.IsLoggedIn)
            {
                return(this.BadRequest());
            }

            var post = await this.PostService.GetPostById(postId);

            if (post is null)
            {
                return(this.BadRequest());
            }

            if (parentId.HasValue)
            {
                var parentComment = await this.CommentService.GetCommentById(parentId.Value);

                if (parentComment == null)
                {
                    return(this.BadRequest());
                }

                // We don't want more than 1 level of nesting
                if (parentComment.ParentId.HasValue)
                {
                    return(this.BadRequest());
                }
            }

            if (string.IsNullOrWhiteSpace(content.Trim()))
            {
                return(this.BadRequest());
            }

            string sanitizedContent = Regex.Replace(content, @"(\n){2,}", Environment.NewLine);

            var now = DateTime.Now;

            var commentPoco = new CommentPoco
            {
                Content   = sanitizedContent,
                UserId    = session.UserAccount.UserId,
                PostId    = post.Id,
                ParentId  = parentId,
                CreatedOn = now
            };

            int?commentId = await this.CommentService.CreateComment(commentPoco);

            var resposeCommentDto = new CommentDto
            {
                CommentId = commentId,
                Content   = sanitizedContent,
                Username  = session.UserAccount.Username,
                AvatarUrl = session.UserAccount.Avatar,
                HumanDate = DateUtils.DateTimeToLongAgo(now)
            };

            return(this.Ok(resposeCommentDto));
        }