Skip to content

Commit

Permalink
Merge pull request #12 from umbraco/feature/automatic-deserialize-models
Browse files Browse the repository at this point in the history
Automatic deserialize JSON to IContent implementations
  • Loading branch information
sitereactor authored Nov 25, 2020
2 parents 72ddc9b + ba0f652 commit 0842180
Show file tree
Hide file tree
Showing 49 changed files with 652 additions and 253 deletions.
5 changes: 5 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "3.1.404"
}
}
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Umbraco.Headless.Client.Net.Delivery.Models;

namespace Umbraco.Headless.Client.Samples.Web.Models
{
public class Frontpage : Content, IHideInNavigation
{
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<IElement> 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,12 +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
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Umbraco.Headless.Client.Net.Delivery.Models;

namespace Umbraco.Headless.Client.Samples.Web.Models
{
public class Textpage : Content, IHideInNavigation
{
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 @@ -5,6 +5,9 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.IO;
using Microsoft.Extensions.Hosting;
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 All @@ -31,19 +34,25 @@ public void ConfigureServices(IServiceCollection services)

services.AddMvc()
.AddMvcOptions(opt => opt.EnableEndpointRouting = false)
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

var umbracoConfig = Configuration.GetSection("umbraco");
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>();

configuration.ElementModelTypes.Add<TextAndImage>();

services.AddUmbracoHeadlessContentDelivery(configuration);

services.AddUmbracoHeadlessWebEngine();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<LangVersion>latest</LangVersion>
Expand All @@ -11,9 +11,6 @@
</Content>
</ItemGroup>
<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" />
<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

This file was deleted.

This file was deleted.

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
@await Html.PartialAsync("_UniqueSellingPoints", new UniqueSellingPointsViewModel
{
title = Model.Value<string>("uniqueSellingPointsTitle"),
contents = Model.Value<IEnumerable<Content>>("uniqueSellingPoints")
Title = Model.UniqueSellingPointsTitle,
UniqueSellingPoints = 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
@@ -1,4 +1,4 @@
@model IEnumerable<NavigationItem>
@model System.Collections.Generic.IEnumerable<NavigationItem>

<nav class="page-header__nav-container" aria-label="Primary">
<ul class="nav">
Expand Down
Loading

0 comments on commit 0842180

Please sign in to comment.