public BlogResult GetStoriesByTerm(string term, int pageSize, int page)
        {
            var lowerTerm  = term.ToLowerInvariant();
            var totalCount = _ctx.Stories.Where(s =>
                                                s.IsPublished &&
                                                (s.Body.ToLowerInvariant().Contains(lowerTerm) ||
                                                 s.Categories.ToLowerInvariant().Contains(lowerTerm) ||
                                                 s.Title.ToLowerInvariant().Contains(lowerTerm))
                                                ).Count();

            var result = new BlogResult()
            {
                CurrentPage  = page,
                TotalResults = totalCount,
                TotalPages   = CalculatePages(totalCount, pageSize),
                Stories      = _ctx.Stories
                               .Where(s => s.IsPublished && (s.Body.ToLowerInvariant().Contains(lowerTerm) ||
                                                             s.Categories.ToLowerInvariant().Contains(lowerTerm) ||
                                                             s.Title.ToLowerInvariant().Contains(lowerTerm)))
                               .OrderByDescending(o => o.DatePublished)
                               .Skip((page - 1) * pageSize).Take(pageSize)
            };

            return(FixResults(result));
        }
        public BlogResult GetStories(int pageSize = 25, int page = 1)
        {
            var count = _ctx.Stories.Count();

            // Fix random SQL Errors due to bad offset
            if (page < 1)
            {
                page = 1;
            }
            if (pageSize > 100)
            {
                pageSize = 100;
            }

            var result = new BlogResult()
            {
                CurrentPage  = page,
                TotalResults = count,
                TotalPages   = CalculatePages(count, pageSize),
                Stories      = _ctx.Stories
                               .Where(s => s.IsPublished)
                               .OrderByDescending(s => s.DatePublished)
                               .Skip(pageSize * (page - 1))
                               .Take(pageSize)
                               .ToList(),
            };

            return(FixResults(result));
        }
        private BlogResult FixResults(BlogResult result)
        {
            foreach (var s in result.Stories)
            {
                FixStory(s);
            }

            return(result);
        }
示例#4
0
 public void OnGet()
 {
     if (Resultpage > 0)
     {
         SearchResults = _blogRepo.GetPublishedPostsByTerm(Term, 10, Resultpage);
     }
     else
     {
         SearchResults = _blogRepo.GetPublishedPostsByTerm(Term);
     }
 }
        public async Task <IActionResult> Pager(string term, int page = 1)
        {
            ViewBag.Term = term;
            var results = new BlogResult();

            try
            {
                results = await _repo.GetStoriesByTerm(term, 15, page);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to get search results: {term} - {ex}");
            }

            return(View("Index", results));
        }
        public BlogResult GetStoriesByTag(string tag, int pageSize, int page)
        {
            var lowerTag   = tag.ToLowerInvariant();
            var totalCount = _ctx.Stories
                             .Where(s => s.IsPublished && s.Categories.ToLower().Contains(lowerTag)) // Limiting the search for perf
                             .ToArray()
                             .Where(s => s.Categories.ToLower().Split(',').Contains(lowerTag)).Count();

            var result = new BlogResult()
            {
                CurrentPage  = page,
                TotalResults = totalCount,
                TotalPages   = CalculatePages(totalCount, pageSize),
                Stories      = _ctx.Stories
                               .Where(s => s.IsPublished && s.Categories.ToLower().Contains(lowerTag))
                               .ToArray()
                               .Where(s => s.Categories.ToLower().Split(',').Contains(lowerTag))
                               .Skip((page - 1) * pageSize).Take(pageSize)
            };

            return(FixResults(result));
        }