diff --git a/src/Squidex.Write/Schemas/SchemaDomainObject.cs b/src/Squidex.Write/Schemas/SchemaDomainObject.cs index ec99f7bee..3e9577ac6 100644 --- a/src/Squidex.Write/Schemas/SchemaDomainObject.cs +++ b/src/Squidex.Write/Schemas/SchemaDomainObject.cs @@ -16,6 +16,7 @@ using Squidex.Infrastructure.CQRS.Events; using Squidex.Infrastructure.Dispatching; using Squidex.Infrastructure.Reflection; using Squidex.Write.Schemas.Commands; +using System.Collections.Generic; namespace Squidex.Write.Schemas { @@ -111,35 +112,49 @@ namespace Squidex.Write.Schemas isDeleted = true; } - public SchemaDomainObject AddField(AddField command) + public SchemaDomainObject Create(CreateSchema command) { - Guard.Valid(command, nameof(command), () => $"Cannot add field to schema {Id}"); + Guard.Valid(command, nameof(command), () => "Cannot create schema"); - VerifyCreatedAndNotDeleted(); + VerifyNotCreated(); - RaiseEvent(SimpleMapper.Map(command, new FieldAdded { FieldId = new NamedId(totalFields + 1, command.Name) })); + var @event = SimpleMapper.Map(command, new SchemaCreated { SchemaId = new NamedId(Id, command.Name) }); + + if (command.Fields != null) + { + @event.Fields = new List(); + + foreach (var commandField in command.Fields) + { + var eventField = SimpleMapper.Map(commandField, new SchemaCreatedField()); + + @event.Fields.Add(eventField); + } + } + + RaiseEvent(@event); return this; } - public SchemaDomainObject UpdateField(UpdateField command) + public SchemaDomainObject AddField(AddField command) { - Guard.Valid(command, nameof(command), () => $"Cannot update schema '{Id}'"); + Guard.Valid(command, nameof(command), () => $"Cannot add field to schema {Id}"); VerifyCreatedAndNotDeleted(); - RaiseEvent(command, SimpleMapper.Map(command, new FieldUpdated())); + RaiseEvent(SimpleMapper.Map(command, new FieldAdded { FieldId = new NamedId(totalFields + 1, command.Name) })); return this; } - public SchemaDomainObject Create(CreateSchema command) + public SchemaDomainObject UpdateField(UpdateField command) { - Guard.Valid(command, nameof(command), () => "Cannot create schema"); + Guard.Valid(command, nameof(command), () => $"Cannot update schema '{Id}'"); - VerifyNotCreated(); + VerifyCreatedAndNotDeleted(); - RaiseEvent(SimpleMapper.Map(command, new SchemaCreated { SchemaId = new NamedId(Id, command.Name) })); + RaiseEvent(command, SimpleMapper.Map(command, new FieldUpdated())); return this; }