add page creation handler?...

This commit is contained in:
2020-11-06 14:16:11 +01:00
parent 7edd9549e6
commit ffa66f7c0c
5 changed files with 74 additions and 14 deletions
+45 -2
View File
@@ -1,4 +1,5 @@
using FluentValidation;
using Microsoft.Extensions.Logging;
using Raven.Client.Documents;
using Raven.Client.Documents.Linq;
using Raven.Client.Documents.Session;
@@ -9,6 +10,7 @@ using System.Threading.Tasks;
using zero.Core.Database.Indexes;
using zero.Core.Entities;
using zero.Core.Extensions;
using zero.Core.Handlers;
using zero.Core.Options;
namespace zero.Core.Api
@@ -20,12 +22,48 @@ namespace zero.Core.Api
protected IZeroOptions Options { get; private set; }
protected IRecycleBinApi RecycleBinApi { get; private set; }
public PagesApi(IZeroOptions options, IBackofficeStore store, IRecycleBinApi recycleBinApi) : base(store)
protected ILogger<PagesApi> Logger { get; private set; }
protected IHandlerHolder Handler { get; private set; }
public PagesApi(IZeroOptions options, IBackofficeStore store, IRecycleBinApi recycleBinApi, ILogger<PagesApi> logger, IHandlerHolder handler) : base(store)
{
Options = options;
RecycleBinApi = recycleBinApi;
Logger = logger;
Handler = handler;
}
/// <inheritdoc />
public Task<IPage> GetEmpty(string pageType, string parentId = null)
{
PageType type = GetPageType(pageType);
if (type == null)
{
return Task.FromResult<IPage>(null);
}
try
{
IPage model = Activator.CreateInstance(type.ContentType) as IPage;
model.PageTypeAlias = type.Alias;
model.ParentId = parentId; // TODO validate if type is allowed and if parentid is allowed
Handler.Get<IPageCreationHandler>()?.OnCreate(model);
return Task.FromResult(model);
}
catch
{
Logger.LogWarning("Could not create page with type {type}", type);
}
return Task.FromResult<IPage>(null);
}
@@ -359,6 +397,11 @@ namespace zero.Core.Api
public interface IPagesApi
{
/// <summary>
/// Get a new empty page with the specified type
/// </summary>
public Task<IPage> GetEmpty(string pageType, string parentId = null);
/// <summary>
/// Get page by Id
/// </summary>
+9
View File
@@ -40,6 +40,15 @@ namespace zero.Core.Api
}
/// <summary>
/// Converts a term to a safe alias (suitable for URLs)
/// </summary>
public static string Alias(object value)
{
return Generate(value?.ToString(), Scope.Url);
}
/// <summary>
///
/// </summary>
@@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zero.Core.Entities;
namespace zero.Core.Handlers
{
public interface IPageCreationHandler : IHandler
{
void OnCreate(IPage page);
}
}
+2 -8
View File
@@ -50,15 +50,9 @@ namespace zero.Web.Controllers
}
public EditModel<IPage> GetEmpty([FromQuery] string type, [FromQuery] string parent = null)
public async Task<EditModel<IPage>> GetEmpty([FromQuery] string type, [FromQuery] string parent = null)
{
PageType pageType = Api.GetPageType(type);
IPage model = Activator.CreateInstance(pageType.ContentType) as IPage;
model.PageTypeAlias = type;
model.ParentId = parent;
return Edit(model);
return Edit(await Api.GetEmpty(type, parent));
}
+3 -4
View File
@@ -1,8 +1,10 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using zero.Core.Api;
using zero.Core.Entities;
using zero.Core.Routing;
using Microsoft.Extensions.DependencyInjection;
namespace zero.Web.Controllers
{
@@ -21,10 +23,7 @@ namespace zero.Web.Controllers
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.RouteData.Values.TryGetValue("zero.app", out object app))
{
Application = (IApplication)app;
}
Application = filterContext.HttpContext.RequestServices.GetService<IApplicationContext>()?.App;
if (filterContext.RouteData.Values.TryGetValue("zero.route", out object route))
{