Example #1
0
        public IActionResult Get(string keyword)
        {
            try
            {
                //如果keyword为空,不做要求
                if (keyword == null)
                {
                    keyword = "";
                }

                var pediaList = from pedia in _context.Encyclopedia
                                where KeywordSearch.ContainsKeywords(pedia.title + " " + pedia.content, keyword)
                                orderby pedia.post_date descending
                                select pedia;

                if (pediaList.Count() == 0)
                {
                    throw (new Exception("没有找到满足条件的数据"));
                }
                List <EncyclopediaForShow> showList = new List <EncyclopediaForShow>();
                string contract_content             = new string("");
                foreach (var nwsrow in pediaList)
                {
                    if (nwsrow.content.Count() > 50)
                    {
                        contract_content = nwsrow.content.Substring(0, 50) + "...";
                    }
                    else
                    {
                        contract_content = nwsrow.content;
                    }
                    EncyclopediaForShow temp = new EncyclopediaForShow()
                    {
                        author_name = nwsrow.author_name,
                        author_id   = nwsrow.author_id,
                        content     = contract_content,
                        ID          = nwsrow.ID,
                        part        = nwsrow.part,
                        post_date   = nwsrow.post_date.ToString(),
                        title       = nwsrow.title
                    };
                    showList.Add(temp);
                }

                var rr = new RestfulResult.RestfulArray <EncyclopediaForShow>();
                rr.code    = 1;
                rr.message = "成功查询到结果";
                rr.data    = showList.ToArray();
                return(new JsonResult(rr));
            }
            catch (Exception exc)
            {
                RestfulResult.RestfulData rr = new RestfulResult.RestfulData(0, exc.Message);
                return(new JsonResult(rr));
            }
        }
Example #2
0
        public async Task <IActionResult> GetOne(string ID)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw (new Exception("Bad Request,ModelState:" + ModelState.ToString()));
                }

                var pedia = await _context.Encyclopedia.FindAsync(ID);

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