public async Task Comment([FromBody] ArticleCommentInput comment) { //获取验证码 string _code = HttpContext.Session.GetString(StringSession.ArticleCommentKey); if (string.IsNullOrEmpty(_code) || _code != comment.Code.ToLower()) { throw new UserFriendlyException(403, "验证码有误"); } await _blogAppService.CreateArticleCommentAsync(comment); HttpContext.Session.Remove(StringSession.ArticleCommentKey); }
public async Task CreateArticleCommentAsync(ArticleCommentInput articleComment) { var commentLast = await _cacheManager.GetCache(BlogCacheNames.CacheArticleComment) .GetOrDefaultAsync(_clientInfoProvider.ClientIpAddress); if (commentLast != null) { throw new UserFriendlyException(401, $"上次评论时间为{commentLast},必须间隔1分钟"); } var _articleComment = ObjectMapper.Map <ArticleComment>(articleComment); var article = await _articleRepository.FirstOrDefaultAsync(_articleComment.ArticleId); if (article == null) { throw new UserFriendlyException(404, "找不到文章信息"); } if (_articleComment.ParArticleCommentId.HasValue) { var _comment = await _articleCommentRepository.FirstOrDefaultAsync(_articleComment.ParArticleCommentId.Value); //只开放二级评论 var flag = await _articleCommentRepository.GetAll().AnyAsync(p => p.Id == _comment.ParArticleCommentId && p.ParArticleCommentId.HasValue); if (flag) { throw new UserFriendlyException(500, "目前只开放二级评论"); } _articleComment.ReplyNickName = _comment.NickName; _articleComment.ParArticleCommentId = _comment.ParArticleCommentId ?? _articleComment.ParArticleCommentId; } else { //如何是评论,楼层数加1,并加上楼层 article.FloorNum++; _articleComment.Floor = article.FloorNum; } //如果是管理无需审核 if (await _permissionChecker.IsGrantedAsync(PermissionNames.Pages_Blogs_Admin)) { _articleComment.ToExamine = ToExamine.Adopt; } //获取评论的IP _articleComment.Ip = _clientInfoProvider.ClientIpAddress; await _articleCommentRepository.InsertAsync(_articleComment); article.CommentNum++; await _articleRepository.UpdateAsync(article); await _cacheManager.GetCache(BlogCacheNames.CacheArticleComment) .SetAsync(_clientInfoProvider.ClientIpAddress, DateTime.Now.ToString()); }