/// <summary> /// 按关键字搜索 /// </summary> /// <param name="keyword"></param> /// <param name="pageIndex"></param> /// <returns></returns> public async Task <SearchResult> SearchAsync(string keyword, int pageIndex = 1) { SearchPost searchPost = new SearchPost() { Keyword = keyword, PageIndex = pageIndex }; var searchResult = await Post <SearchPost, SearchResult>(ServiceUri.Search, searchPost); return(searchResult); }
public ActionResult Search(SearchPost searchParams) { List <PostTitle> postsToReturn = new List <PostTitle>(); if (string.IsNullOrWhiteSpace(searchParams.Username) && string.IsNullOrWhiteSpace(searchParams.SubTitle) && string.IsNullOrWhiteSpace(searchParams.Title)) { postsToReturn.AddRange(db.Posts.OrderByDescending(p => p.CreationDate).Select(post => new PostTitle { Title = post.Title, SubTitle = post.SubTitle, CreationDate = post.CreationDate, UserEmail = post.Author.Email, PostTitleId = post.Id }).ToList()); } else { Func <Post, bool> usernamePredicate = (Post p) => { return(true); }; Func <Post, bool> titlePredicate = (Post p) => { return(true); }; Func <Post, bool> subTitlePredicate = (Post p) => { return(true); }; if (!string.IsNullOrWhiteSpace(searchParams.Username)) { usernamePredicate = (Post p) => p.Author.Email.Contains(searchParams.Username); } if (!string.IsNullOrWhiteSpace(searchParams.Title)) { titlePredicate = (Post p) => p.Title.Contains(searchParams.Title); } if (!string.IsNullOrWhiteSpace(searchParams.SubTitle)) { subTitlePredicate = (Post p) => p.SubTitle.Contains(searchParams.SubTitle); } postsToReturn.AddRange(db.Posts.Include(p => p.Author) .Where(usernamePredicate) .Where(titlePredicate) .Where(subTitlePredicate) .OrderByDescending(p => p.CreationDate).Select(post => new PostTitle { Title = post.Title, SubTitle = post.SubTitle, CreationDate = post.CreationDate, UserEmail = post.Author.Email, PostTitleId = post.Id }).ToList()); } return(View("Index", postsToReturn)); }
public ActionResult Search(string search) { var skip = (1 - 1) * 5; var model = new MainPageViewModel(); var otherPosts = _postRepository.GetAll(); var posts = SearchPost.Search(search); var societyPosts = posts.Where(p => p.CategoryID == 6); var politicPosts = posts.Where(p => p.CategoryID == 1); var sportNEWS = posts.Where(p => p.CategoryID == 5); var galleryNEWS = _postRepository.GetPostsHasGallery(); var bestAuthors = new NEWSAuthor().Authors(); var categories = _categoryRepository.GetAll(); societyPosts.Shuffle(); politicPosts.Shuffle(); sportNEWS.Shuffle(); posts.Shuffle(); otherPosts.Shuffle(); var newsList = (from post in posts where post.CategoryID != null select new NEWSPost { PostID = post.ID, Title = post.Title, ShortDescription = post.ShortDescription, ImageUrl = post.MainImageUrl, CreatePost = post.CreateDate, IsGallery = post.IsGallery, CommentCount = _commentRepository.GetCommentsByPost((long)post.ID).Count(), CategoryID = post.CategoryID, CategoryName = _categoryRepository.GetByID((long)post.CategoryID).Name }).ToList(); var othernewsList = (from post in otherPosts where post.CategoryID != null select new NEWSPost { PostID = post.ID, Title = post.Title, ShortDescription = post.ShortDescription, ImageUrl = post.MainImageUrl, CreatePost = post.CreateDate, IsGallery = post.IsGallery, CommentCount = _commentRepository.GetCommentsByPost((long)post.ID).Count(), CategoryID = post.CategoryID, CategoryName = _categoryRepository.GetByID((long)post.CategoryID).Name }).ToList(); model.TrendNEWS = othernewsList .Skip(skip) .Take(5) .ToArray(); model.RecomendedNEWS = othernewsList .Skip(5) .Take(5) .ToArray(); model.LastNEWS = newsList .OrderByDescending(p => p.CreatePost) .Skip(0) .Take(20) .ToArray(); model.GalleryNEWS = (from post in galleryNEWS where post.CategoryID != null select new NEWSPost { PostID = post.ID, Title = post.Title, ShortDescription = post.ShortDescription, ImageUrl = post.MainImageUrl, CreatePost = post.CreateDate, IsGallery = post.IsGallery, CommentCount = _commentRepository.GetCommentsByPost((long)post.ID).Count(), CategoryID = post.CategoryID, CategoryName = _categoryRepository.GetByID((long)post.CategoryID).Name }) .Skip(skip) .Take(5) .ToList(); model.BestAuthors = bestAuthors; model.CategoryNEWS = categories; model.CategoryName = search; return(View(model)); }