mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
5.9 KiB
165 lines
5.9 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Squidex.Domain.Apps.Core.Contents;
|
|
using Squidex.Domain.Apps.Entities.Apps.Commands;
|
|
using Squidex.Domain.Apps.Entities.Apps.Templates.Builders;
|
|
using Squidex.Domain.Apps.Entities.Contents.Commands;
|
|
using Squidex.Domain.Apps.Entities.Schemas.Commands;
|
|
using Squidex.Infrastructure;
|
|
using Squidex.Infrastructure.Commands;
|
|
|
|
namespace Squidex.Domain.Apps.Entities.Apps.Templates
|
|
{
|
|
public sealed class CreateBlogCommandMiddleware : ICommandMiddleware
|
|
{
|
|
private const string TemplateName = "Blog";
|
|
|
|
public async Task HandleAsync(CommandContext context, Func<Task> next)
|
|
{
|
|
if (context.IsCompleted && context.Command is CreateApp createApp && IsRightTemplate(createApp))
|
|
{
|
|
var appId = NamedId.Of(createApp.AppId, createApp.Name);
|
|
|
|
var publish = new Func<ICommand, Task>(command =>
|
|
{
|
|
if (command is IAppCommand appCommand)
|
|
{
|
|
appCommand.AppId = appId;
|
|
}
|
|
|
|
return context.CommandBus.PublishAsync(command);
|
|
});
|
|
|
|
await Task.WhenAll(
|
|
CreatePagesAsync(publish),
|
|
CreatePostsAsync(publish),
|
|
CreateClientAsync(publish, appId.Id));
|
|
}
|
|
|
|
await next();
|
|
}
|
|
|
|
private static bool IsRightTemplate(CreateApp createApp)
|
|
{
|
|
return string.Equals(createApp.Template, TemplateName, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
private static async Task CreateClientAsync(Func<ICommand, Task> publish, Guid appId)
|
|
{
|
|
await publish(new AttachClient { Id = "sample-client", AppId = appId });
|
|
}
|
|
|
|
private static async Task CreatePostsAsync(Func<ICommand, Task> publish)
|
|
{
|
|
var postsId = await CreatePostsSchemaAsync(publish);
|
|
|
|
await publish(new CreateContent
|
|
{
|
|
SchemaId = postsId,
|
|
Data =
|
|
new NamedContentData()
|
|
.AddField("title",
|
|
new ContentFieldData()
|
|
.AddValue("iv", "My first post with Squidex"))
|
|
.AddField("text",
|
|
new ContentFieldData()
|
|
.AddValue("iv", "Just created a blog with Squidex. I love it!")),
|
|
Publish = true
|
|
});
|
|
}
|
|
|
|
private static async Task CreatePagesAsync(Func<ICommand, Task> publish)
|
|
{
|
|
var pagesId = await CreatePagesSchemaAsync(publish);
|
|
|
|
await publish(new CreateContent
|
|
{
|
|
SchemaId = pagesId,
|
|
Data =
|
|
new NamedContentData()
|
|
.AddField("title",
|
|
new ContentFieldData()
|
|
.AddValue("iv", "About Me"))
|
|
.AddField("text",
|
|
new ContentFieldData()
|
|
.AddValue("iv", "I love Squidex and SciFi!")),
|
|
Publish = true
|
|
});
|
|
}
|
|
|
|
private static async Task<NamedId<Guid>> CreatePostsSchemaAsync(Func<ICommand, Task> publish)
|
|
{
|
|
var schema =
|
|
SchemaBuilder.Create("Posts")
|
|
.AddString("Title", f => f
|
|
.Length(100)
|
|
.Required()
|
|
.ShowInList()
|
|
.Hints("The title of the post."))
|
|
.AddString("Text", f => f
|
|
.AsRichText()
|
|
.Length(100)
|
|
.Required()
|
|
.Hints("The text of the post."))
|
|
.AddString("Slug", f => f
|
|
.Disabled()
|
|
.Label("Slug (Autogenerated)")
|
|
.Hints("Autogenerated slug that can be used to identity the post."))
|
|
.Build();
|
|
|
|
await publish(schema);
|
|
|
|
var schemaId = NamedId.Of(schema.SchemaId, schema.Name);
|
|
|
|
await publish(new ConfigureScripts
|
|
{
|
|
SchemaId = schemaId.Id,
|
|
ScriptCreate = Scripts.Slug,
|
|
ScriptUpdate = Scripts.Slug
|
|
});
|
|
|
|
return schemaId;
|
|
}
|
|
|
|
private static async Task<NamedId<Guid>> CreatePagesSchemaAsync(Func<ICommand, Task> publish)
|
|
{
|
|
var schema =
|
|
SchemaBuilder.Create("Pages")
|
|
.AddString("Title", f => f
|
|
.Length(100)
|
|
.Required()
|
|
.ShowInList()
|
|
.Hints("The title of the page."))
|
|
.AddString("Text", f => f
|
|
.AsRichText()
|
|
.Length(100)
|
|
.Required()
|
|
.Hints("The text of the page."))
|
|
.AddString("Slug", f => f
|
|
.Disabled()
|
|
.Label("Slug (Autogenerated)")
|
|
.Hints("Autogenerated slug that can be used to identity the page."))
|
|
.Build();
|
|
|
|
await publish(schema);
|
|
|
|
var schemaId = NamedId.Of(schema.SchemaId, schema.Name);
|
|
|
|
await publish(new ConfigureScripts
|
|
{
|
|
SchemaId = schemaId.Id,
|
|
ScriptCreate = Scripts.Slug,
|
|
ScriptUpdate = Scripts.Slug
|
|
});
|
|
|
|
return schemaId;
|
|
}
|
|
}
|
|
}
|