Exemplo n.º 1
0
        public ApiResult <CommentResDto> Publish(CommentReqDto reqDto)
        {
            #region 验证
            if (reqDto == null)
            {
                throw new ApiException(400, "验证参数失败");
            }
            if (reqDto.CommentType != 1 && reqDto.CommentType != 2)
            {
                throw new ApiException(40000, "评论类型超出业务范围");
            }
            if (reqDto.Id <= 0)
            {
                throw new ApiException(40000, "验证参数Id失败");
            }

            if (string.IsNullOrWhiteSpace(reqDto.Content))
            {
                throw new ApiException(40000, "内容不能为空");
            }

            if (reqDto.Type != 1 && reqDto.Type != 2)
            {
                throw new ApiException(40000, "评论对象类型超出业务范围");
            }
            #endregion

            return(commentService.Publish(reqDto, this.UserInfo));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 发表评论
        /// </summary>
        /// <param name="reqDto"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public ApiResult <CommentResDto> Publish(CommentReqDto reqDto, IdentityInfo user)
        {
            #region step1.黑名单和禁言验证
            //step1.黑名单和禁言验证
            string validateSql = @"select count(1) from [dbo].[UserState]
	  where ([CommentBlack]=1 or ([CommentShut]=1 and GETDATE() between [CommentShutBegin] and [CommentShutEnd])) and UserId="     +
                                 user.UserId;

            object obj = SqlHelper.ExecuteScalar(validateSql);

            if (obj != null && Convert.ToInt32(obj) > 0)
            {
                throw new ApiException(10001, "你已被禁言或拉黑");
            }
            #endregion

            #region step2.评论/回复对象是否存在

            long refId        = 0;
            long pid          = 0;
            long refCommentId = 0;
            int  refUserId    = reqDto.RefUserId;

            if (reqDto.CommentType == 1)
            {
                if (reqDto.Type == 1)
                {
                    //查询计划
                    //var model = Util.GetEntityById<BettingRecord>(reqDto.Id);
                    //if (model == null) throw new ApiException(400, "计划已不存在或已删除");

                    //id为彩种Id。对计划评论为用户在该彩种的评论
                    refId = reqDto.Id;
                    //refCommentId=
                }
                else if (reqDto.Type == 2)
                {
                    //查询文章
                    var model = Util.GetEntityById <News>(reqDto.Id);
                    if (model == null)
                    {
                        throw new ApiException(400, "资讯不存在或已删除");
                    }
                    refId = model.Id;
                }
            }
            else
            {
                //查询评论
                var model = Util.GetEntityById <Comment>(reqDto.Id);
                if (model == null)
                {
                    throw new ApiException(400, "评论不存在或已删除");
                }
                refId = model.Id;
                pid   = model.Id;

                if (model.RefCommentId > 0)
                {
                    //第N级回复,则关联评论Id=上一级评论关联Id
                    refCommentId = model.RefCommentId;
                    refUserId    = model.UserId;
                }
                else
                {
                    //第一级回复,则关联评论Id=上一级评论Id
                    refCommentId = model.Id;
                }
            }


            #endregion

            #region step3.评论内容过滤
            //去Html标签
            string content = WebHelper.NoHtml(reqDto.Content);
            //脏字过滤
            content = WebHelper.FilterSensitiveWords(content, "*");
            #endregion


            #region step4.添加评论
            string sql = @"INSERT INTO [dbo].[Comment]
           ([PId],[UserId],[Content],[SubTime],[Type],[ArticleId]
           ,[StarCount],[IsDeleted],[RefCommentId],[ArticleUserId])
     VALUES(@PId,@UserId,@Content,GETDATE()
           ,@Type,@ArticleId,0,0,@RefCommentId,@ArticleUserId);SELECT IDENT_CURRENT('Comment')";

            SqlParameter[] parameters =
            {
                new SqlParameter("@PID",           pid),
                new SqlParameter("@UserId",        user.UserId),
                new SqlParameter("@Content",       content),
                new SqlParameter("@Type",          reqDto.Type),
                new SqlParameter("@ArticleId",     refId),
                new SqlParameter("@RefCommentId",  refCommentId),
                new SqlParameter("@ArticleUserId", refUserId),
            };
            int row = SqlHelper.ExecuteScalar(sql, parameters).ToInt32();
            if (row <= 0)
            {
                throw new ApiException(50000, "服务器繁忙");
            }

            //添加评论图资源关联
            resourceService.InsertResources((int)ResourceTypeEnum.评论图, row, reqDto.Pictures);

            #endregion

            #region 获取评论实体
            string getSql =
                @"select top 1 a.*,isnull(b.Name,'') as NickName,isnull(c.RPath,'') as Avater
,0 as CurrentUserLikes,0 as ReplayCount 
from Comment a
  left join UserInfo b on b.Id = a.UserId
  left join ResourceMapping c on c.FkId = a.UserId and c.Type = @ResourceType
  where a.IsDeleted = 0 and a.Id=@Id";
            var getParameters = new[]
            {
                new SqlParameter("@Id", row),
                new SqlParameter("@ResourceType", (int)ResourceTypeEnum.评论图),
            };

            var    list          = Util.ReaderToList <Comment>(getSql, getParameters);
            string webHost       = ConfigurationManager.AppSettings["webHost"];
            string defaultAvater = string.Format("{0}/images/default_avater.png", webHost);
            int    resouceType   = (int)ResourceTypeEnum.评论图;
            var    resDto        = list.Select(x => new CommentResDto()
            {
                Id           = x.Id,
                ArticleId    = x.ArticleId,
                Avater       = string.IsNullOrWhiteSpace(x.Avater) ? defaultAvater : x.Avater,
                Content      = x.Content,
                IsLike       = x.CurrentUserLikes > 0,
                NickName     = x.NickName,
                PId          = x.PId,
                RefCommentId = x.RefCommentId,
                ReplayCount  = x.ReplayCount,
                UserId       = x.UserId,
                Pictures     = resourceService.GetResources(resouceType, x.Id)
                               .Select(n => n.RPath).ToList(),
                SubTime = x.SubTime
            }).FirstOrDefault();



            #endregion

            return(new ApiResult <CommentResDto>()
            {
                Data = resDto
            });
        }