Skip to content

Instantly share code, notes, and snippets.

@nul800sebastiaan
Created October 5, 2021 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nul800sebastiaan/5b249ee1a7cd1b104ee991614d028eb5 to your computer and use it in GitHub Desktop.
Save nul800sebastiaan/5b249ee1a7cd1b104ee991614d028eb5 to your computer and use it in GitHub Desktop.
BlogOverviewController.cs
using System;
using System.Linq;
using Cultiv.Site.Models;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Web.Common.Controllers;
using Umbraco.Cms.Web.Common.PublishedModels;
using Umbraco.Cms.Web.Website.Controllers;
namespace Cultiv.Site.Controllers
{
public class BlogOverviewController : SurfaceController, IRenderController
{
private readonly IVariationContextAccessor _variationContextAccessor;
private readonly ServiceContext _serviceContext;
public BlogOverviewController(IUmbracoContextAccessor umbracoContextAccessor,
IUmbracoDatabaseFactory databaseFactory,
ServiceContext services,
AppCaches appCaches,
IProfilingLogger profilingLogger,
IPublishedUrlProvider publishedUrlProvider,
IVariationContextAccessor variationContextAccessor) :
base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
{
_variationContextAccessor = variationContextAccessor;
_serviceContext = services;
}
[HttpGet]
public IActionResult Index([FromQuery(Name = "page")] int page = 1)
{
var publishedValueFallback = new PublishedValueFallback(_serviceContext, _variationContextAccessor);
var model = new BlogOverviewModel(CurrentPage, publishedValueFallback) { Page = page };
var allBlogPosts = model.Children.ToList();
var skip = 10 * model.Page - 10;
model.TotalPages = Convert.ToInt32(Math.Ceiling(allBlogPosts.Count / 10.0));
model.PreviousPage = model.Page - 1;
model.NextPage = model.Page + 1;
model.IsFirstPage = model.Page <= 1;
model.IsLastPage = model.Page >= model.TotalPages;
var selection = allBlogPosts.OrderByDescending(x => x.CreateDate)
.Skip(skip)
.Take(10)
.Select(publishedContent => publishedContent as BlogPost)
.ToList();
model.PagedBlogPosts = selection;
return View("BlogOverview", model);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment