mirror of https://github.com/Squidex/squidex.git
70 changed files with 905 additions and 804 deletions
@ -0,0 +1,46 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using Squidex.Infrastructure; |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Core.Schemas |
||||
|
{ |
||||
|
public static class Scripts |
||||
|
{ |
||||
|
public const string Change = "Change"; |
||||
|
public const string Create = "Create"; |
||||
|
public const string Query = "Query"; |
||||
|
public const string Update = "Update"; |
||||
|
public const string Delete = "Delete"; |
||||
|
|
||||
|
public static string GetChange(this IReadOnlyDictionary<string, string> scripts) |
||||
|
{ |
||||
|
return scripts?.GetOrDefault(Change); |
||||
|
} |
||||
|
|
||||
|
public static string GetCreate(this IReadOnlyDictionary<string, string> scripts) |
||||
|
{ |
||||
|
return scripts?.GetOrDefault(Create); |
||||
|
} |
||||
|
|
||||
|
public static string GetQuery(this IReadOnlyDictionary<string, string> scripts) |
||||
|
{ |
||||
|
return scripts?.GetOrDefault(Query); |
||||
|
} |
||||
|
|
||||
|
public static string GetUpdate(this IReadOnlyDictionary<string, string> scripts) |
||||
|
{ |
||||
|
return scripts?.GetOrDefault(Update); |
||||
|
} |
||||
|
|
||||
|
public static string GetDelete(this IReadOnlyDictionary<string, string> scripts) |
||||
|
{ |
||||
|
return scripts?.GetOrDefault(Delete); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,49 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Apps.Templates |
||||
|
{ |
||||
|
public static class DefaultScripts |
||||
|
{ |
||||
|
private const string ScriptToGenerateSlug = @"
|
||||
|
var data = ctx.data; |
||||
|
|
||||
|
if (data.title && data.title.iv) { |
||||
|
data.slug = { iv: slugify(data.title.iv) }; |
||||
|
|
||||
|
replace(data); |
||||
|
}";
|
||||
|
|
||||
|
private const string ScriptToGenerateUsername = @"
|
||||
|
var data = ctx.data; |
||||
|
|
||||
|
if (data.userName && data.userName.iv) { |
||||
|
data.normalizedUserName = { iv: data.userName.iv.toUpperCase() }; |
||||
|
} |
||||
|
|
||||
|
if (data.email && data.email.iv) { |
||||
|
data.normalizedEmail = { iv: data.email.iv.toUpperCase() }; |
||||
|
} |
||||
|
|
||||
|
replace(data);";
|
||||
|
|
||||
|
public static readonly Dictionary<string, string> GenerateSlug = new Dictionary<string, string> |
||||
|
{ |
||||
|
[Scripts.Create] = ScriptToGenerateSlug, |
||||
|
[Scripts.Update] = ScriptToGenerateSlug |
||||
|
}; |
||||
|
|
||||
|
public static readonly Dictionary<string, string> GenerateUsername = new Dictionary<string, string> |
||||
|
{ |
||||
|
[Scripts.Create] = ScriptToGenerateUsername, |
||||
|
[Scripts.Update] = ScriptToGenerateUsername |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Schemas.Commands |
||||
|
{ |
||||
|
public sealed class SynchronizeSchema : UpsertCommand |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,101 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using Squidex.Domain.Apps.Core; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using SchemaFields = System.Collections.Generic.List<Squidex.Domain.Apps.Entities.Schemas.Commands.UpsertSchemaField>; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Schemas.Commands |
||||
|
{ |
||||
|
public abstract class UpsertCommand : SchemaCommand |
||||
|
{ |
||||
|
public bool Publish { get; set; } |
||||
|
|
||||
|
public string Category { get; set; } |
||||
|
|
||||
|
public SchemaFields Fields { get; set; } |
||||
|
|
||||
|
public SchemaProperties Properties { get; set; } |
||||
|
|
||||
|
public Dictionary<string, string> Scripts { get; set; } |
||||
|
|
||||
|
public Dictionary<string, string> PreviewUrls { get; set; } |
||||
|
|
||||
|
public Schema ToSchema(string name, bool isSingleton) |
||||
|
{ |
||||
|
var schema = new Schema(name, Properties, isSingleton); |
||||
|
|
||||
|
if (Publish) |
||||
|
{ |
||||
|
schema = schema.Publish(); |
||||
|
} |
||||
|
|
||||
|
var totalFields = 0; |
||||
|
|
||||
|
if (Fields != null) |
||||
|
{ |
||||
|
foreach (var eventField in Fields) |
||||
|
{ |
||||
|
totalFields++; |
||||
|
|
||||
|
var partitioning = Partitioning.FromString(eventField.Partitioning); |
||||
|
|
||||
|
var field = eventField.Properties.CreateRootField(totalFields, eventField.Name, partitioning); |
||||
|
|
||||
|
if (field is ArrayField arrayField && eventField.Nested?.Count > 0) |
||||
|
{ |
||||
|
foreach (var nestedEventField in eventField.Nested) |
||||
|
{ |
||||
|
totalFields++; |
||||
|
|
||||
|
var nestedField = nestedEventField.Properties.CreateNestedField(totalFields, nestedEventField.Name); |
||||
|
|
||||
|
if (nestedEventField.IsHidden) |
||||
|
{ |
||||
|
nestedField = nestedField.Hide(); |
||||
|
} |
||||
|
|
||||
|
if (nestedEventField.IsDisabled) |
||||
|
{ |
||||
|
nestedField = nestedField.Disable(); |
||||
|
} |
||||
|
|
||||
|
if (nestedEventField.IsLocked) |
||||
|
{ |
||||
|
nestedField = nestedField.Lock(); |
||||
|
} |
||||
|
|
||||
|
arrayField = arrayField.AddField(nestedField); |
||||
|
} |
||||
|
|
||||
|
field = arrayField; |
||||
|
} |
||||
|
|
||||
|
if (eventField.IsHidden) |
||||
|
{ |
||||
|
field = field.Hide(); |
||||
|
} |
||||
|
|
||||
|
if (eventField.IsDisabled) |
||||
|
{ |
||||
|
field = field.Disable(); |
||||
|
} |
||||
|
|
||||
|
if (eventField.IsLocked) |
||||
|
{ |
||||
|
field = field.Lock(); |
||||
|
} |
||||
|
|
||||
|
schema = schema.AddField(field); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return schema; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,67 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|
||||
// All rights reserved. Licensed under the MIT license.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System; |
|
||||
using Squidex.Domain.Apps.Core.Schemas; |
|
||||
using Squidex.Infrastructure; |
|
||||
using Xunit; |
|
||||
|
|
||||
namespace Squidex.Domain.Apps.Core.Model.Schemas |
|
||||
{ |
|
||||
public class FieldRegistryTests |
|
||||
{ |
|
||||
private readonly FieldRegistry sut = new FieldRegistry(new TypeNameRegistry()); |
|
||||
|
|
||||
private sealed class InvalidProperties : FieldProperties |
|
||||
{ |
|
||||
public override T Accept<T>(IFieldPropertiesVisitor<T> visitor) |
|
||||
{ |
|
||||
return default; |
|
||||
} |
|
||||
|
|
||||
public override T Accept<T>(IFieldVisitor<T> visitor, IField field) |
|
||||
{ |
|
||||
return default; |
|
||||
} |
|
||||
|
|
||||
public override RootField CreateRootField(long id, string name, Partitioning partitioning, IFieldSettings settings = null) |
|
||||
{ |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
public override NestedField CreateNestedField(long id, string name, IFieldSettings settings = null) |
|
||||
{ |
|
||||
return null; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void Should_throw_exception_if_creating_field_and_field_is_not_registered() |
|
||||
{ |
|
||||
Assert.Throws<InvalidOperationException>(() => sut.CreateRootField(1, "name", Partitioning.Invariant, new InvalidProperties())); |
|
||||
} |
|
||||
|
|
||||
[Theory] |
|
||||
[InlineData(typeof(AssetsFieldProperties))] |
|
||||
[InlineData(typeof(BooleanFieldProperties))] |
|
||||
[InlineData(typeof(DateTimeFieldProperties))] |
|
||||
[InlineData(typeof(GeolocationFieldProperties))] |
|
||||
[InlineData(typeof(JsonFieldProperties))] |
|
||||
[InlineData(typeof(NumberFieldProperties))] |
|
||||
[InlineData(typeof(ReferencesFieldProperties))] |
|
||||
[InlineData(typeof(StringFieldProperties))] |
|
||||
[InlineData(typeof(TagsFieldProperties))] |
|
||||
public void Should_create_field_by_properties(Type propertyType) |
|
||||
{ |
|
||||
var properties = (FieldProperties)Activator.CreateInstance(propertyType); |
|
||||
|
|
||||
var field = sut.CreateRootField(1, "name", Partitioning.Invariant, properties); |
|
||||
|
|
||||
Assert.Equal(properties, field.RawProperties); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,106 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using Squidex.Domain.Apps.Core; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Squidex.Domain.Apps.Events; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Squidex.Infrastructure.EventSourcing; |
||||
|
using Squidex.Infrastructure.Reflection; |
||||
|
using SchemaCreatedV2 = Squidex.Domain.Apps.Events.Schemas.SchemaCreated; |
||||
|
using SchemaFields = System.Collections.Generic.List<Squidex.Domain.Apps.Events.Schemas.SchemaCreatedField>; |
||||
|
|
||||
|
namespace Migrate_01.OldEvents |
||||
|
{ |
||||
|
[EventType(nameof(SchemaCreated))] |
||||
|
[Obsolete] |
||||
|
public sealed class SchemaCreated : SchemaEvent, IMigrated<IEvent> |
||||
|
{ |
||||
|
public string Name { get; set; } |
||||
|
|
||||
|
public bool Singleton { get; set; } |
||||
|
|
||||
|
public bool Publish { get; set; } |
||||
|
|
||||
|
public SchemaFields Fields { get; set; } |
||||
|
|
||||
|
public SchemaProperties Properties { get; set; } |
||||
|
|
||||
|
public IEvent Migrate() |
||||
|
{ |
||||
|
var schema = new Schema(Name, Properties, Singleton); |
||||
|
|
||||
|
if (Publish) |
||||
|
{ |
||||
|
schema = schema.Publish(); |
||||
|
} |
||||
|
|
||||
|
var totalFields = 0; |
||||
|
|
||||
|
if (Fields != null) |
||||
|
{ |
||||
|
foreach (var eventField in Fields) |
||||
|
{ |
||||
|
totalFields++; |
||||
|
|
||||
|
var partitioning = Partitioning.FromString(eventField.Partitioning); |
||||
|
|
||||
|
var field = eventField.Properties.CreateRootField(totalFields, eventField.Name, partitioning); |
||||
|
|
||||
|
if (field is ArrayField arrayField && eventField.Nested?.Count > 0) |
||||
|
{ |
||||
|
foreach (var nestedEventField in eventField.Nested) |
||||
|
{ |
||||
|
totalFields++; |
||||
|
|
||||
|
var nestedField = nestedEventField.Properties.CreateNestedField(totalFields, nestedEventField.Name); |
||||
|
|
||||
|
if (nestedEventField.IsHidden) |
||||
|
{ |
||||
|
nestedField = nestedField.Hide(); |
||||
|
} |
||||
|
|
||||
|
if (nestedEventField.IsDisabled) |
||||
|
{ |
||||
|
nestedField = nestedField.Disable(); |
||||
|
} |
||||
|
|
||||
|
if (nestedEventField.IsLocked) |
||||
|
{ |
||||
|
nestedField = nestedField.Lock(); |
||||
|
} |
||||
|
|
||||
|
arrayField = arrayField.AddField(nestedField); |
||||
|
} |
||||
|
|
||||
|
field = arrayField; |
||||
|
} |
||||
|
|
||||
|
if (eventField.IsHidden) |
||||
|
{ |
||||
|
field = field.Hide(); |
||||
|
} |
||||
|
|
||||
|
if (eventField.IsDisabled) |
||||
|
{ |
||||
|
field = field.Disable(); |
||||
|
} |
||||
|
|
||||
|
if (eventField.IsLocked) |
||||
|
{ |
||||
|
field = field.Lock(); |
||||
|
} |
||||
|
|
||||
|
schema = schema.AddField(field); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return SimpleMapper.Map(this, new SchemaCreatedV2 { Schema = schema }); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,65 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Squidex.Domain.Apps.Events; |
||||
|
using Squidex.Domain.Apps.Events.Schemas; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Squidex.Infrastructure.EventSourcing; |
||||
|
using Squidex.Infrastructure.Reflection; |
||||
|
|
||||
|
namespace Migrate_01.OldEvents |
||||
|
{ |
||||
|
[EventType(nameof(ScriptsConfigured))] |
||||
|
[Obsolete] |
||||
|
public sealed class ScriptsConfigured : SchemaEvent, IMigrated<IEvent> |
||||
|
{ |
||||
|
public string ScriptQuery { get; set; } |
||||
|
|
||||
|
public string ScriptCreate { get; set; } |
||||
|
|
||||
|
public string ScriptUpdate { get; set; } |
||||
|
|
||||
|
public string ScriptDelete { get; set; } |
||||
|
|
||||
|
public string ScriptChange { get; set; } |
||||
|
|
||||
|
public IEvent Migrate() |
||||
|
{ |
||||
|
var scripts = new Dictionary<string, string>(); |
||||
|
|
||||
|
if (!string.IsNullOrWhiteSpace(ScriptQuery)) |
||||
|
{ |
||||
|
scripts[Scripts.Query] = ScriptQuery; |
||||
|
} |
||||
|
|
||||
|
if (!string.IsNullOrWhiteSpace(ScriptCreate)) |
||||
|
{ |
||||
|
scripts[Scripts.Create] = ScriptCreate; |
||||
|
} |
||||
|
|
||||
|
if (!string.IsNullOrWhiteSpace(ScriptUpdate)) |
||||
|
{ |
||||
|
scripts[Scripts.Update] = ScriptUpdate; |
||||
|
} |
||||
|
|
||||
|
if (!string.IsNullOrWhiteSpace(ScriptDelete)) |
||||
|
{ |
||||
|
scripts[Scripts.Delete] = ScriptDelete; |
||||
|
} |
||||
|
|
||||
|
if (!string.IsNullOrWhiteSpace(ScriptChange)) |
||||
|
{ |
||||
|
scripts[Scripts.Change] = ScriptChange; |
||||
|
} |
||||
|
|
||||
|
return SimpleMapper.Map(this, new SchemaScriptsConfigured { Scripts = scripts }); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue