Skip to content

Commit

Permalink
Add form headless to musicfestival backend and add sample form
Browse files Browse the repository at this point in the history
Fixes: AFORM-4107
Story: AFORM-3507
  • Loading branch information
Linh Hoang committed May 3, 2024
1 parent 882c674 commit 9ef2a98
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 8 deletions.
Binary file modified samples/musicfestival-backend-dotnet/App_Data/db.mdf
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using EPiServer.Cms.Shell;
using EPiServer.Web;
using EPiServer.Web.Routing;
using Microsoft.AspNetCore.Mvc;
using Optimizely.Cms.Preview2.Content;
using Optimizely.Cms.Preview2.Content.Models;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace AlloyMvcTemplates.Controllers;
[Route("api/[controller]")]
[ApiController]
public class ReactController : ControllerBase
{
private readonly IContentRepository _contentRepositoryInteApi;

public ReactController(IContentRepository contentRepositoryInteApi)
{
_contentRepositoryInteApi = contentRepositoryInteApi;
}

[HttpGet("GetFormInPageByUrl")]
public async Task<IActionResult> GetFormInPageByUrl(string url)
{
var builder = new EPiServer.UrlBuilder(url);
var content = UrlResolver.Current.Route(builder, ContextMode.Default);

if (content is null)
{
return NoContent();
}
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
var key = ContentKey.FormatAsKey(content.ContentGuid);
var pageModel = new PageModel();
var contentHeadless = await _contentRepositoryInteApi.GetAsync(key, content.LanguageBranch());

pageModel.Title = contentHeadless.DisplayName;
pageModel.PageUrl = UrlResolver.Current.GetUrl(content.ContentLink);

if (contentHeadless.Properties.ContainsKey("MainContentArea"))
{
pageModel.Childrens.AddRange(contentHeadless.Properties["MainContentArea"] as IList<IContentComponent>);
}

return Ok(pageModel);
}
}

public class PageModel
{
public string Title { get; set; }
public string PageUrl { get; set; }
public List<IContentComponent> Childrens { get; set; } = new List<IContentComponent>();
}
39 changes: 31 additions & 8 deletions samples/musicfestival-backend-dotnet/MusicFestival.Backend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,39 @@
<RootNamespace>MusicFestival.Backend</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<CmsUIVersion>12.26.0</CmsUIVersion>
<CmsCoreVersion>12.20.1</CmsCoreVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EPiServer.CMS" Version="12.23.0" />
<PackageReference Include="EPiServer.ContentDefinitionsApi" Version="3.9.0" />
<PackageReference Include="EPiServer.ContentDeliveryApi.Cms" Version="3.9.0" />
<PackageReference Include="EPiServer.ContentDeliveryApi.Core" Version="3.9.0" />
<PackageReference Include="EPiServer.ContentManagementApi" Version="3.9.0" />
<PackageReference Include="EPiServer.OpenIDConnect" Version="3.9.0" />
<PackageReference Include="EPiServer.OpenIDConnect.UI" Version="3.9.0" />
<PackageReference Include="Optimizely.ContentGraph.Cms" Version="3.4.0" />
<PackageReference Include="EPiServer.CloudPlatform.Cms" Version="1.6.1" />
<PackageReference Include="EPiServer.CMS" Version="12.26.0" />
<PackageReference Include="EPiServer.CMS.AspNetCore" Version="$(CmsCoreVersion)" />
<PackageReference Include="EPiServer.CMS.AspNetCore.HtmlHelpers" Version="$(CmsCoreVersion)" />
<PackageReference Include="EPiServer.CMS.AspNetCore.MVC" Version="$(CmsCoreVersion)" />
<PackageReference Include="EPiServer.CMS.AspNetCore.Routing" Version="$(CmsCoreVersion)" />
<PackageReference Include="EPiServer.CMS.AspNetCore.Templating" Version="$(CmsCoreVersion)" />
<PackageReference Include="EPiServer.CMS.Core" Version="$(CmsCoreVersion)" />
<PackageReference Include="EPiServer.Framework" Version="$(CmsCoreVersion)" />
<PackageReference Include="EPiServer.Framework.AspNetCore" Version="$(CmsCoreVersion)" />
<PackageReference Include="EPiServer.Hosting" Version="$(CmsCoreVersion)" />
<PackageReference Include="EPiServer.CMS.UI" Version="$(CmsUIVersion)" />
<PackageReference Include="EPiServer.CMS.UI.Core" Version="$(CmsUIVersion)" />
<PackageReference Include="EPiServer.CMS.UI.VisitorGroups" Version="$(CmsUIVersion)" />
<PackageReference Include="EPiServer.CMS.UI.AspNetIdentity" Version="$(CmsUIVersion)" />
<PackageReference Include="EPiServer.ContentDefinitionsApi" Version="3.9.1" />
<PackageReference Include="EPiServer.ContentDeliveryApi.Cms" Version="3.9.1" />
<PackageReference Include="EPiServer.ContentDeliveryApi.Core" Version="3.9.1" />
<PackageReference Include="EPiServer.ContentManagementApi" Version="3.9.1" />
<PackageReference Include="EPiServer.OpenIDConnect" Version="3.9.1" />
<PackageReference Include="EPiServer.OpenIDConnect.UI" Version="3.9.1" />
<PackageReference Include="EPiServer.Forms" Version="5.8.0" />
</ItemGroup>

<ItemGroup Label="HeadlessForm">
<PackageReference Include="Optimizely.Headless.Form.Service" Version="0.1.0--inte-256" />
<PackageReference Include="Optimizely.Headless.Form.ContentGraph" Version="0.1.0--inte-256" />
<PackageReference Include="Optimizely.Cms.Preview2.Content.EPiServer" Version="12.20.1-ci-2644" />
<PackageReference Include="Optimizely.ContentGraph.Cms" Version="3.5.1" />
</ItemGroup>
</Project>
49 changes: 49 additions & 0 deletions samples/musicfestival-backend-dotnet/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
using EPiServer.OpenIDConnect;
using EPiServer.Web;
using EPiServer.Web.Routing;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using OpenIddict.Server;
using Optimizely.Headless.Form;
using Optimizely.Headless.Form.DependencyInjection;

// using Optimizely.ContentGraph.Cms.NetCore.ProxyUtils;

Expand All @@ -21,6 +26,11 @@ public class Startup
private readonly string _frontendUri;
private readonly IConfiguration _configuration;

private readonly string _allowedOrigins = "_allowedOrigins";
private const string TestClientId = "TestClient";
private const string TestClientSecret = "TestClientSecret";
private const string ClientEndpoint = "http://localhost:8082";

public Startup(IWebHostEnvironment webHostingEnvironment, IConfiguration configuration)
{
_webHostingEnvironment = webHostingEnvironment;
Expand Down Expand Up @@ -133,9 +143,27 @@ public void ConfigureServices(IServiceCollection services)
o.IncludeInternalContentRoots = true;
o.IncludeNumericContentIdentifier = true;
});
services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<HeadlessFormServiceOptions>, HeadlessFormServiceOptionsPostConfigure>());

// Register the Optimizely Headless Form API Services
services.AddOptimizelyHeadlessFormService(options =>
{
options.EnableOpenApiDocumentation = true;
options.FormCorsPolicy = new FormCorsPolicy
{
AllowOrigins = new string[] { "http://localhost:3000" }, //Enter '*' to allow any origins, multiple origins separate by comma
AllowCredentials = true
};
options.OpenIDConnectClients.Add(new()
{
Authority = ClientEndpoint
});
});

services.AddContentGraph(OpenIDConnectOptionsDefaults.AuthenticationScheme);
services.AddHostedService<ProvisionDatabase>();

services.AddOptimizelyHeadlessFormContentGraph();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Expand Down Expand Up @@ -181,4 +209,25 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
return Task.CompletedTask;
});
}

public class HeadlessFormServiceOptionsPostConfigure : IPostConfigureOptions<HeadlessFormServiceOptions>
{
private readonly OpenIddictServerOptions _options;

public HeadlessFormServiceOptionsPostConfigure(IOptions<OpenIddictServerOptions> options)
{
_options = options.Value;
}

public void PostConfigure(string name, HeadlessFormServiceOptions options)
{
foreach (var client in options.OpenIDConnectClients)
{
foreach (var key in _options.EncryptionCredentials.Select(c => c.Key))
{
client.EncryptionKeys.Add(key);
}
}
}
}
}

0 comments on commit 9ef2a98

Please sign in to comment.