public async Task <IViewComponentResult> InvokeAsync(CategoryRequestViewModel category) { var response = await _postService.GetRecentPostsAsync(category); if (response.IsSuccess) { return(View(response.Item.ToList())); } ViewBag.ComponentErrorMessage = response.Message; return(View(new List <RecentPostViewModel>())); }
public async Task <IActionResult> GetPostsByCategory(string url, string code, int page = 1, [FromServices] IMemoryCache memoryCache = null) { var pageView = new PageViewModel(commonFactory.BlogConfig, commonFactory.HttpContextAccessor, commonFactory.AesEncryption); if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(code)) { return(View("NotFound", pageView)); } if (page < 1) { page = 1; } var request = new CategoryRequestViewModel { Url = url, Code = code }; var category = memoryCache.GetOrCreate($"{StaticCacheKeys.CategoryName}-{code}", entry => repositoryFactory.Category.GetAsync(url, code).Result); if (category != null) { pageView.Title = new CategoryTitleViewModel { Name = category.DisplayName, Note = category.Note }; ViewData["Title"] = category.DisplayName; } var result = await repositoryFactory.Post.GetPostsByCategoryAsync(request, CodeMazeConfiguration.PageSize, page); if (!result.IsSuccess) { return(View("NotFound", pageView)); } var postList = result.Item; if (postList?.Count > 0) { var token = postList.Count == CodeMazeConfiguration.PageSize ? commonFactory.AesEncryption.Encrypt((page + 1).ToString()) : string.Empty; int postCount = memoryCache.GetOrCreate($"{StaticCacheKeys.PostCount}-{code}", entry => repositoryFactory.Post.CountVisiblePosts(url, code)); var postsAsIPagedList = new StaticPagedList <PostListItem>(postList, page, CodeMazeConfiguration.PageSize, postCount); pageView.Items = postsAsIPagedList; pageView.Token = token; } return(View(pageView)); }
public Task <Response <List <RecentPostViewModel> > > GetRecentPostsAsync(CategoryRequestViewModel category, int top = 5) { return(TryExecuteAsync <List <RecentPostViewModel> >(async() => { IQueryable <PostEntity> postQuery; if (category is null) { postQuery = _postRepository.GetAsQueryable() .Include(post => post.PostExtension) .Where(post => post.IsPublished && post.PubDateUtc.HasValue && !post.IsDeleted ); } else { postQuery = _postRepository.GetAsQueryable() .Include(post => post.PostExtension).Where(post => post.PostCategory.Any(cat => cat.Category.Url.Equals(category.Url) && cat.Category.Code.Equals(category.Code) ) && post.IsPublished && post.PubDateUtc.HasValue && !post.IsDeleted); } var posts = await postQuery.OrderByDescending(p => p.PostExtension.Views) .Skip(0) .Take(top) .Select(post => _mapper.Map <RecentPostViewModel>(post)) .ToListAsync(); return new SuccessResponse <List <RecentPostViewModel> >(posts); })); }
public Task <Response <IReadOnlyList <PostListItem> > > GetPostsByCategoryAsync(CategoryRequestViewModel category, int pageSize = 20, int pageIndex = 1) { return(TryExecuteAsync <IReadOnlyList <PostListItem> >(async() => { if (category is null) { throw new ArgumentNullException("Could not found category because url or code null"); } var posts = await _postRepository.GetAsQueryable() .Include(post => post.PostCategory).ThenInclude(category => category.Category) .Include(post => post.PostTag).ThenInclude(tag => tag.Tag) .Include(post => post.PostExtension) .Where(p => p.PostCategory.Any(c => c.Category.Url.Equals(category.Url) && c.Category.Code.Equals(category.Code)) && p.IsPublished && !p.IsDeleted) .OrderByDescending(post => post.CreateOnUtc) .Skip((pageIndex - 1) * pageSize) .Take(pageSize) .Select(post => _mapper.Map <PostListItem>(post)) .ToListAsync(); return new SuccessResponse <IReadOnlyList <PostListItem> >(posts); })); }