Skip to content

Commit

Permalink
Added Typed PageIdentity with "Data" property to access the typed fie…
Browse files Browse the repository at this point in the history
…lds.
  • Loading branch information
KenticoDevTrev committed Jan 24, 2022
1 parent 6fe6afc commit a3e228b
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 58 deletions.
18 changes: 0 additions & 18 deletions MVC/MVC.Libraries/Libraries/AutoMapperMaps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,6 @@ public void Process(TreeNode source, SitemapNode destination, ResolutionContext
}
}

public class PageIdentityMapping : IMappingAction<TreeNode, PageIdentity>
{
private readonly IPageUrlRetriever _pageUrlRetriever;

public PageIdentityMapping(IPageUrlRetriever pageUrlRetriever)
{
_pageUrlRetriever = pageUrlRetriever;
}

public void Process(TreeNode source, PageIdentity destination, ResolutionContext context)
{
var url = _pageUrlRetriever.Retrieve(source);
destination.RelativeUrl = url.RelativePath;
destination.AbsoluteUrl = url.AbsoluteUrl;
}
}



public class SearchItemMapping : IMappingAction<SearchResultItem, SearchItem>
{
Expand Down
57 changes: 36 additions & 21 deletions MVC/MVC.Libraries/Libraries/Extensions/TreeNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Generic.Models;
using Kentico.Content.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Generic.Libraries.Extensions
Expand All @@ -18,25 +17,14 @@ public static class TreeNodeExtensions
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public static PageIdentity ToPageIdentity(this TreeNode node)
public static PageIdentity<TPage> ToPageIdentity<TPage>(this TPage node) where TPage : TreeNode
{
var pageIdentity = new PageIdentity()
{
NodeID = node.NodeID,
NodeGUID = node.NodeGUID,
DocumentID = node.DocumentID,
DocumentGUID = node.DocumentGUID,
Path = node.NodeAliasPath,
Alias = node.NodeAlias,
Name = node.DocumentName,
NodeLevel = node.NodeLevel
};

// Get Urls
var relativeAndAbsoluteUrl = CacheHelper.Cache(cs =>
{
if (cs.Cached)
{
cs.CacheDependency = CacheHelper.GetCacheDependency($"documentid|{pageIdentity.DocumentID}");
cs.CacheDependency = CacheHelper.GetCacheDependency($"documentid|{node.DocumentID}");
}
try
{
Expand All @@ -51,7 +39,7 @@ public static PageIdentity ToPageIdentity(this TreeNode node)
{
// Will need to re-query the page, must be missing columns
}
if (pageIdentity.DocumentID > 0)
if (node.DocumentID > 0)
{
// get full page
var fullNode = CacheHelper.Cache(cs =>
Expand All @@ -60,15 +48,15 @@ public static PageIdentity ToPageIdentity(this TreeNode node)
{
cs.CacheDependency = CacheHelper.GetCacheDependency(new string[]
{
$"documentid{ pageIdentity.DocumentID }"
$"documentid{ node.DocumentID }"
});
}
return new DocumentQuery()
.WhereEquals(nameof(TreeNode.DocumentID), node.DocumentID)
.EnsureUrls()
.GetEnumerableTypedResult()
.FirstOrDefault();
}, new CacheSettings(10, "GetDocumentForUrlRetrieval", pageIdentity.DocumentID));
}, new CacheSettings(10, "GetDocumentForUrlRetrieval", node.DocumentID));

string url = DocumentURLProvider.GetUrl(fullNode);
return new Tuple<string, string>(url.Replace("~", ""), GetAbsoluteUrlOptimized(url, fullNode.NodeSiteID, fullNode.DocumentCulture, true));
Expand All @@ -78,11 +66,27 @@ public static PageIdentity ToPageIdentity(this TreeNode node)
return new Tuple<string, string>(string.Empty, string.Empty);
}
}, new CacheSettings(10, "GetNodeUrlsForPageIdentity", node.DocumentID));
pageIdentity.RelativeUrl = relativeAndAbsoluteUrl.Item1;
pageIdentity.AbsoluteUrl = relativeAndAbsoluteUrl.Item2;

var pageIdentity = new PageIdentity<TPage>()
{
NodeID = node.NodeID,
NodeGUID = node.NodeGUID,
DocumentID = node.DocumentID,
DocumentGUID = node.DocumentGUID,
Path = node.NodeAliasPath,
Alias = node.NodeAlias,
Name = node.DocumentName,
NodeLevel = node.NodeLevel,
RelativeUrl = relativeAndAbsoluteUrl.Item1,
AbsoluteUrl = relativeAndAbsoluteUrl.Item2,
Data = node
};

return pageIdentity;
}



/// <summary>
/// DocumentURLProvider.GetPresentationUrl() does various uncached database calls, this caches that to minimize calls for absolute url
/// </summary>
Expand Down Expand Up @@ -127,6 +131,17 @@ private static string GetAbsoluteUrlOptimized(string virtualPath, SiteInfoIdenti
return str;
}
}
}

// Kentico specific typed variation
namespace Generic.Models
{
public record PageIdentity<TData, TPage> : PageIdentity<TData> where TPage : TreeNode
{
public PageIdentity()
: base()
{

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
using MVCCaching.Base.Core.Interfaces;
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using CMS.Base.Internal;
using Kentico.Web.Mvc;
using Kentico.PageBuilder.Web.Mvc;
using Generic.Libraries.Extensions;
using CMS.DocumentEngine.Routing;

namespace Generic.Repositories.Implementations
{
Expand Down Expand Up @@ -48,7 +46,7 @@ public Task<PageIdentity> GetCurrentPageAsync()
if (_pageDataContextRetriever.TryRetrieve<TreeNode>(out var currentPage))
{
builder.Node(currentPage.Page.NodeID);
return Task.FromResult(currentPage.Page.ToPageIdentity());
return Task.FromResult((PageIdentity)currentPage.Page.ToPageIdentity());
}
else
{
Expand Down
37 changes: 37 additions & 0 deletions MVC/MVC.Libraries/Services/Implementations/PageIdentityFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using AutoMapper;
using Generic.Models;
using Generic.Services.Interfaces;

namespace Generic.Services.Implementations
{
public class PageIdentityFactory : IPageIdentityFactory
{
private readonly IMapper _mapper;

public PageIdentityFactory(IMapper mapper)
{
_mapper = mapper;
}

public PageIdentity<TData> Convert<TData, TOriginalData>(PageIdentity<TOriginalData> pageIdentity)
{
// Convert data using AutoMapper
TData data = (pageIdentity.Data != null ? _mapper.Map<TData>(pageIdentity.Data) : default);

return new PageIdentity<TData>()
{
Data = data,
DocumentGUID = pageIdentity.DocumentGUID,
DocumentID = pageIdentity.DocumentID,
NodeGUID = pageIdentity.NodeGUID,
NodeID = pageIdentity.NodeID,
NodeLevel = pageIdentity.NodeLevel,
AbsoluteUrl = pageIdentity.AbsoluteUrl,
RelativeUrl = pageIdentity.RelativeUrl,
Alias = pageIdentity.Alias,
Name = pageIdentity.Name,
Path = pageIdentity.Path
};
}
}
}
69 changes: 58 additions & 11 deletions MVC/MVC.Models/Models/PageIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,64 @@

namespace Generic.Models
{
public class PageIdentity
public record PageIdentity
{
public string Name { get; set; }
public string Alias { get; set; }
public int NodeID { get; set; }
public Guid NodeGUID { get; set; }
public int DocumentID { get; set; }
public Guid DocumentGUID { get; set; }
public string Path { get; set; }
public string RelativeUrl { get; set; }
public string AbsoluteUrl { get; set; }
public int NodeLevel { get; set; }
/// <summary>
/// The Name of the page
/// </summary>
public string Name { get; init; }

/// <summary>
/// The Code Name of the page
/// </summary>
public string Alias { get; init; }

/// <summary>
/// The Page's int identity (culture agnostic)
/// </summary>
public int NodeID { get; init; }

/// <summary>
/// The Page's guid identity (culture agnostic)
/// </summary>
public Guid NodeGUID { get; init; }

/// <summary>
/// The Page's int identity (culture specific)
/// </summary>
public int DocumentID { get; init; }

/// <summary>
/// The Page's guid identity (culture specific)
/// </summary>
public Guid DocumentGUID { get; init; }

/// <summary>
/// The content path of the page
/// </summary>
public string Path { get; init; }

/// <summary>
/// Relative URL of the page
/// </summary>
public string RelativeUrl { get; init; }

/// <summary>
/// Absolute URL of the page
/// </summary>
public string AbsoluteUrl { get; init; }

/// <summary>
/// Nesting level of the page.
/// </summary>
public int NodeLevel { get; init; }
}

public record PageIdentity<T> : PageIdentity
{
/// <summary>
/// Typed page data
/// </summary>
public T Data { get; set; }
}
}
10 changes: 10 additions & 0 deletions MVC/MVC.Models/Services/Interfaces/IPageIdentityFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Generic.Models;
using MVCCaching;

namespace Generic.Services.Interfaces
{
public interface IPageIdentityFactory : IService
{
PageIdentity<TData> Convert<TData, TOriginalData>(PageIdentity<TOriginalData> pageIdentity);
}
}
15 changes: 14 additions & 1 deletion MVC/MVC/Features/TabParent/TabParentPageTemplate.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
@model ComponentViewModel<Generic.Features.TabParent.TabParentPageTemplateProperties>
@using CMS.DocumentEngine.Types.Generic;
@using Generic.Models;
@using Generic.Libraries.Extensions;
@inject IPageIdentityFactory pageIdentityFactory
@section head{
<vc:page-meta-data />
}
<vc:tab-parent page=@Model.Page.ToPageIdentity() />
@{
// This component requires the Page be specifically a TabParent
if (Model.Page is not TabParent tabParent)
{
return;
}
// Convert to identity with our TabParentItem model
var identity = pageIdentityFactory.Convert<TabParentItem, TabParent>(tabParent.ToPageIdentity());
}
<vc:tab-parent page="identity" />
10 changes: 6 additions & 4 deletions MVC/MVC/Features/TabParent/TabParentViewComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ public TabParentViewComponent(ITabRepository tabRepository)
_tabRepository = tabRepository;
}

public async Task<IViewComponentResult> InvokeAsync(PageIdentity page)
public async Task<IViewComponentResult> InvokeAsync(PageIdentity<TabParentItem> page)
{
var model = new TabParentViewModel();
if(page == null)
if (page == null)
{
model = new TabParentViewModel()
{
Name = "Unknown",
Tabs = new List<TabItem>()
};
} else {
model.Name = (await _tabRepository.GetTabParentAsync(page.NodeID)).Name;
}
else
{
model.Name = page.Data.Name;
model.Tabs = await _tabRepository.GetTabsAsync(page.Path);
}
return View("TabParent", model);
Expand Down

0 comments on commit a3e228b

Please sign in to comment.