/// <summary> /// Gets the archive for the category with the given slug. /// </summary> /// <param name="slug">The unique slug</param> /// <param name="page">Optional page number</param> /// <returns>The archive</returns> public async Task<Models.CategoryArchive> GetCategoryArchiveAsync(string slug, int page = 1) { var model = new Models.CategoryArchive(); var categoryRep = new CategoryRepository(uow); var postRep = new PostRepository(uow); model.Category = await categoryRep.GetBySlugAsync(slug); if (model.Category != null) { // Fill model model.TotalCount = await uow.Posts.Where(p => p.Published.HasValue && p.Categories.Any(c => c.Slug == slug)).CountAsync(); model.PageSize = Config.Blog.ArchivePageSize; model.PageCount = Math.Max(Convert.ToInt32(Math.Ceiling(((double)model.TotalCount) / model.PageSize)), 1); model.CurrentPage = Math.Min(page, model.PageCount); model.Posts = await postRep.GetAsync(p => p.Published.HasValue && p.Categories.Any(c => c.Slug == slug), model.CurrentPage * model.PageSize); // Filter out the current page model.Posts = model.Posts.Subset((model.CurrentPage - 1) * model.PageSize).ToList(); return model; } return null; }