public async Task <IActionResult> Articles(int page = 1, string categoryName = "") { var posts = await this.blog.All(page, PostsPerPage, categoryName); var categories = await this.blog.Categories(); foreach (var post in posts) { post.Text = post.Text.RemoveHtmlTags(); post.Text = post.Text.Substring(0, Math.Min(post.Text.Length, 500)); } var model = new BlogListingViewModel { Posts = posts, Categories = categories, Navigation = new NavigationViewModel { TotalCount = await this.blog.TotalCount(), PerPage = PostsPerPage, CurrentPage = page } }; return(View(model)); }
public async Task <IActionResult> All(int page = 1) { var model = new BlogListingViewModel { Articles = await this.blogService.AllArtilesAsync(page), TotalArticles = await this.blogService.TotalAsync(), CurrentPage = page }; return(View(model)); }
public ActionResult BlogListing(AgilityContentItem module) { HttpContext.Response.Cache.VaryByParams["month"] = true; HttpContext.Response.Cache.VaryByParams[BlogContext.PagingQueryStringKey] = true; /* * check querystrings for filtering by "month" * check module for any specific filters being set (by category(s) or by tag(s)) * check current dynamic page item and identify if its a category or tag, if so override any previous filter * get all blog posts by passing in filter and paging logic * create a viewmodel that contains posts, the module, and blog configuration * return partial view in inline code or local files (dependant on setting in BlogUtils) */ #region --- Get Blog Config and Repos --- var blogConfigRef = module["BlogConfiguration"] as string; AgilityContentItem blogConfig = null; IAgilityContentRepository <AgilityContentItem> blogPosts = null; IAgilityContentRepository <AgilityContentItem> blogCategories = null; IAgilityContentRepository <AgilityContentItem> blogTags = null; IAgilityContentRepository <AgilityContentItem> blogAuthors = null; BlogUtils.GetBlogConfig(blogConfigRef, out blogConfig, out blogPosts, out blogCategories, out blogTags, out blogAuthors); #endregion #region --- Determine Filters --- DateTime filterMonth = DateTime.MinValue; var categories = new List <AgilityContentItem>(); var tags = new List <AgilityContentItem>(); //check for month string monthQS = Request.QueryString["month"] as string; //look for month=05-2015 if (!string.IsNullOrEmpty(monthQS)) { try { filterMonth = DateTime.ParseExact(monthQS, "MM-yyyy", CultureInfo.InvariantCulture); } catch (FormatException ex) { Agility.Web.Tracing.WebTrace.WriteInfoLine(ex.ToString()); } } //check for dynamic item var dynamicItem = ResolveBlogCategory(); var node = SiteMap.CurrentNode; if (dynamicItem != null) { if (dynamicItem.IsBlogTag()) { tags.Add(dynamicItem); } else if (dynamicItem.IsBlogCategory()) { categories.Add(dynamicItem); } else { //this is on some other dynamic page item, ignore it dynamicItem = null; } } //skip module settings if we have a dynamic item set, else check if (dynamicItem == null) { string categoryIDs = module["FilterByCategoriesIDs"] as string; string tagsIDs = module["FilterByTagsIDs"] as string; if (!string.IsNullOrEmpty(categoryIDs)) { var categoriesFromModule = blogCategories.GetByIDs(categoryIDs); if (categoriesFromModule != null && categoriesFromModule.Any()) { categories.AddRange(categoriesFromModule); } } if (!string.IsNullOrEmpty(tagsIDs)) { var tagsFromModule = blogTags.GetByIDs(tagsIDs); if (tagsFromModule != null && tagsFromModule.Any()) { tags.AddRange(tagsFromModule); } } } string dateFilter = ""; if (filterMonth != DateTime.MinValue) { dateFilter = BlogUtils.GetSqlBetweenMonthRangeStatement("Date", filterMonth); } #endregion --- End Determine Filters --- #region --- Get Posts and set Pagination --- int pageSize = 10; int.TryParse(string.Format("{0}", module["PageSize"]), out pageSize); if (pageSize < 1) { pageSize = 10; } int pageNumber = 1; int.TryParse(Request.QueryString[BlogContext.PagingQueryStringKey] as string, out pageNumber); if (pageNumber < 1) { pageNumber = 1; } int skip = (pageNumber * pageSize) - pageSize; int totalCount = 0; //get the posts List <AgilityContentItem> posts = BlogUtils.GetPosts(blogPosts, null, null, categories, tags, dateFilter, "Date Desc", pageSize, skip, out totalCount); var pagination = new BlogPaginationViewModel(pageNumber, pageSize, totalCount, true, 5, BlogContext.PagingQueryStringKey, "agility-active", "agility-disabled"); #endregion #region --- Set Title --- //cannot set Page Title from child controller action and no way of knowing how the website layout is setting the title... //best we can do is utilize the Page Title from the Dynamic Page settings and set the <h1> on the module string title = module["DefaultTitle"] as string; if (dynamicItem != null) { title = dynamicItem["Title"] as string; } if (filterMonth != DateTime.MinValue) { if (!string.IsNullOrEmpty(title)) { title += ": "; } title += filterMonth.ToString("MMMM yyyy"); } #endregion #region --- Set ViewModel --- var model = new BlogListingViewModel(); model.Module = module; model.Posts = posts; model.Title = title; model.Configuration = blogConfig; model.CurrentCategory = categories.FirstOrDefault(); model.Pagination = pagination; #endregion return(PartialView(AgilityComponents.TemplatePath("Blog-ListingModule"), model)); }