public IActionResult Pager(int page) { ViewBag.ControllerName = "Root"; var cacheKey = $"Root_Pager_{page}"; string cached; PostsResult result = null; if (_memoryCache.TryGetValue(cacheKey, out cached)) { result = JsonConvert.DeserializeObject <PostsResult>(cached); } if (result == null) { result = _repo.GetPosts(_appSettings.Value.PostPerPage, page); if (result != null) { cached = JsonConvert.SerializeObject(result); _memoryCache.Set(cacheKey, cached, new MemoryCacheEntryOptions() { SlidingExpiration = TimeSpan.FromMinutes(5) }); } } return(View("_List", result)); }
public PostsResult GetPostsByCategory(string categoryId, int pageSize, int page) { //var list = _ctx.PostsInCategories.Include(m => m.Posts).Include(m => m.Posts.Author).Include(m => m.Posts.Comments) // .Where(m => m.CategoriesId == categoryId && m.Posts.IsPublished).Select(m => m.Posts); var list = _ctx.Posts.Include(m => m.Author).Include(m => m.PostsInCategories).Where(m => m.PostsInCategories.Any(n => n.CategoriesId == categoryId) && m.IsPublished); var totalCount = list.Count(); var posts = list.ToArray().OrderByDescending(o => o.DatePublished).Skip((page - 1) * pageSize).Take(pageSize); var category = _ctx.Categories.Where(m => m.Id == categoryId).FirstOrDefault(); var postlist = new List <PostItem>(); foreach (var item in posts) { postlist.Add(_jsonService.GetPost(item)); } var result = new PostsResult() { CurrentPage = page, TotalResults = totalCount, TotalPages = CalculatePages(totalCount, pageSize), Posts = postlist, Category = category.Title }; return(result); }
public PostsResult GetPostsByTag(string tag, int pageSize, int page) { var lowerTag = tag.ToLowerInvariant(); var totalCount = _ctx.Posts .Where(s => s.IsPublished && s.Tags.ToLower().Contains(lowerTag)) // Limiting the search for perf .ToArray() .Where(s => s.Tags.ToLower().Split(',').Contains(lowerTag)).Count(); var posts = _ctx.Posts.Include(m => m.Author).Include(m => m.Comments) .Where(s => s.IsPublished && s.Tags.ToLower().Contains(lowerTag)) .ToArray() .Where(s => s.Tags.ToLower().Split(',').Contains(lowerTag)) .OrderByDescending(o => o.DatePublished) .Skip((page - 1) * pageSize).Take(pageSize); var postlist = new List <PostItem>(); foreach (var item in posts) { postlist.Add(_jsonService.GetPost(item)); } var result = new PostsResult() { CurrentPage = page, TotalResults = totalCount, TotalPages = CalculatePages(totalCount, pageSize), Posts = postlist }; return(result); }
public PostsResult GetPostsByTerm(string term, int pageSize, int page) { var lowerTerm = term.ToLowerInvariant(); var totalCount = _ctx.Posts.Where(s => s.IsPublished && (s.Content.ToLowerInvariant().Contains(lowerTerm) || s.Tags.ToLowerInvariant().Contains(lowerTerm) || s.Title.ToLowerInvariant().Contains(lowerTerm)) ).Count(); var posts = _ctx.Posts.Include(m => m.Author).Include(m => m.Comments) .Where(s => s.IsPublished && (s.Content.ToLowerInvariant().Contains(lowerTerm) || s.Tags.ToLowerInvariant().Contains(lowerTerm) || s.Title.ToLowerInvariant().Contains(lowerTerm))) .OrderByDescending(o => o.DatePublished) .Skip((page - 1) * pageSize).Take(pageSize); var postlist = new List <PostItem>(); foreach (var item in posts) { postlist.Add(_jsonService.GetPost(item)); } var result = new PostsResult() { CurrentPage = page, TotalResults = totalCount, TotalPages = CalculatePages(totalCount, pageSize), Posts = postlist }; return(result); }
public IActionResult Pager(string id, int page) { var cacheKey = $"Categor_Index_{id.ToString()}_{page}"; string cached; PostsResult result = null; if (!_memoryCache.TryGetValue(cacheKey, out cached)) { result = _postsRepository.GetPostsByCategory(id, _appSettings.Value.PostPerPage, page); if (result != null) { cached = JsonConvert.SerializeObject(result); _memoryCache.Set(cacheKey, cached, new MemoryCacheEntryOptions() { SlidingExpiration = TimeSpan.FromMinutes(5) }); } } else { try { result = JsonConvert.DeserializeObject <PostsResult>(cached); } catch { result = _postsRepository.GetPostsByCategory(id, _appSettings.Value.PostPerPage, page); } } ViewBag.ControllerName = "category"; ViewBag.Id = id.ToString(); ViewBag.Title = $"{result.Category}"; return(View("_List", result)); }
public PostsResult GetPosts(int pageSize = 16, int page = 1, string authorId = null) { var count = 0; // Fix random SQL Errors due to bad offset if (page < 1) { page = 1; } if (pageSize > 100) { pageSize = 100; } List <Post> posts = null; if (string.IsNullOrEmpty(authorId)) { posts = _ctx.Posts.Include(m => m.Author).Include(m => m.Comments) .Where(s => s.IsPublished) .OrderByDescending(s => s.DatePublished) .Skip(pageSize * (page - 1)) .Take(pageSize) .ToList(); count = _ctx.Posts.Where(m => m.IsPublished).Count(); } else { posts = _ctx.Posts.Include(m => m.Author).Include(m => m.Comments) .Where(s => s.IsPublished && (s.Author.Id == authorId)) .OrderByDescending(s => s.DatePublished) .Skip(pageSize * (page - 1)) .Take(pageSize) .ToList(); count = _ctx.Posts.Include(m => m.Author).Where(s => s.IsPublished && (s.Author.Id == authorId)).Count(); } var postlist = new List <PostItem>(); foreach (var item in posts) { postlist.Add(_jsonService.GetPost(item)); } List <PostItem> recommendlist = null; if (page == 1) { var temp = _ctx.Posts.Where(m => m.HasRecommendEnabled).OrderByDescending(m => m.DatePublished).Take(3).ToList(); if (temp != null && temp.Count > 0) { recommendlist = new List <PostItem>(); foreach (var item in temp) { recommendlist.Add(_jsonService.GetPost(item)); } } } var result = new PostsResult() { CurrentPage = page, TotalResults = count, TotalPages = CalculatePages(count, pageSize), RecommendPosts = recommendlist, Posts = postlist, }; return(result); }