示例#1
0
        /// <summary>
        /// 投票
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task VoteAsync(ArticleVoteRequest request)
        {
            var articleKey = string.Format(CacheConst.Article, request.ArticleId);

            //检查文章是否超过投票时间(文章发布一周内可投)
            var time = GetTimestamp(DateTime.Now) - CacheConst.OneWeekSeconds;
            var publishTime = await RedisHelper.ZScoreAsync(CacheConst.Time, articleKey);
            if (publishTime < time)
                return;

            var votedKey = string.Format(CacheConst.Voted, request.ArticleId);
            //第一次投票添加记录
            var isSuccess = await RedisHelper.SAddAsync(votedKey, request.User);
            if(isSuccess == 0L ? false : true)
            {
                //增加评分
                await RedisHelper.ZIncrByAsync(CacheConst.Score,articleKey,CacheConst.VoteScore);
                //增加票数
                await RedisHelper.HIncrByAsync(articleKey,"votes",1);
            }
        }
示例#2
0
        public async Task <IActionResult> VoteAsync([FromBody] ArticleVoteRequest request)
        {
            await ArticleService.VoteAsync(request);

            return(Ok());
        }