// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Collections.Generic; using Squidex.Domain.Apps.Core.Schemas; using Squidex.Domain.Apps.Entities.Schemas.Commands; using Squidex.Infrastructure; namespace Squidex.Domain.Apps.Entities.Apps.Templates.Builders { public sealed class SchemaBuilder { private readonly CreateSchema command; public SchemaBuilder(CreateSchema command) { this.command = command; } public static SchemaBuilder Create(string name) { var schemaName = name.ToKebabCase(); return new SchemaBuilder(new CreateSchema { Name = schemaName }).Published().WithLabel(name); } public SchemaBuilder WithLabel(string? label) { command.Properties ??= new SchemaProperties(); command.Properties.Label = label; return this; } public SchemaBuilder WithScripts(SchemaScripts scripts) { command.Scripts = scripts; return this; } public SchemaBuilder Published() { command.IsPublished = true; return this; } public SchemaBuilder Singleton() { command.IsSingleton = true; return this; } public SchemaBuilder AddAssets(string name, Action configure) { var field = AddField(name); configure(new AssetFieldBuilder(field, command)); return this; } public SchemaBuilder AddBoolean(string name, Action configure) { var field = AddField(name); configure(new BooleanFieldBuilder(field, command)); return this; } public SchemaBuilder AddDateTime(string name, Action configure) { var field = AddField(name); configure(new DateTimeFieldBuilder(field, command)); return this; } public SchemaBuilder AddJson(string name, Action configure) { var field = AddField(name); configure(new JsonFieldBuilder(field, command)); return this; } public SchemaBuilder AddNumber(string name, Action configure) { var field = AddField(name); configure(new NumberFieldBuilder(field, command)); return this; } public SchemaBuilder AddString(string name, Action configure) { var field = AddField(name); configure(new StringFieldBuilder(field, command)); return this; } public SchemaBuilder AddTags(string name, Action configure) { var field = AddField(name); configure(new TagsFieldBuilder(field, command)); return this; } private UpsertSchemaField AddField(string name) where T : FieldProperties, new() { var field = new UpsertSchemaField { Name = name.ToCamelCase(), Properties = new T { Label = name } }; command.Fields ??= new List(); command.Fields.Add(field); return field; } public CreateSchema Build() { return command; } } }