コード例 #1
0
        public async Task <IActionResult> Post([FromBody] CommentItem item)
        {
            var commentRepository = _unitOfWork.GetRepository <Comment>();
            var postRepository    = _unitOfWork.GetRepository <Post>();

            try
            {
                var post = await postRepository.GetFirstOrDefaultAsync(predicate : m => m.Id == item.PostId);

                var newItem = new Comment()
                {
                    Content     = item.Content,
                    CreateDate  = DateTime.UtcNow,
                    DisplayName = item.DisplayName,
                    Ip          = AspNetCoreHelper.GetRequestIP(),
                    Posts       = post,
                    Email       = item.Email,
                    IsApproved  = true,
                    ParentId    = item.ParentId,
                };
                await commentRepository.InsertAsync(newItem);

                await _unitOfWork.SaveChangesAsync();

                item.Id          = newItem.Id;
                item.HasChildren = false;
                item.IsApproved  = true;
                item.DateCreated = newItem.CreateDate.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
                return(Ok(item));
            }
            catch (Exception ex)
            {
                return(NotFound());
            }
        }
コード例 #2
0
        public CommentItem Add(CommentDetail item)
        {
            var c = new Comment();

            try
            {
                var post = _ctx.Posts.Where(p => p.Id == item.PostId).FirstOrDefault();
                c.CreateDate = DateTime.Now;
                c.ParentId   = item.ParentId;
                c.IsApproved = true;
                c.Content    = item.Content;
                //if (string.IsNullOrEmpty(item.Author.Id))
                //{
                //    var guid = string.NewGuid().ToString().Replace("-", "");
                //    var user = new AppUser { UserName = "******" + guid, Email = item.Author.Email, DisplayName = "匿名_" + item.Author.DisplayName };
                //    user.Avatar = AvatarHelper.GetRandomAvatar();
                //    var result = _userManager.CreateAsync(user).Result;
                //    if (!result.Succeeded)
                //    {
                //        return null;
                //    }
                //    item.Author = user;
                //}
                //if (item.Author != null)
                //{
                //    c.AuthorId = item.Author.Id;
                //    c.DisplayName = item.Author.DisplayName;
                //    c.SiteUrl = item.Author.SiteUrl;
                //}
                c.DisplayName = item.DislpayName;
                c.Email       = item.Email;
                c.Ip          = AspNetCoreHelper.GetRequestIP();
                c.Posts       = post;
                _ctx.Comments.Add(c);
                _ctx.SaveChanges();
                //var profile = AuthorProfile.GetProfile(c.Author);
                //if (profile != null && !string.IsNullOrEmpty(profile.DisplayName))
                //{
                //    c.Author = profile.DisplayName;
                //}
                //c.Email = Membership.Provider.GetUser(Security.CurrentUser.Identity.Name, true).Email;
                //c.IP = WebUtils.GetClientIP();
                //c.DateCreated = DateTime.Now;
                //c.Parent = post;
                var newComm = post.Comments.Where(cm => cm.Content == c.Content).FirstOrDefault();
                return(_jsonService.GetComment(newComm, post.Comments.ToList()));
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
コード例 #3
0
ファイル: RootController.cs プロジェクト: chenrensong/OneBlog
        public async Task <IActionResult> Comment(CommentViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new
                {
                    Error = "提交的信息有误,请检查后再试"
                }));
            }
            var validateCode = _session.GetString("ValidateCode");

            if (string.IsNullOrEmpty(validateCode))
            {
                return(Json(new
                {
                    Error = "验证码过期,请刷新重试!",
                }));
            }
            _session.Remove("ValidateCode");
            if (!string.Equals(validateCode, model.Captcha, StringComparison.OrdinalIgnoreCase))
            {
                return(Json(new
                {
                    Error = "提交的验证码错误!",
                }));
            }
            var replyToCommentId  = Request.Form["hiddenReplyTo"].ToString();
            var commentRepository = _unitOfWork.GetRepository <Comment>();
            var postRepository    = _unitOfWork.GetRepository <Post>();
            var post = await postRepository.GetFirstOrDefaultAsync(predicate : m => m.Id == model.PostId, disableTracking : false);

            var commentItem = new CommentItem()
            {
                PostId = model.PostId, DisplayName = model.UserName, Email = model.Email, Content = model.Content
            };

            if (!string.IsNullOrEmpty(replyToCommentId))
            {
                commentItem.ParentId = replyToCommentId;
            }
            var newItem = new Comment()
            {
                Content     = commentItem.Content,
                CreateDate  = DateTime.UtcNow,
                DisplayName = commentItem.DisplayName,
                Ip          = AspNetCoreHelper.GetRequestIP(),
                Posts       = post,
                Email       = commentItem.Email,
                IsApproved  = true,
                ParentId    = commentItem.ParentId,
            };
            await commentRepository.InsertAsync(newItem);

            await _unitOfWork.SaveChangesAsync();

            commentItem.Id          = newItem.Id;
            commentItem.HasChildren = false;
            commentItem.IsApproved  = true;
            commentItem.DateCreated = newItem.CreateDate.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);

            var result = await _viewRenderService.RenderToStringAsync(this, "_Comment", commentItem);

            return(Json(new
            {
                Error = "",
                CommentId = commentItem.Id,
                CommentCount = (post.Comments.Count + 1),
                Result = result,
                Content = model.Content
            }));
        }