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)); } }
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); }
public IActionResult Get([FromBody] dynamic _in) { try { //搜索条件 string keyword = _in.keyword; bool isOnlyPost = _in.isOnlyPost; string user_id = _in.user_id; //如果keyword为空,不做要求 if (keyword == null) { keyword = ""; } //如果userid为空,不做要求 bool isId = true; if (user_id == "" || user_id == null) { isId = false; } //获得表单数据 var postList = from post in _context.Post_data join user in _context.user_data on post.user_id equals user.user_id where (KeywordSearch.ContainsKeywords(post.title + " " + post.content, keyword)) && ((!isId) || (post.user_id == user_id)) orderby post.post_date descending select new { post, user }; var replyList = from reply in _context.Reply_data join user in _context.user_data on reply.user_id equals user.user_id where (!isOnlyPost) & (KeywordSearch.ContainsKeywords(reply.content, keyword)) && ((!isId) || (reply.user_id == user_id)) orderby reply.reply_date descending select new { reply, user }; //没找到数据的异常 if (postList.Count() + replyList.Count() == 0) { throw (new Exception("没找到满足要求的贴")); } //将表单数据转化为展示数据 var showList = new List <PostOrReplyForShow>(); var contract_content = new string(""); foreach (var postRow in postList) { if (postRow.post.content.Count() > 50) { contract_content = postRow.post.content.Substring(0, 50) + "..."; } else { contract_content = postRow.post.content; } var temp = new PostOrReplyForShow { content = contract_content, date = postRow.post.post_date.ToString(), isPost = true, post_id = postRow.post.post_id, title = postRow.post.title, user_id = postRow.post.user_id, user_name = postRow.user.user_name }; showList.Add(temp); } foreach (var replyRow in replyList) { if (replyRow.reply.content.Count() > 50) { contract_content = replyRow.reply.content.Substring(0, 50) + "..."; } else { contract_content = replyRow.reply.content; } var title = _context.Post_data.Find(replyRow.reply.post_id).title; var temp = new PostOrReplyForShow { content = contract_content, date = replyRow.reply.reply_date.ToString(), isPost = true, post_id = replyRow.reply.post_id, title = title, user_id = replyRow.reply.user_id, user_name = replyRow.user.user_name }; showList.Add(temp); } //整理数据 showList.Sort((x, y) => { if (Convert.ToDateTime(x.date) > Convert.ToDateTime(x.date)) { return(-1); } else { return(1); } }); //返回正确结果 RestfulResult.RestfulArray <PostOrReplyForShow> rr = new RestfulResult.RestfulArray <PostOrReplyForShow>(); 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)); } }