示例#1
0
 /// <summary>
 /// 获取文章管理页面列表
 /// </summary>
 /// <param name="pageIndex"></param>
 /// <param name="pageSize"></param>
 /// <param name="sortName"></param>
 /// <param name="sortOrder"></param>
 /// <param name="searchText"></param>
 /// <param name="cateAlias"></param>
 /// <param name="uniqueId"></param> 
 /// <param name="title"></param> 
 /// <returns></returns>
 public async Task<PostResult> GetArticles(int pageIndex, int pageSize, string sortName, string sortOrder, string searchText, string cateAlias, string uniqueId, string title)
 {
     var result = new PostResult();
     var query = await _posts.Collection.Find(x => x.IsActive).ToListAsync();
     if (!string.IsNullOrWhiteSpace(searchText))
     {
         query = query.Where(x => x.Content != null ? (x.UniqueId.ToUpper().Contains(searchText.ToUpper()) || x.Title.ToUpper().Contains(searchText.ToUpper()) || x.Summary.ToUpper().Contains(searchText.ToUpper()) || x.Content.ToUpper().Contains(searchText.ToUpper())) : (x.UniqueId.ToUpper().Contains(searchText.ToUpper()) || x.Title.ToUpper().Contains(searchText.ToUpper()) || x.Summary.ToUpper().Contains(searchText.ToUpper()))).ToList();
     }
     if (!string.IsNullOrWhiteSpace(cateAlias))
     {
         query = query.Where(x => x.CategoryAlias.ToUpper() == cateAlias.ToUpper()).ToList();
     }
     if (!string.IsNullOrWhiteSpace(uniqueId))
     {
         query = query.Where(x => x.UniqueId.ToUpper().Contains(uniqueId.ToUpper())).ToList();
     }
     if (!string.IsNullOrWhiteSpace(title))
     {
         query = query.Where(x => x.Title.ToUpper().Contains(title.ToUpper())).ToList();
     }
     result.PostCount = query.Count;
     switch (sortName)
     {
         case "ModifyTime":
             if (sortOrder == "desc")
             {
                 query = query.OrderByDescending(x => x.ModifyTime).ThenByDescending(x => x.CreateTime).ToList();
             }
             else
             {
                 query = query.OrderBy(x => x.ModifyTime).ThenBy(x => x.CreateTime).ToList();
             }
             break;
         case "ViewCount":
             if (sortOrder == "desc")
             {
                 query = query.OrderByDescending(x => x.ViewCount).ThenByDescending(x => x.CreateTime).ToList();
             }
             else
             {
                 query = query.OrderBy(x => x.ViewCount).ThenBy(x => x.CreateTime).ToList();
             }
             break;
         default:
             if (sortOrder == "desc")
             {
                 query = query.OrderByDescending(x => x.CreateTime).ToList();
             }
             else
             {
                 query = query.OrderBy(x => x.CreateTime).ToList();
             }
             break;
     }
     result.PostList = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
     return result;
 }
示例#2
0
 /// <summary>
 /// 获取筛选条件下的所有文章
 /// </summary>
 /// <param name="cateAlias"></param>
 /// <param name="pageIndex"></param>
 /// <param name="pageSize"></param>
 /// <param name="sortBy"></param>
 /// <param name="filtType"></param>
 /// <param name="keyword"></param>
 /// <returns></returns>
 public async Task<PostResult> GetPosts(string cateAlias, int pageIndex, int pageSize, SortBy sortBy = SortBy.Latest, FiltType filtType = FiltType.Content, string keyword = "")
 {
     var result = new PostResult();
     var query = await _posts.Collection.Find(x => x.IsActive).ToListAsync();
     if (!string.IsNullOrWhiteSpace(cateAlias))
     {
         query = query.Where(x => x.CategoryAlias.ToUpper() == cateAlias.ToUpper()).ToList();
     }
     if (!string.IsNullOrWhiteSpace(keyword))
     {
         if (filtType == FiltType.Title)
         {
             query = query.Where(x => x.Title.Contains(keyword)).ToList();
         }
         else if (filtType == FiltType.Label)
         {
             query = query.Where(x => x.Labels.Contains(keyword)).ToList();
         }
         else if (filtType == FiltType.Datetime)
         {
             DateTime dt;
             DateTime.TryParse(keyword, out dt);
             query = query.Where(x => x.CreateTime.Date == dt.Date).ToList();
         }
         else
         {
             query = query.Where(x => x.Content != null ? (x.Title.Contains(keyword) || x.Summary.Contains(keyword) || x.Content.Contains(keyword)) : (x.Title.Contains(keyword) || x.Summary.Contains(keyword))).ToList();
         }
     }
     result.PostCount = query.Count;
     if (sortBy == SortBy.Hotest)
     {
         query = query.OrderByDescending(x => x.ViewCount).ThenByDescending(x => x.CreateTime).ToList();
     }
     else
     {
         query = query.OrderByDescending(x => x.CreateTime).ToList();
     }
     result.PostList = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
     return result;
 }