Exemplo n.º 1
0
        private List <NewsForShow> NewsSearch(string keyword, string author_id)
        {
            //如果keyword为空,不做要求
            if (keyword == null)
            {
                keyword = "";
            }
            //如果authorid为空,则不做要求
            bool isAuthorId = true;

            if (author_id == string.Empty || author_id == null)
            {
                isAuthorId = false;
            }
            var newsList = from news in _context.News
                           where (KeywordSearch.ContainsKeywords(news.title + " " + news.content, keyword)) &&
                           ((!isAuthorId) || (news.author_id == author_id))
                           orderby news.post_date descending
                           select news;

            if (newsList.Count() == 0)
            {
                throw (new Exception("没有找到满足条件的数据"));
            }
            List <NewsForShow> showList         = new List <NewsForShow>();
            string             contract_content = new string("");

            foreach (news nwsrow in newsList)
            {
                if (nwsrow.content.Count() > 50)
                {
                    contract_content = nwsrow.content.Substring(0, 50) + "...";
                }
                else
                {
                    contract_content = nwsrow.content;
                }
                NewsForShow temp = new NewsForShow()
                {
                    author_name = nwsrow.author_name,
                    author_id   = nwsrow.author_id,
                    content     = contract_content,
                    news_id     = nwsrow.news_id,
                    part        = nwsrow.part,
                    post_date   = nwsrow.post_date.ToString(),
                    title       = nwsrow.title
                };
                showList.Add(temp);
            }
            return(showList);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Getnews(string news_id)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw (new Exception("Bad Request,ModelState:" + ModelState.ToString()));
                }

                var news = await _context.News.FindAsync(news_id);

                if (news == null)
                {
                    throw (new Exception("没有找到满足条件的数据"));
                }
                NewsForShow newsshow = new NewsForShow()
                {
                    author_name = news.author_name,
                    author_id   = news.author_id,
                    content     = news.content,
                    news_id     = news.news_id,
                    part        = news.part,
                    post_date   = news.post_date.ToString(),
                    title       = news.title
                };
                RestfulResult.RestfulData <NewsForShow> rr = new RestfulResult.RestfulData <NewsForShow>();
                rr.code    = 1;
                rr.message = "成功查找新闻";
                rr.data    = newsshow;
                return(new JsonResult(rr));
            }
            catch (Exception exc)
            {
                RestfulResult.RestfulData rr = new RestfulResult.RestfulData(0, exc.Message);
                return(new JsonResult(rr));
            }
        }