public async Task <IEnumerable <PostEntity> > GetQueriedNews(PostSearchQuery query) { DateTime?dtStart = null; DateTime?dtEnd = null; query.Tag = query.Tag ?? string.Empty; if (query.Date.HasValue) { dtStart = new DateTime(query.Date.Value.Year, query.Date.Value.Month, query.Date.Value.Day); dtEnd = new DateTime(query.Date.Value.Year, query.Date.Value.Month, query.Date.Value.Day + 1); } IQueryable <PostEntity> posts = _newsContext.Posts .Include(post => post.Category) .Include(post => post.PostTags) .ThenInclude(post => post.Tag) .Where(post => (string.IsNullOrEmpty(query.CategoryUrl) || post.Category.Url == query.CategoryUrl) && (string.IsNullOrEmpty(query.Tag) && (string.IsNullOrEmpty(query.Query) || post.Description.ToLower().Contains(query.Query.ToLower()) || post.Title.ToLower().Contains(query.Query.ToLower()) || (post.PostTags != null ? post.PostTags.Select(posttag => posttag.Tag).Any(c => c.Name.ToLower().Contains(query.Query.ToLower())) : false)) || (!string.IsNullOrEmpty(query.Tag) && post.PostTags != null ? post.PostTags.Select(posttag => posttag.Tag.Name.ToLower()).Contains(query.Tag.ToLower()) : false)) && (!query.Date.HasValue || post.PublishedOn.HasValue && (post.PublishedOn.Value > dtStart && post.PublishedOn.Value < dtEnd)) && (post.IsPublished ?? true))//TODO change true to false .OrderByDescending(x => x.PublishedOn) .Skip((query.Page - 1) * query.Count) .Take(query.Count); return(await posts.ToListAsync()); }
public IActionResult SearchByTag(string tag, string categoryUrl) { ViewBag.CategoryName = _categoryService.GetCategoryByUrlAsync(categoryUrl).Result; var postQuery = new PostSearchQuery { Tag = tag, CategoryUrl = categoryUrl }; ViewBag.Query = postQuery; var searchResult = _searchService.SearchPosts(postQuery).Result.ToList(); return(View(searchResult)); }
public async Task <IActionResult> SearchPosts(int page, int count, string tag = null, string categoryUrl = null, string query = null, string date = null) { DateTime?dt = null; if (!string.IsNullOrEmpty(date)) { dt = DateTime.Parse(date); } PostSearchQuery postQuery = new PostSearchQuery { CategoryUrl = categoryUrl, Count = count, Date = dt, Page = page, Query = query, Tag = tag }; IEnumerable <PostPreview> posts = await _searchService.SearchPosts(postQuery); return(PartialView("~/Views/Search/_SearchResultTemplate.cshtml", posts)); }
public IActionResult Posts(int page = 1, int count = 10, string categoryUrl = null, string query = null, string date = null) { DateTime?dt = null; if (!String.IsNullOrEmpty(date)) { dt = DateTime.Parse(date); } var postQuery = new PostSearchQuery { CategoryUrl = categoryUrl, Count = count, Date = dt, Page = page, Query = query, }; ViewBag.CategoryName = _categoryService.GetCategoryByUrlAsync(categoryUrl).Result; ViewBag.Query = postQuery; var searchResult = _searchService.SearchPosts(postQuery).Result.ToList(); return(View("Index", searchResult)); }
public async Task <IEnumerable <PostPreview> > SearchPosts(PostSearchQuery query) { var posts = await _postRepository.GetQueriedNews(query); return(posts.Select(Mapper.Map <PostEntity, PostPreview>)); }