예제 #1
0
파일: NewsService.cs 프로젝트: mjq003/News
        /// <summary>
        /// 查询新闻列表
        /// </summary>
        public ResponseModel GetNewsList(Expression <Func <News, bool> > where, int NewsClassifyId, int topCount)
        {
            var listAll = _db.News.Include("NewsClassify").Include("NewsComment");
            var list    = new List <News>();

            if (NewsClassifyId == 0) //所有球队
            {
                list = listAll.Where(s => s.NewsClassify.ParentId == 0 && s.NewsClassify.ClassifyType == 0).OrderByDescending(c => c.PublishDate).Take(topCount).ToList();
            }
            else
            {
                var NewsClassifylst = _db.NewsClassify.Find(NewsClassifyId);
                if (NewsClassifylst.ParentId == 0 && NewsClassifylst.ClassifyType == 1)
                {
                    var newsIds = _db.NewsClassify.Where(s => s.ParentId == NewsClassifyId).Select(c => c.Id);
                    list = listAll.Where(s => newsIds.Contains(s.NewsClassifyId)).OrderByDescending(c => c.PublishDate).Take(topCount).ToList();
                }
                else
                {
                    list = listAll.Where(s => s.NewsClassifyId == NewsClassifyId).OrderByDescending(c => c.PublishDate).Take(topCount).ToList();
                }
            }
            var response = new ResponseModel
            {
                code   = 200,
                result = "新闻列表获取成功"
            };

            response.data = new List <NewsModel>();
            HtmlToText convert = new HtmlToText();

            foreach (var news in list)
            {
                response.data.Add(new NewsModel
                {
                    Id           = news.Id,
                    ClassifyName = news.NewsClassify.Name,
                    Title        = news.Title,
                    Image        = news.Image == null ? convert.ConvertImgSrc(news.Contents) : news.Image,
                    Contents     = convert.Convert(news.Contents).Length > 50 ? $"{convert.Convert(news.Contents).Substring(0, 50)}..." : convert.Convert(news.Contents),
                    PublishDate  = news.PublishDate.ToString("yyyy-MM-dd"),
                    CommentCount = news.NewsComment.Count(),
                    LoveCount    = news.NewsComment.Sum(s => s.Love),
                    Remark       = news.Remark
                });
            }
            return(response);
        }