コード例 #1
0
ファイル: ApiHandler.cs プロジェクト: atsmith6/SGForum
        public async Task <TopicResult> getTopics(Int64 tokenId, int?parentTopicId)
        {
            var token = await quickGetToken(tokenId, true);

            List <Topic> topics;
            int          tokenRole = token.User.RawRole;
            var          parentId  = parentTopicId;

            if (parentId != null)
            {
                topics = await(from t in _context.topics
                               where t.ParentId == parentId && t.IsRootEntry == false && t.RoleToRead <= tokenRole
                               select t).ToListAsync();
            }
            else
            {
                topics = await(from t in _context.topics
                               where t.ParentId == null && t.IsRootEntry == true && t.RoleToRead <= tokenRole
                               select t).ToListAsync();
            }
            TopicResult ret = new TopicResult();

            for (int i = 0; i < topics.Count; ++i)
            {
                ret.Topics.Add(topics[i].CloneForExport());
            }

            ret.TopicCount = ret.Topics.Count;

            if (parentId != null)
            {
                ret.ParentList = await TopicTasks.CreateParentList(_context, parentId.Value);
            }
            return(ret);
        }
コード例 #2
0
ファイル: ApiHandler.cs プロジェクト: atsmith6/SGForum
        public async Task <TopicResult> getTopicPage(Int64 tokenId, int?parentTopicId, int page, int pageSize)
        {
            var token = await quickGetToken(tokenId, true);

            var parentId = parentTopicId;

            if (page == -1)
            {
                page = 0;
            }

            int count;

            if (parentId == null)
            {
                count = await(from t in _context.topics
                              where t.ParentId == null && t.RoleToRead <= token.User.RawRole
                              select t).CountAsync();
            }
            else
            {
                count = await(from t in _context.topics
                              where t.ParentId == parentId.Value && t.RoleToRead <= token.User.RawRole
                              select t).CountAsync();
            }
            var pageCount = count / pageSize;

            if (count % pageSize != 0)
            {
                ++pageCount;
            }
            if (page >= pageCount)
            {
                page = Math.Max(0, pageCount - 1);
            }

            int begin = (page) * pageSize;

            IQueryable <Topic> query;

            if (parentId == null)
            {
                query = (from t in _context.topics
                         where t.ParentId == null && t.RoleToRead <= token.User.RawRole
                         orderby t.Modified descending
                         select t);
            }
            else
            {
                query = (from t in _context.topics
                         where t.ParentId == parentId.Value && t.RoleToRead <= token.User.RawRole
                         orderby t.Modified descending
                         select t);
            }
            var topics = await query.Skip(begin).Take(pageSize).ToListAsync();

            var topicData = new TopicResult();

            if (parentId != null)
            {
                topicData.ParentList = await TopicTasks.CreateParentList(_context, parentId.Value);
            }
            foreach (var topic in topics)
            {
                topicData.Topics.Add(topic.CloneForExport());
            }
            topicData.TopicCount  = count;
            topicData.CurrentPage = page;
            topicData.PageCount   = pageCount;
            return(topicData);
        }