/// <summary> /// 分页根据显示获取文章 /// </summary> /// <param name="limit"></param> /// <param name="pageIndex"></param> /// <param name="categoryId"></param> /// <param name="isShow"></param> /// <param name="title"></param> /// <returns></returns> public async Task <PageDTO <ArticleDTO> > GetArticleListByCategoryIdAsync(int limit, int pageIndex, int categoryId, bool isShow, string title) { Expression <Func <ArticleEntity, bool> > expression = t => true; if (isShow) { expression = expression.And(ac => ac.isShow); } if (!string.IsNullOrEmpty(title)) { expression = expression.And(ac => ac.title.Contains(title)); } if (categoryId > 0) { expression = expression.And(ac => ac.categoryId == categoryId); } var articles = ArticleRepository.GetModelsByPage(limit, pageIndex, false, a => a.lastUpdateDate, expression, out var pageCount); var articleDtoList = Mapper.Map <List <ArticleDTO> >(articles .Include(t => t.category).ToList()); foreach (var articleDto in articleDtoList) { articleDto.tags = await ArticleTagService.GetTagListByArticleIdAsync(articleDto.id); articleDto.content = StringHelper.ReplaceTag(articleDto.content, 500); } return(new PageDTO <ArticleDTO>(limit, pageIndex, pageCount, articleDtoList)); }
/// <summary> /// 根据文章id获取文章DTO /// </summary> /// <param name="articleId">文章编码</param> /// <param name="isShow">是否显示</param> /// <param name="addVisit">是否添加浏览数</param> /// <returns></returns> public async Task <ArticleDTO> GetArticleByIdAsync(int articleId, bool isShow, bool addVisit) { ArticleEntity article; if (isShow) { article = await ArticleRepository.GetSingleModelAsync(a => a.id == articleId && a.isShow); } else { article = await ArticleRepository.GetSingleModelAsync(a => a.id == articleId); } if (article == null) { throw new Exception("未找到文章"); } if (addVisit) { await AddArticleVisitAsync(article); } var articleDto = Mapper.Map <ArticleDTO>(article); articleDto.category = await CategoryService.GetCategoryByIdAsync(articleDto.categoryId); articleDto.tags = await ArticleTagService.GetTagListByArticleIdAsync(articleId); return(articleDto); }
/// <summary> /// 根据文章标题模糊查询 /// </summary> /// <param name="dimTitle"></param> /// <param name="isShow"></param> /// <returns></returns> public async Task <List <ArticleDTO> > GetArticlesByDimTitleAsync(string dimTitle, bool isShow) { var articles = ArticleRepository.GetModels(a => a.title.Contains(dimTitle) && isShow ? a.isShow == true : a.id > 0).Include(c => c.category); var articleDtoList = Mapper.Map <List <ArticleDTO> >(articles.ToList().Where(a => a.isShow)); foreach (var articleDto in articleDtoList) { articleDto.tags = await ArticleTagService.GetTagListByArticleIdAsync(articleDto.id); articleDto.content = StringHelper.ReplaceTag(articleDto.content, 500); } return(articleDtoList); }