Skip to content

Commit

Permalink
Automatic deserialize JSON to IContent implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmusjp committed Dec 10, 2019
1 parent f78cad8 commit 5837b3d
Show file tree
Hide file tree
Showing 33 changed files with 316 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Umbraco.Headless.Client.Samples.Web", "Umbraco.Headless.Client.Samples.Web\Umbraco.Headless.Client.Samples.Web.csproj", "{7E351693-56E2-47B9-94FE-F7481C9674AB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Umbraco.Headless.Client.Net", "..\..\src\Umbraco.Headless.Client.Net\Umbraco.Headless.Client.Net.csproj", "{71515FBA-2746-4AAE-B9BF-99ED4AFF60CE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -12,5 +14,9 @@ Global
{7E351693-56E2-47B9-94FE-F7481C9674AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7E351693-56E2-47B9-94FE-F7481C9674AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7E351693-56E2-47B9-94FE-F7481C9674AB}.Release|Any CPU.Build.0 = Release|Any CPU
{71515FBA-2746-4AAE-B9BF-99ED4AFF60CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{71515FBA-2746-4AAE-B9BF-99ED4AFF60CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{71515FBA-2746-4AAE-B9BF-99ED4AFF60CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{71515FBA-2746-4AAE-B9BF-99ED4AFF60CE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ public static T Value<T>(this Content content, string alias)
return (T) Convert.ChangeType(value, typeof(T));
}

public static bool IsVisible(this Content content)
public static bool IsVisible(this IContent content)
{
return content.Value<bool>("umbracoNaviHide") == false;
if (content is IHideInNavigation hideInNavigation)
return hideInNavigation.HideInNavigation == false;

if (content is Content c)
return c.Value<bool>("umbracoNaviHide") == false;

return true;
}
}
public static class ElementExtensions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Umbraco.Headless.Client.Net.Delivery.Models;

namespace Umbraco.Headless.Client.Samples.Web.Models
{
public class Frontpage : ContentBase, IContent, IHideInNavigation
{
public string ContentTypeAlias { get; set; }

public string HeroTitle { get; set; }
public string HeroSubtitle { get; set; }
public Image HeroImage { get; set; }

public string UniqueSellingPointsTitle { get; set; }
public IEnumerable<UniqueSellingPoint> UniqueSellingPoints { get; set; }

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

public string FooterTitle { get; set; }
public IEnumerable<MultiUrlPickerLink> FooterLinks { get; set; }

[JsonProperty("umbracoNaviHide")]
public bool HideInNavigation { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Umbraco.Headless.Client.Samples.Web.Models
{
public interface IHideInNavigation
{
bool HideInNavigation { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using Microsoft.AspNetCore.Html;
using Newtonsoft.Json;
using Umbraco.Headless.Client.Samples.Web.Serialization;

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

[JsonConverter(typeof(HtmlContentConverter))]
public IHtmlContent Text { get; set; }
public string ImageUrl { get; set; }
public bool ShowLargeImage { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Umbraco.Headless.Client.Net.Delivery.Models;

namespace Umbraco.Headless.Client.Samples.Web.Models
{
public class Textpage : ContentBase, IContent, IHideInNavigation
{
public string ContentTypeAlias { get; set; }

public string HeroTitle { get; set; }
public string HeroSubtitle { get; set; }
public Image HeroImage { get; set; }

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

[JsonProperty("umbracoNaviHide")]
public bool HideInNavigation { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +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 UniqueSellingPoint
{
public string Title { get; set; }

[JsonConverter(typeof(HtmlContentConverter))]
public IHtmlContent Text { get; set; }
public MultiUrlPickerLink Link { get; set; }
public string ImageUrl { get; set; }
public Image Image { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using Microsoft.AspNetCore.Html;
using Newtonsoft.Json;

namespace Umbraco.Headless.Client.Samples.Web.Serialization
{
public class HtmlContentConverter : JsonConverter
{
public override bool CanWrite { get; } = false;

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var value = serializer.Deserialize<string>(reader);
return new HtmlString(value);
}

public override bool CanConvert(Type objectType) => typeof(IHtmlContent).IsAssignableFrom(objectType);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Headless.Client.Net.Configuration;
using Umbraco.Headless.Client.Samples.Web.Models;
using Umbraco.Headless.Client.Samples.Web.Mvc;

namespace Umbraco.Headless.Client.Samples.Web
Expand Down Expand Up @@ -36,7 +38,11 @@ public void ConfigureServices(IServiceCollection services)
var projectAlias = umbracoConfig.GetValue<string>("projectAlias");
var apiKey = umbracoConfig.GetValue<string>("apiKey");

services.AddUmbracoHeadlessContentDelivery(projectAlias, apiKey);
var configuration = new ApiKeyBasedConfiguration(projectAlias, apiKey);
configuration.ContentModelTypes.Add<Frontpage>();
configuration.ContentModelTypes.Add<Textpage>();

services.AddUmbracoHeadlessContentDelivery(configuration);

services.AddUmbracoHeadlessWebEngine();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Umbraco.Headless.Client.Net" Version="0.9.0.28874-RC" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.7.9" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Umbraco.Headless.Client.Net\Umbraco.Headless.Client.Net.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public FooterViewComponent(UmbracoContext umbracoContext)

public async Task<IViewComponentResult> InvokeAsync()
{
var content = (Content) await _umbracoContext.Cache.GetContentByUrl("/");
var content = (Frontpage) await _umbracoContext.Cache.GetContentByUrl("/");

return View(new FooterViewModel
{
Title = content.Value<string>("footerTitle"),
Links = content.Value<IEnumerable<MultiUrlPickerLink>>("footerLinks")
Title = content.FooterTitle,
Links = content.FooterLinks
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task<IViewComponentResult> InvokeAsync()
var rootContent = await _umbracoCache.GetContentByUrl("/");
var children = await _contentDeliveryService.Content.GetChildren(rootContent.Id);

return View(from item in children.Content.Items.Where(x => x.IsVisible())
return View(from item in children.Content.Items
select new NavigationItem
{
Title = item.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@ namespace Umbraco.Headless.Client.Samples.Web.ViewComponents
{
public class UniqueSellingPointsViewComponent : ViewComponent
{
public IViewComponentResult Invoke(string title, IEnumerable<Content> contents)
public IViewComponentResult Invoke(string title, IEnumerable<UniqueSellingPoint> contents)
{
return View(new UniqueSellingPointsViewModel
{
Title = title,
UniqueSellingPoints = from c in contents
select new UniqueSellingPoint
{
Link = c.Value<MultiUrlPickerLink>("link"),
Text = c.Value<IHtmlContent>("text"),
Title = c.Value<string>("title"),
ImageUrl = c.Value<Image>("image")?.Url
}
UniqueSellingPoints = contents
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
@using Umbraco.Headless.Client.Net.Delivery.Models
@model Umbraco.Headless.Client.Net.Delivery.Models.Content
@model Frontpage

@await Component.InvokeAsync("Hero", new
{
title = Model.Value<string>("heroTitle"),
subTitle = Model.Value<string>("heroSubtitle"),
image = Model.Value<Image>("heroImage")
title = Model.HeroTitle,
subTitle = Model.HeroSubtitle,
image = Model.HeroImage
})

@await Component.InvokeAsync("UniqueSellingPoints", new
{
title = Model.Value<string>("uniqueSellingPointsTitle"),
contents = Model.Value<IEnumerable<Content>>("uniqueSellingPoints")
title = Model.UniqueSellingPointsTitle,
contents = Model.UniqueSellingPoints
})

@await Html.PartialAsync("_Elements", Model.Value<IEnumerable<Element>>("elements"))
@await Html.PartialAsync("_Elements", Model.Elements)
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
@using Umbraco.Headless.Client.Net.Delivery.Models
@model Umbraco.Headless.Client.Net.Delivery.Models.Content
@model Textpage

@await Component.InvokeAsync("Hero", new {
title = Model.Value<string>("heroTitle"),
subTitle = Model.Value<string>("heroSubtitle"),
image = Model.Value<Image>("heroImage")
title = Model.HeroTitle,
subTitle = Model.HeroSubtitle,
image = Model.HeroImage,
})

@await Html.PartialAsync("_Elements", Model.Value<IEnumerable<Element>>("elements"))
@await Html.PartialAsync("_Elements", Model.Elements)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
{
<section class="usp">
<h2 class="usp__title">@usp.Title</h2>
<img class="usp__image" src="@usp.ImageUrl" alt="">
@if (usp.Image != null)
{
<img class="usp__image" src="@usp.Image.Url" alt="">
}
<p class="usp__text">@usp.Text</p>
@if (usp.Link != null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@model IEnumerable<Element>
@model IEnumerable<Umbraco.Headless.Client.Net.Delivery.Models.Element>

@foreach (var element in Model)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
namespace Umbraco.Headless.Client.Net.Configuration
{
public class ApiKeyBasedConfiguration : IApiKeyBasedConfiguration
public class ApiKeyBasedConfiguration : HeadlessConfiguration, IApiKeyBasedConfiguration
{
public ApiKeyBasedConfiguration(string projectAlias, string token)
public ApiKeyBasedConfiguration(string projectAlias, string token) : base(projectAlias)
{
ProjectAlias = projectAlias;
Token = token;
}
public string ProjectAlias { get; }

public string Token { get; }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Umbraco.Headless.Client.Net.Delivery.Models;

namespace Umbraco.Headless.Client.Net.Configuration
{
public class HeadlessConfiguration : IHeadlessConfiguration
{
public HeadlessConfiguration(string projectAlias)
{
ProjectAlias = projectAlias ?? throw new ArgumentNullException(nameof(projectAlias));
ContentModelTypes = new TypeList<IContent>();
}

public string ProjectAlias { get; }
public ITypeList<IContent> ContentModelTypes { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
namespace Umbraco.Headless.Client.Net.Configuration
using System;
using System.Collections;
using System.Collections.Generic;
using Umbraco.Headless.Client.Net.Delivery.Models;

namespace Umbraco.Headless.Client.Net.Configuration
{
public interface IHeadlessConfiguration
{
string ProjectAlias { get; }
ITypeList<IContent> ContentModelTypes { get; }
}

public interface ITypeList<in TBaseType> : IEnumerable<Type>
{
void Add<TImplementation>() where TImplementation : TBaseType;
void Add(Type type);
void Remove<TImplementation>() where TImplementation : TBaseType;
void Remove(Type type);
void Clear();
}

internal class TypeList<TBaseType> : ITypeList<TBaseType>
{
private readonly List<Type> _types = new List<Type>();

public IEnumerator<Type> GetEnumerator() => _types.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

public void Add<TImplementation>() where TImplementation : TBaseType => _types.Add(typeof(TImplementation));

public void Add(Type type)
{
if (typeof(TBaseType).IsAssignableFrom(type) == false)
throw new Exception();

_types.Add(type);
}

public void Remove<TImplementation>() where TImplementation : TBaseType =>
_types.Remove(typeof(TImplementation));

public void Remove(Type type) => _types.Remove(type);

public void Clear() => _types.Clear();
}
}
Loading

0 comments on commit 5837b3d

Please sign in to comment.