public async Task <IActionResult> Query([FromQuery] TopicRequestCondition Request = null)
        {
            IPagedList <BZTopicModel> pagedList = null;
            var query = Request.CreateQueryExpression <BZTopicModel, TopicRequestCondition>();

            query     = query.And(p => p.Status == 0);
            pagedList = await _bZTopicRepository.GetPagedListAsync(query, o => o.OrderBy(p => p.Id), null, Request.PageIndex - 1, Request.PageSize);

            if (pagedList != null && pagedList.Items.Any())
            {
                var pagedatas = pagedList.From(res => _mapper.Map <List <PersonalTopicDisplayDto> >(res));

                var users = await _cacheService.GetUsersAsync(p => pagedList.Items.Select(d => d.CreatorId).Contains(p.Id));

                foreach (PersonalTopicDisplayDto topic in pagedatas.Items)
                {
                    var user = users.FirstOrDefault(p => p.Id == topic.CreatorId);
                    if (user != null)
                    {
                        topic.UserName = user?.UserName;
                        topic.Avator   = user?.Avator;
                        topic.NickName = user?.NickName;
                    }
                }
                if (pagedatas is null || pagedatas.TotalCount == 0)
                {
                    return(NoContent());
                }
                return(Ok(pagedatas));
            }

            return(NoContent());
        }
        public async Task <IActionResult> SeachTopicByTitle(string Title, int PageIndex, int PageSize)
        {
            IPagedList <BZTopicModel> pagedList = null;
            Expression <Func <BZTopicModel, bool> > expression = p => p.Status == 0;

            if (string.IsNullOrWhiteSpace(Title))
            {
                return(NoContent());
            }
            expression = expression.And(p => p.Title.Contains(Title.Replace(" ", "")));
            pagedList  = await _bZTopicRepository.GetPagedListAsync(expression, o => o.OrderBy(p => p.Id), null, PageIndex - 1, PageSize);

            if (pagedList.Items.Any())
            {
                return(Ok(pagedList.From(res => _mapper.Map <List <SeachTopicDto> >(res))));
            }
            else
            {
                return(NoContent());
            }
        }
        public async Task <ActionResult <IPagedList <TopicDisplayDto> > > Query([FromQuery] TopicRequestCondition Request = null)
        {
            IPagedList <BZTopicModel> pagedList = null;
            var query = Request.CreateQueryExpression <BZTopicModel, TopicRequestCondition>();

            if (!string.IsNullOrWhiteSpace(Request.UserName))
            {
                var Users = await _cacheService.GetUsersAsync(p =>
                                                              p.UserName.Contains(Request.UserName) ||
                                                              p.NickName.Contains(Request.UserName));

                if (Users != null)
                {
                    query = query.And(p => Users.Select(x => x.Id).Contains(p.CreatorId));
                }
                else
                {
                    return(NoContent());
                }
            }
            pagedList = await _bZTopicRepository.GetPagedListAsync(query, o => o.OrderBy(p => p.Id), null, Request.PageIndex - 1, Request.PageSize);

            if (pagedList.Items.Any())
            {
                var pagedatas = pagedList.From(result => _mapper.Map <List <TopicDisplayDto> >(result));
                var users     = await _cacheService.GetUsersAsync(p => pagedList.Items.Select(d => d.CreatorId).Contains(p.Id));

                foreach (TopicDisplayDto topic in pagedatas.Items)
                {
                    topic.UserName = users.FirstOrDefault(p => p.Id == topic.CreatorId)?.UserName;
                    if (!string.IsNullOrWhiteSpace(topic.RoleId))
                    {
                        topic.RoleName = roleManager.Roles.FirstOrDefault(p => p.Id == topic.RoleId)?.Name;
                    }
                }
                return(Ok(pagedatas));
            }
            return(NoContent());
        }
        public async Task <IActionResult> QueryTopicsByOrder([SwaggerParameter(Required = false)] int orderType = 0,
                                                             [SwaggerParameter(Required = false)] int topicType = -1,
                                                             [SwaggerParameter(Required = false)] int pageSize  = 10,
                                                             [SwaggerParameter(Required = false)] int pageIndex = 1)
        {
            if (orderType < 0)
            {
                return(new BadRequestResponse(" topicType id  error"));
            }
            var topicRepo = _unitOfWork.GetRepository <BZTopicModel>(true);
            IPagedList <BZTopicModel> pagedList = null;
            Expression <Func <BZTopicModel, bool> > predicate = p => p.Status != -1;//-1 已删除

            if (topicType != -1)
            {
                predicate = predicate.And(p => p.Category == topicType);
            }
            switch (orderType)
            {
            case 0:
                pagedList = await _bZTopicRepository.GetPagedListAsync(predicate, o => o.OrderByDescending(o => o.LastModifyDate).ThenByDescending(o => (o.ReplyCount * 1.5 + o.Hot)), null, pageIndex - 1, pageSize);

                break;

            case 1:
                pagedList = await _bZTopicRepository.GetPagedListAsync(predicate, o => o.OrderByDescending(o => o.ReplyCount).ThenByDescending(o => (o.Hot)), null, pageIndex - 1, pageSize);

                break;

            case 2:
                predicate = predicate.And(p => p.Good == 1);    //精华帖
                pagedList = await _bZTopicRepository.GetPagedListAsync(predicate, o => o.OrderByDescending(o => o.LastModifyDate).ThenByDescending(o => (o.Good)), null, pageIndex - 1, pageSize);

                break;

            case 3:
                Expression <Func <BZTopicModel, bool> > predicateEnd = p => p.Status == 1; //1 已结帖
                pagedList = await _bZTopicRepository.GetPagedListAsync(predicateEnd, o => o.OrderByDescending(p => p.LastModifyDate), null, pageIndex - 1, pageSize);

                break;

            default:
                break;
            }
            if (pagedList is null || pagedList.TotalCount == 0)
            {
                return(NoContent());
            }
            var pagedatas      = pagedList.From(res => _mapper.Map <List <BZTopicDto> >(res));
            var userRepository = _unitOfWork.GetRepository <BZUserModel>();
            var users          = await _cacheService.GetUsersAsync(p => pagedList.Items.Select(d => d.CreatorId).Contains(p.Id));

            foreach (BZTopicDto topic in pagedatas.Items)
            {
                var user = users.FirstOrDefault(p => p.Id == topic.CreatorId);
                topic.UserName  = user?.UserName;
                topic.Avator    = user?.Avator;
                topic.NickName  = user?.NickName;
                topic.Signature = user?.Signature;
            }
            if (pagedatas is null || pagedatas.TotalCount == 0)
            {
                return(NoContent());
            }
            return(Ok(pagedatas));
        }
Exemple #5
0
        public async Task <IActionResult> QueryReplys([FromQuery] ReplyRequestCondition Request = null)
        {
            IPagedList <BZReplyModel> pagedList = null;
            var query = Request.CreateQueryExpression <BZReplyModel, ReplyRequestCondition>();

            Expression <Func <BZTopicModel, bool> > where = p => true;
            if (!string.IsNullOrWhiteSpace(Request.Title))
            {
                where = where.And(p => p.Title.Contains(Request.Title));
                var Topics = await _cacheService.GetTopicsAsync(where);

                if (Topics != null && Topics.Any())
                {
                    query = query.And(p => Topics.Select(x => x.Id).Contains(p.TopicId));
                }
                else
                {
                    return(NoContent());
                }
            }
            if (!string.IsNullOrWhiteSpace(Request.UserName))
            {
                var Users = await _cacheService.GetUsersAsync(p =>
                                                              p.UserName.Contains(Request.UserName) ||
                                                              p.NickName.Contains(Request.UserName));

                if (Users.Any())
                {
                    query = query.And(p => Users.Select(x => x.Id).Contains(p.CreatorId));
                }
                else
                {
                    return(NoContent());
                }
            }
            pagedList = await _replyRepository.GetPagedListAsync(query, o => o.OrderByDescending(p => p.CreateDate), null, Request.PageIndex - 1, Request.PageSize);

            if (pagedList.Items.Any())
            {
                var pagedatas = pagedList.From(result => _mapper.Map <List <ReplyDisplayDto> >(result));
                var topics    = await _cacheService.GetTopicsAsync(p => pagedList.Items.Select(d => d.TopicId).Contains(p.Id));

                var users = await _cacheService.GetUsersAsync(p => pagedList.Items.Select(d => d.CreatorId).Contains(p.Id));

                foreach (var replyDto in pagedatas.Items)
                {
                    var user  = users.FirstOrDefault(p => p.Id == replyDto.CreatorId);
                    var topic = topics.FirstOrDefault(p => p.Id == replyDto.TopicId);
                    if (topic != null)
                    {
                        var topicuser = (await _cacheService.GetUsersAsync(p => true)).FirstOrDefault(p => p.Id == topic.CreatorId);
                        if (user != null)
                        {
                            replyDto.NickName = user?.NickName;
                            replyDto.Author   = topicuser?.NickName;
                            replyDto.UserId   = user.Id;
                            replyDto.Title    = topic?.Title;
                        }
                    }
                }
                return(Ok(pagedatas));
            }
            else
            {
                return(NoContent());
            }
        }