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));
        }
        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.Where(s => s.Categories.ToLower().Split(',').Contains(lowerTag)).Count();

            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);
        }
        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));
        }