Skip to content

Commit

Permalink
Add auto Element deserialization and refactor web sample
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmusjp committed Jan 6, 2020
1 parent 4aaea84 commit 84db6cc
Show file tree
Hide file tree
Showing 20 changed files with 127 additions and 227 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Frontpage : Content, IHideInNavigation
public string UniqueSellingPointsTitle { get; set; }
public IEnumerable<UniqueSellingPoint> UniqueSellingPoints { get; set; }

public IEnumerable<Element> Elements { get; set; }
public IEnumerable<IElement> Elements { get; set; }

public string FooterTitle { get; set; }
public IEnumerable<MultiUrlPickerLink> FooterLinks { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using Microsoft.AspNetCore.Html;
using Newtonsoft.Json;
using Umbraco.Headless.Client.Net.Delivery.Models;
using Umbraco.Headless.Client.Samples.Web.Serialization;

namespace Umbraco.Headless.Client.Samples.Web.Models
{
public class TextAndImage
public class TextAndImage : Element
{
public string Title { get; set; }

[JsonConverter(typeof(HtmlContentConverter))]
public IHtmlContent Text { get; set; }
public string ImageUrl { get; set; }
public Image Image { get; set; }
public bool ShowLargeImage { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public void ConfigureServices(IServiceCollection services)
configuration.ContentModelTypes.Add<Frontpage>();
configuration.ContentModelTypes.Add<Textpage>();

configuration.ElementModelTypes.Add<TextAndImage>();

services.AddUmbracoHeadlessContentDelivery(configuration);

services.AddUmbracoHeadlessWebEngine();
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
image = Model.HeroImage
})

@await Component.InvokeAsync("UniqueSellingPoints", new
@await Html.PartialAsync("_UniqueSellingPoints", new UniqueSellingPointsViewModel
{
title = Model.UniqueSellingPointsTitle,
contents = Model.UniqueSellingPoints
Title = Model.UniqueSellingPointsTitle,
UniqueSellingPoints = Model.UniqueSellingPoints
})

@await Html.PartialAsync("_Elements", Model.Elements)
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@
@Model.Text
</div>
@* Fix image and sizes *@
@if (Model.ShowLargeImage && Model.ImageUrl != null)
@if (Model.ShowLargeImage && Model.Image != null)
{
<img class="text-and-image__image" srcset="
@Model.ImageUrl?width=320&height=240&mode=crop 320w,
@Model.ImageUrl?width=480&height=360&mode=crop 480w,
@Model.ImageUrl?width=1024px&height=768&mode=crop 1024w"
@Model.Image.Url?width=320&height=240&mode=crop 320w,
@Model.Image.Url?width=480&height=360&mode=crop 480w,
@Model.Image.Url?width=1024px&height=768&mode=crop 1024w"
sizes="(max-width: 320px) 320px,
(max-width: 480px) 480px
(max-width: 1024px) 1024px"
src="@Model.ImageUrl?width750&height=515&mode=crop"
src="@Model.Image.Url?width750&height=515&mode=crop"
loading="lazy"
alt="">
}
else if(Model.ImageUrl != null)
else if(Model.Image != null)
{
<img class="text-and-image__image" srcset="
@Model.ImageUrl?width=320&height=240&mode=crop 320w,
@Model.ImageUrl?width=480&height=360&mode=crop 480w,
@Model.ImageUrl?width=1024px&height=768&mode=crop 1024w"
@Model.Image.Url?width=320&height=240&mode=crop 320w,
@Model.Image.Url?width=480&height=360&mode=crop 480w,
@Model.Image.Url?width=1024px&height=768&mode=crop 1024w"
sizes="(max-width: 320px) 320px,
(max-width: 480px) 480px
(max-width: 1024px) 1024px"
src="@Model.ImageUrl?width=320&height=240&mode=crop"
src="@Model.Image.Url?width=320&height=240&mode=crop"
loading="lazy"
alt="">
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@model IEnumerable<Umbraco.Headless.Client.Net.Delivery.Models.Element>
@model IEnumerable<Umbraco.Headless.Client.Net.Delivery.Models.IElement>

@foreach (var element in Model)
{
@await Component.InvokeAsync(element.ContentTypeAlias, new { element })
await Html.RenderPartialAsync($"Elements/_{element.ContentTypeAlias}", element);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class HeadlessConfiguration : IStronglyTypedHeadlessConfiguration
public HeadlessConfiguration(string projectAlias)
{
ProjectAlias = projectAlias ?? throw new ArgumentNullException(nameof(projectAlias));
ElementModelTypes = new TypeList<IElement>();
ContentModelTypes = new TypeList<IContent>();
MediaModelTypes = new TypeList<IMedia>
{
Expand All @@ -19,6 +20,7 @@ public HeadlessConfiguration(string projectAlias)
}

public string ProjectAlias { get; }
public ITypeList<IElement> ElementModelTypes { get; }
public ITypeList<IContent> ContentModelTypes { get; }
public ITypeList<IMedia> MediaModelTypes { get; }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
using System.Linq;
using Newtonsoft.Json;
using Umbraco.Headless.Client.Net.Delivery;
using Umbraco.Headless.Client.Net.Delivery.Models;
using Umbraco.Headless.Client.Net.Serialization;

namespace Umbraco.Headless.Client.Net.Configuration
{
internal static class HeadlessConfigurationExtensions
{
public static JsonConverter[] GetJsonConverters(this IHeadlessConfiguration configuration)
public static JsonConverter[] GetJsonConverters(this IHeadlessConfiguration configuration,
ModelNameResolver modelNameResolver)
{
if (configuration is IStronglyTypedHeadlessConfiguration stronglyTypedHeadlessConfiguration)
{
return new JsonConverter[]
{
new ContentConverter(
stronglyTypedHeadlessConfiguration.ContentModelTypes.ToDictionary(ContentDelivery.GetAliasFromClassName)
new ModelConverter<IContent, Content>(
stronglyTypedHeadlessConfiguration.ContentModelTypes.ToDictionary(modelNameResolver.GetContentModelAlias),
"contentTypeAlias"
),
new ModelConverter<IMedia, Media>(
stronglyTypedHeadlessConfiguration.MediaModelTypes.ToDictionary(modelNameResolver.GetMediaModelAlias),
"mediaTypeAlias"
),
new ModelConverter<IElement, Element>(
stronglyTypedHeadlessConfiguration.ElementModelTypes.ToDictionary(modelNameResolver.GetElementModelAlias),
"contentTypeAlias"
),

new MediaConverter(
stronglyTypedHeadlessConfiguration.MediaModelTypes.ToDictionary(MediaDelivery.GetAliasFromClassName)
)
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Umbraco.Headless.Client.Net.Configuration
{
public interface IStronglyTypedHeadlessConfiguration : IHeadlessConfiguration
{
ITypeList<IElement> ElementModelTypes { get; }
ITypeList<IContent> ContentModelTypes { get; }
ITypeList<IMedia> MediaModelTypes { get; }
}
Expand Down
26 changes: 5 additions & 21 deletions src/Umbraco.Headless.Client.Net/Delivery/ContentDelivery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ internal class ContentDelivery : IContentDelivery
{
private readonly IHeadlessConfiguration _configuration;
private readonly HttpClient _httpClient;
private readonly ModelNameResolver _modelNameResolver;
private ContentDeliveryEndpoints _service;

public ContentDelivery(IHeadlessConfiguration configuration, HttpClient httpClient)
public ContentDelivery(IHeadlessConfiguration configuration, HttpClient httpClient, ModelNameResolver modelNameResolver)
{
_configuration = configuration;
_httpClient = httpClient;
_modelNameResolver = modelNameResolver;
}

private ContentDeliveryEndpoints Service =>
Expand All @@ -29,7 +31,7 @@ public ContentDelivery(IHeadlessConfiguration configuration, HttpClient httpClie
{
ContentSerializer = new JsonContentSerializer(new JsonSerializerSettings
{
Converters = _configuration.GetJsonConverters()
Converters = _configuration.GetJsonConverters(_modelNameResolver)
})
}));

Expand Down Expand Up @@ -158,24 +160,6 @@ public async Task<PagedContent> Search(string term, string culture = null, int p
return content;
}

private static string GetAliasFromClassName<T>() => GetAliasFromClassName(typeof(T));

internal static string GetAliasFromClassName(Type type)
{
if (type.GetCustomAttribute(typeof(ContentModelAttribute)) is ContentModelAttribute attr)
return attr.ContentTypeAlias;

var className = type.Name;
if (className.IndexOf("Model", StringComparison.Ordinal) > -1)
{
className = className.Substring(0, className.IndexOf("Model", StringComparison.Ordinal));
}

// test for default casing
if (className.Length > 1)
className = $"{className.Substring(0, 1).ToLower()}{className.Substring(1)}";

return className;
}
private string GetAliasFromClassName<T>() => _modelNameResolver.GetContentModelAlias(typeof(T));
}
}
18 changes: 12 additions & 6 deletions src/Umbraco.Headless.Client.Net/Delivery/ContentDeliveryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ public ContentDeliveryService(IPasswordBasedConfiguration configuration)
BaseAddress = new Uri(Constants.Urls.BaseCdnUrl)
};

Content = new ContentDelivery(configuration, httpClient);
Media = new MediaDelivery(configuration, httpClient);
var modelNameResolver = new ModelNameResolver();

Content = new ContentDelivery(configuration, httpClient, modelNameResolver);
Media = new MediaDelivery(configuration, httpClient, modelNameResolver);
}

/// <summary>
Expand All @@ -70,8 +72,10 @@ public ContentDeliveryService(IApiKeyBasedConfiguration configuration)
DefaultRequestHeaders = { { Constants.Headers.ApiKey, configuration.Token } }
};

Content = new ContentDelivery(configuration, httpClient);
Media = new MediaDelivery(configuration, httpClient);
var modelNameResolver = new ModelNameResolver();

Content = new ContentDelivery(configuration, httpClient, modelNameResolver);
Media = new MediaDelivery(configuration, httpClient, modelNameResolver);
}

/// <summary>
Expand All @@ -84,8 +88,10 @@ public ContentDeliveryService(IApiKeyBasedConfiguration configuration)
/// <param name="httpClient">Reference to the <see cref="HttpClient"/></param>
public ContentDeliveryService(IHeadlessConfiguration configuration, HttpClient httpClient)
{
Content = new ContentDelivery(configuration, httpClient);
Media = new MediaDelivery(configuration, httpClient);
var modelNameResolver = new ModelNameResolver();

Content = new ContentDelivery(configuration, httpClient, modelNameResolver);
Media = new MediaDelivery(configuration, httpClient, modelNameResolver);
}

/// <summary>
Expand Down
Loading

0 comments on commit 84db6cc

Please sign in to comment.