-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatic deserialize JSON to IContent implementations
- Loading branch information
Showing
33 changed files
with
316 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...braco.Headless.Client.Samples.Web/Umbraco.Headless.Client.Samples.Web/Models/Frontpage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...adless.Client.Samples.Web/Umbraco.Headless.Client.Samples.Web/Models/IHideInNavigation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...co.Headless.Client.Samples.Web/Umbraco.Headless.Client.Samples.Web/Models/TextAndImage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...mbraco.Headless.Client.Samples.Web/Umbraco.Headless.Client.Samples.Web/Models/Textpage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
6 changes: 5 additions & 1 deletion
6
...dless.Client.Samples.Web/Umbraco.Headless.Client.Samples.Web/Models/UniqueSellingPoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...ent.Samples.Web/Umbraco.Headless.Client.Samples.Web/Serialization/HtmlContentConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 7 additions & 8 deletions
15
...ent.Samples.Web/Umbraco.Headless.Client.Samples.Web/Views/DefaultUmbraco/Frontpage.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
11 changes: 5 additions & 6 deletions
11
...ient.Samples.Web/Umbraco.Headless.Client.Samples.Web/Views/DefaultUmbraco/Textpage.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...less.Client.Samples.Web/Umbraco.Headless.Client.Samples.Web/Views/Shared/_Elements.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 3 additions & 4 deletions
7
src/Umbraco.Headless.Client.Net/Configuration/ApiKeyBasedConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
12 changes: 0 additions & 12 deletions
12
src/Umbraco.Headless.Client.Net/Configuration/BasicHeadlessConfiguration.cs
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
src/Umbraco.Headless.Client.Net/Configuration/HeadlessConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
43 changes: 42 additions & 1 deletion
43
src/Umbraco.Headless.Client.Net/Configuration/IHeadlessConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.