//gets all post according to given detailed information. Used in detailed search public DetailedSearchViewModel GetAllDetailed(DetailedSearchViewModel detailedSearch) { DateTime?minDate; DateTime?maxDate; if (detailedSearch.DateOne.HasValue && detailedSearch.DateTwo.HasValue && detailedSearch.DateOne > detailedSearch.DateTwo) { minDate = detailedSearch.DateTwo; maxDate = detailedSearch.DateOne; } else { minDate = detailedSearch.DateOne; maxDate = detailedSearch.DateTwo; } var query = _postRepository.GetAll().Where(x => true); if (detailedSearch.Keyword != null) { query = query.Where(x => x.Title.Contains(detailedSearch.Keyword)); } if (minDate.HasValue || maxDate.HasValue) { if (minDate.HasValue && maxDate == null) { query = query.Where(x => x.CreateDate > minDate.Value); } else if (minDate == null && maxDate.HasValue) { query = query.Where(x => x.CreateDate < maxDate.Value); } else { query = query.Where(x => (x.CreateDate >= minDate.Value && x.CreateDate <= maxDate.Value)); } } //todo: this can be enum if (detailedSearch.SortType == 1) //new to old { query = query.OrderByDescending(x => x.CreateDate); detailedSearch.DetailedSearchPostResults = new List <PostViewModel>(); foreach (var item in query.ToList()) { var postResult = new PostViewModel(); postResult.Title = item.Title; postResult.UserId = item.UserId; postResult.ClickCount = item.ClickCount; postResult.CategoryId = item.CategoryId; postResult.Id = item.Id; detailedSearch.DetailedSearchPostResults.Add(postResult); } } if (detailedSearch.SortType == 2) //old to new { query = query.OrderBy(x => x.CreateDate); detailedSearch.DetailedSearchPostResults = new List <PostViewModel>(); foreach (var item in query.ToList()) { var postResult = new PostViewModel(); postResult.Title = item.Title; postResult.UserId = item.UserId; postResult.ClickCount = item.ClickCount; postResult.CategoryId = item.CategoryId; postResult.Id = item.Id; detailedSearch.DetailedSearchPostResults.Add(postResult); } } return(detailedSearch); }
public IActionResult DetailedSearch(DetailedSearchViewModel searchmodel = null) { searchmodel = _postService.GetAllDetailed(searchmodel); return(View(searchmodel)); }