Skip to content

Commit

Permalink
122 subpages (#127)
Browse files Browse the repository at this point in the history
* 122 introduce subpages element + bump nuget

* 122 introduce ManagementElementHelper

* 122 introduce ElementTypeHelper

* 122 subpages impl

* 122 little refactoring

* 122 little refactoring

* 122 add missing test case
  • Loading branch information
Sevitas authored Jan 13, 2022
1 parent 3f48775 commit a1b0278
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 186 deletions.
15 changes: 5 additions & 10 deletions src/Kentico.Kontent.ModelGenerator.Core/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ internal async Task<ICollection<ClassCodeGenerator>> GetClassCodeGenerators()
? managementTypes?.FirstOrDefault(managementType => managementType.Codename == contentType.System.Codename)
: null;

codeGenerators.Add(GetClassCodeGenerator(contentType, _options.StructuredModel, managementSnippets, managementContentType));
codeGenerators.Add(GetClassCodeGenerator(contentType, managementSnippets, managementContentType));
}
catch (InvalidIdentifierException)
{
Expand All @@ -127,23 +127,18 @@ internal async Task<ICollection<ClassCodeGenerator>> GetClassCodeGenerators()
return codeGenerators;
}

internal ClassCodeGenerator GetClassCodeGenerator(IContentType contentType, bool structuredModel, IEnumerable<ContentTypeSnippetModel> managementSnippets, ContentTypeModel managementContentType = null)
internal ClassCodeGenerator GetClassCodeGenerator(IContentType contentType, IEnumerable<ContentTypeSnippetModel> managementSnippets = null, ContentTypeModel managementContentType = null)
{
var classDefinition = new ClassDefinition(contentType.System.Codename);

foreach (var element in contentType.Elements.Values)
{
try
{
var elementType = element.Type;
if (structuredModel && Property.IsContentTypeSupported(elementType + Property.StructuredSuffix, _options.ContentManagementApi))
{
elementType += Property.StructuredSuffix;
}

var elementId = ElementIdHelper.GetElementId(_options.ContentManagementApi, managementSnippets, managementContentType, element);
var managementElement = ManagementElementHelper.GetManagementElement(_options.ContentManagementApi, element, managementSnippets, managementContentType);
var elementType = ElementTypeHelper.GetElementType(_options, element.Type, managementElement);
var property = Property.FromContentType(element.Codename, elementType, _options.ContentManagementApi, managementElement?.Id.ToString());

var property = Property.FromContentType(element.Codename, elementType, _options.ContentManagementApi, elementId);
classDefinition.AddPropertyCodenameConstant(element);
classDefinition.AddProperty(property);
}
Expand Down
40 changes: 21 additions & 19 deletions src/Kentico.Kontent.ModelGenerator.Core/Common/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Globalization;
using Kentico.Kontent.Delivery.Abstractions;
using Kentico.Kontent.Management.Models.LanguageVariants.Elements;
using Kentico.Kontent.ModelGenerator.Core.Helpers;

namespace Kentico.Kontent.ModelGenerator.Core.Common
Expand All @@ -22,7 +23,7 @@ public class Property
/// </summary>
public string TypeName { get; }

private static readonly Dictionary<string, string> DeliverTypes = new Dictionary<string, string>
private static readonly Dictionary<string, string> DeliverElementTypesDictionary = new Dictionary<string, string>
{
{ "text", "string" },
{ "rich_text", "string" },
Expand All @@ -37,22 +38,23 @@ public class Property
{ "custom", "string" }
};

private static readonly Dictionary<string, string> ContentManagementTypes = new Dictionary<string, string>
private static readonly Dictionary<string, string> ContentManagementElementTypesDictionary = new Dictionary<string, string>
{
{ "text", "TextElement" },
{ "rich_text", "RichTextElement" },
{ "number", "NumberElement" },
{ "multiple_choice", "MultipleChoiceElement" },
{ "date_time", "DateTimeElement"},
{ "asset", "AssetElement" },
{ "modular_content", "LinkedItemsElement" },
{ "taxonomy", "TaxonomyElement" },
{ "url_slug", "UrlSlugElement" },
{ "custom", "CustomElement" }
{ "text", nameof(TextElement) },
{ "rich_text", nameof(RichTextElement) },
{ "number", nameof(NumberElement) },
{ "multiple_choice", nameof(MultipleChoiceElement) },
{ "date_time", nameof(DateTimeElement)},
{ "asset", nameof(AssetElement) },
{ "modular_content", nameof(LinkedItemsElement) },
{ "subpages", nameof(SubpagesElement) },
{ "taxonomy", nameof(TaxonomyElement) },
{ "url_slug",nameof(UrlSlugElement) },
{ "custom", nameof(CustomElement) }
};

private static Dictionary<string, string> ContentTypeToTypeName(bool cmApi)
=> cmApi ? ContentManagementTypes : DeliverTypes;
private static Dictionary<string, string> GetElementTypesDictionary(bool cmApi)
=> cmApi ? ContentManagementElementTypesDictionary : DeliverElementTypesDictionary;

public Property(string codename, string typeName, string id = null)
{
Expand All @@ -63,17 +65,17 @@ public Property(string codename, string typeName, string id = null)

public static bool IsContentTypeSupported(string contentType, bool cmApi = false)
{
return ContentTypeToTypeName(cmApi).ContainsKey(contentType);
return GetElementTypesDictionary(cmApi).ContainsKey(contentType);
}

public static Property FromContentType(string codename, string contentType, bool cmApi = false, string id = null)
public static Property FromContentType(string codename, string elementContentType, bool cmApi = false, string id = null)
{
if (IsContentTypeSupported(contentType, cmApi))
if (IsContentTypeSupported(elementContentType, cmApi))
{
return new Property(codename, ContentTypeToTypeName(cmApi)[contentType], id);
return new Property(codename, GetElementTypesDictionary(cmApi)[elementContentType], id);
}

throw new ArgumentException($"Unknown Content Type {contentType}", nameof(contentType));
throw new ArgumentException($"Unknown Content Type {elementContentType}", nameof(elementContentType));
}
}
}
64 changes: 0 additions & 64 deletions src/Kentico.Kontent.ModelGenerator.Core/Helpers/ElementIdHelper.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using Kentico.Kontent.Management.Models.Types.Elements;
using Kentico.Kontent.ModelGenerator.Core.Common;
using Kentico.Kontent.ModelGenerator.Core.Configuration;

namespace Kentico.Kontent.ModelGenerator.Core.Helpers
{
public static class ElementTypeHelper
{
public static string GetElementType(CodeGeneratorOptions options, string elementType, ElementMetadataBase managementElement)
{
Validate(options, elementType, managementElement);

if (options.ContentManagementApi && elementType == "modular_content" && managementElement.Type == ElementMetadataType.Subpages)
{
return "subpages";
}

if (options.StructuredModel && Property.IsContentTypeSupported(elementType + Property.StructuredSuffix, options.ContentManagementApi))
{
elementType += Property.StructuredSuffix;
}

return elementType;
}

private static void Validate(CodeGeneratorOptions options, string elementType, ElementMetadataBase managementElement)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

if (elementType == null)
{
throw new ArgumentNullException(nameof(elementType));
}

if (options.ContentManagementApi && managementElement == null)
{
throw new ArgumentNullException(nameof(managementElement));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Kentico.Kontent.Delivery.Abstractions;
using Kentico.Kontent.Management.Models.Types;
using Kentico.Kontent.Management.Models.Types.Elements;
using Kentico.Kontent.Management.Models.TypeSnippets;

namespace Kentico.Kontent.ModelGenerator.Core.Helpers
{
public static class ManagementElementHelper
{
public static ElementMetadataBase GetManagementElement(
bool cmApi,
IContentElement deliverElement,
IEnumerable<ContentTypeSnippetModel> managementSnippets,
ContentTypeModel managementContentType)
{
if (!cmApi)
{
return null;
}

Validate(deliverElement, managementSnippets, managementContentType);

var managementContentTypeElement = managementContentType.Elements.FirstOrDefault(el => el.Codename == deliverElement.Codename);
if (managementContentTypeElement != null)
{
return managementContentTypeElement;
}

var managementSnippet = managementSnippets.FirstOrDefault(s =>
managementContentType.Elements.FirstOrDefault(el =>
el.Type == ElementMetadataType.ContentTypeSnippet && el.Codename == s.Codename) != null);
if (managementSnippet == null)
{
throw new ArgumentException($"{nameof(managementSnippet)} shouldn't be null.");
}

var managementSnippetElement = managementSnippet.Elements.FirstOrDefault(el => el.Codename == deliverElement.Codename);
if (managementSnippetElement == null)
{
throw new ArgumentException($"{nameof(managementSnippetElement)} shouldn't be null.");
}

return managementSnippetElement;
}

private static void Validate(
IContentElement deliverElement,
IEnumerable<ContentTypeSnippetModel> managementSnippets,
ContentTypeModel managementContentType)
{
if (deliverElement == null)
{
throw new ArgumentNullException(nameof(deliverElement));
}

if (managementSnippets == null)
{
throw new ArgumentNullException(nameof(managementSnippets));
}

if (managementContentType == null)
{
throw new ArgumentNullException(nameof(managementContentType));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Kentico.Kontent.Management" Version="3.0.0-alpha3" />
<PackageReference Include="Kentico.Kontent.Management" Version="3.0.0-beta1" />
<PackageReference Include="Kentico.Kontent.Delivery.Abstractions" Version="15.0.1" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.8.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ namespace KenticoKontentModels
[JsonProperty("rich_text")]
[KontentElementId("014d2125-923d-4428-93b4-ad1590274912")]
public RichTextElement RichText { get; set; }
[JsonProperty("subpages")]
[KontentElementId("44924563-44d4-4272-a20f-b8745698b082")]
public SubpagesElement Subpages { get; set; }
[JsonProperty("taxonomy")]
[KontentElementId("83011da2-559d-458c-a4b5-c81a001f4139")]
public TaxonomyElement Taxonomy { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ public void CreateCodeGeneratorOptions_OutputSetInParameters_OutputDirHasCustomV
public void GetClassCodeGenerator_Returns(bool contentManagementApi)
{
var mockOptions = new Mock<IOptions<CodeGeneratorOptions>>();
mockOptions.SetupGet(option => option.Value).Returns(new CodeGeneratorOptions { ContentManagementApi = contentManagementApi });
mockOptions.SetupGet(option => option.Value).Returns(new CodeGeneratorOptions
{
ContentManagementApi = contentManagementApi,
StructuredModel = true
});

var deliveryClient = new Mock<IDeliveryClient>();
var outputProvider = new Mock<IOutputProvider>();
Expand All @@ -69,7 +73,7 @@ public void GetClassCodeGenerator_Returns(bool contentManagementApi)

var codeGenerator = new CodeGenerator(mockOptions.Object, deliveryClient.Object, outputProvider.Object, managementClient.Object);

var result = codeGenerator.GetClassCodeGenerator(contentType.Object, true, new List<ContentTypeSnippetModel>(), new ContentTypeModel());
var result = codeGenerator.GetClassCodeGenerator(contentType.Object, new List<ContentTypeSnippetModel>(), new ContentTypeModel());

Assert.Equal($"{contentTypeCodename}.Generated", result.ClassFilename);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void DAPIModel_FromContentType(string contentType, string expectedTypeNam
[InlineData("date_time", "DateTimeElement")]
[InlineData("asset", "AssetElement")]
[InlineData("modular_content", "LinkedItemsElement")]
[InlineData("subpages", "SubpagesElement")]
[InlineData("taxonomy", "TaxonomyElement")]
[InlineData("url_slug", "UrlSlugElement")]
[InlineData("custom", "CustomElement")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void Build_CreatesClassWithCompleteContentType_CMAPI()
classDefinition.AddProperty(Property.FromContentType("date_time", "date_time", true, "66756a72-6af8-44a4-b58c-485425586a90"));
classDefinition.AddProperty(Property.FromContentType("asset", "asset", true, "af569649-ee18-4d6a-a095-ea6ffa012546"));
classDefinition.AddProperty(Property.FromContentType("modular_content", "modular_content", true, "4fa6bad6-d984-45e8-8ebb-f6be25626ee5"));
classDefinition.AddProperty(Property.FromContentType("subpages", "subpages", true, "44924563-44d4-4272-a20f-b8745698b082"));
classDefinition.AddProperty(Property.FromContentType("taxonomy", "taxonomy", true, "83011da2-559d-458c-a4b5-c81a001f4139"));
classDefinition.AddProperty(Property.FromContentType("url_slug", "url_slug", true, "14390f27-213e-4f8d-9c31-cbf9a7c0a0d8"));
classDefinition.AddProperty(Property.FromContentType("custom", "custom", true, "23154ba2-73fc-450c-99d4-c18ba45bb743"));
Expand Down
Loading

0 comments on commit a1b0278

Please sign in to comment.