public async Task <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      = await _ctx.Stories
                               .Where(s => s.IsPublished)
                               .OrderByDescending(s => s.DatePublished)
                               .Skip(pageSize * (page - 1))
                               .Take(pageSize)
                               .ToListAsync(),
            };

            return(FixResults(result));
        }
        public async Task <BlogResult> GetStoriesByTerm(string term, int pageSize, int page)
        {
            var lowerTerm  = term.ToLower();
            var totalCount = await _ctx.Stories.Where(s =>
                                                      s.IsPublished &&
                                                      (s.Body.ToLower().Contains(lowerTerm) ||
                                                       s.Categories.ToLower().Contains(lowerTerm) ||
                                                       s.Title.ToLower().Contains(lowerTerm))
                                                      ).CountAsync();

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

            return(FixResults(result));
        }
        public async Task <BlogResult> GetStoriesByTag(string tag, int pageSize, int page)
        {
            var lowerTag  = tag.ToLowerInvariant();
            var subResult = await _ctx.Stories
                            .Where(s => s.IsPublished && s.Categories.ToLower().Contains(lowerTag)) // Limiting the search for perf
                            .ToArrayAsync();

            var totalCount = subResult.Count(s => s.Categories.ToLower().Split(',').Contains(lowerTag));

            var stories = await _ctx.Stories
                          .Where(s => s.IsPublished && s.Categories.ToLower().Contains(lowerTag))
                          .ToArrayAsync();

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

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

            return(result);
        }