// ========================================================================== // RuleDomainObject.cs // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex Group // All rights reserved. // ========================================================================== using Squidex.Domain.Apps.Entities.Rules.Commands; using Squidex.Domain.Apps.Entities.Rules.State; using Squidex.Domain.Apps.Events.Rules; using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; using Squidex.Infrastructure.EventSourcing; using Squidex.Infrastructure.Reflection; namespace Squidex.Domain.Apps.Entities.Rules { public class RuleDomainObject : DomainObjectBase { public void Create(CreateRule command) { VerifyNotCreated(); RaiseEvent(SimpleMapper.Map(command, new RuleCreated())); } public void Update(UpdateRule command) { VerifyCreatedAndNotDeleted(); RaiseEvent(SimpleMapper.Map(command, new RuleUpdated())); } public void Enable(EnableRule command) { VerifyCreatedAndNotDeleted(); RaiseEvent(SimpleMapper.Map(command, new RuleEnabled())); } public void Disable(DisableRule command) { VerifyCreatedAndNotDeleted(); RaiseEvent(SimpleMapper.Map(command, new RuleDisabled())); } public void Delete(DeleteRule command) { VerifyCreatedAndNotDeleted(); RaiseEvent(SimpleMapper.Map(command, new RuleDeleted())); } private void VerifyNotCreated() { if (State.RuleDef != null) { throw new DomainException("Webhook has already been created."); } } private void VerifyCreatedAndNotDeleted() { if (State.IsDeleted || State.RuleDef == null) { throw new DomainException("Webhook has already been deleted or not created yet."); } } protected override void OnRaised(Envelope @event) { UpdateState(State.Apply(@event)); } } }