Example #1
0
        public ContentDomainObject ChangeStatus(ChangeContentStatus command)
        {
            VerifyCreatedAndNotDeleted();

            RaiseEvent(SimpleMapper.Map(command, new ContentStatusChanged()));

            return(this);
        }
Example #2
0
        public void CanChangeContentStatus_should_not_throw_exception_if_status_flow_valid()
        {
            var command = new ChangeContentStatus {
                Status = Status.Published
            };

            GuardContent.CanChangeContentStatus(schema, false, Status.Draft, command);
        }
Example #3
0
        public void CanChangeContentStatus_should_throw_exception_if_status_flow_not_valid()
        {
            var command = new ChangeContentStatus {
                Status = Status.Published
            };

            Assert.Throws <ValidationException>(() => GuardContent.CanChangeContentStatus(Status.Archived, command));
        }
Example #4
0
        public void CanChangeContentStatus_should_throw_exception_if_due_date_in_past()
        {
            var command = new ChangeContentStatus {
                Status = Status.Published, DueTime = dueTimeInPast
            };

            Assert.Throws <ValidationException>(() => GuardContent.CanChangeContentStatus(Status.Draft, command));
        }
Example #5
0
        public void CanChangeContentStatus_should_not_throw_exception_if_publishing_with_pending_changes()
        {
            var command = new ChangeContentStatus {
                Status = Status.Published
            };

            GuardContent.CanChangeContentStatus(true, Status.Published, command);
        }
Example #6
0
        public void CanChangeContentStatus_should_throw_exception_if_publishing_without_pending_changes()
        {
            var command = new ChangeContentStatus {
                Status = Status.Published
            };

            ValidationAssert.Throws(() => GuardContent.CanChangeContentStatus(schema, false, Status.Published, command),
                                    new ValidationError("Content has no changes to publish.", "Status"));
        }
Example #7
0
        public void CanChangeContentStatus_should_throw_exception_if_due_date_in_past()
        {
            var command = new ChangeContentStatus {
                Status = Status.Published, DueTime = dueTimeInPast
            };

            ValidationAssert.Throws(() => GuardContent.CanChangeContentStatus(schema, false, Status.Draft, command),
                                    new ValidationError("Due time must be in the future.", "DueTime"));
        }
Example #8
0
        public void CanChangeContentStatus_should_throw_exception_if_status_flow_not_valid()
        {
            var command = new ChangeContentStatus {
                Status = Status.Published
            };

            ValidationAssert.Throws(() => GuardContent.CanChangeContentStatus(schema, false, Status.Archived, command),
                                    new ValidationError("Cannot change status from Archived to Published.", "Status"));
        }
Example #9
0
        public void CanChangeContentStatus_should_throw_exception_if_status_not_valid()
        {
            var command = new ChangeContentStatus {
                Status = (Status)10
            };

            ValidationAssert.Throws(() => GuardContent.CanChangeContentStatus(schema, false, Status.Archived, command),
                                    new ValidationError("Status is not valid.", "Status"));
        }
Example #10
0
        public static async Task CanChangeStatus(ChangeContentStatus command,
                                                 IContentEntity content,
                                                 IContentWorkflow contentWorkflow,
                                                 IContentRepository contentRepository,
                                                 ISchemaEntity schema)
        {
            Guard.NotNull(command, nameof(command));

            CheckPermission(content, command, Permissions.AppContentsChangeStatus, Permissions.AppContentsUpsert);

            var newStatus = command.Status;

            if (schema.SchemaDef.IsSingleton)
            {
                if (content.NewStatus == null || newStatus != Status.Published)
                {
                    throw new DomainException(T.Get("contents.singletonNotChangeable"));
                }

                return;
            }

            var oldStatus = content.NewStatus ?? content.Status;

            if (oldStatus == Status.Published && command.CheckReferrers)
            {
                var hasReferrer = await contentRepository.HasReferrersAsync(content.AppId.Id, command.ContentId, SearchScope.Published);

                if (hasReferrer)
                {
                    throw new DomainException(T.Get("contents.referenced"));
                }
            }

            await Validate.It(async e =>
            {
                if (!command.DoNotValidateWorkflow)
                {
                    if (!await contentWorkflow.CanMoveToAsync(content, oldStatus, newStatus, command.User))
                    {
                        var values = new { oldStatus, newStatus };

                        e(T.Get("contents.statusTransitionNotAllowed", values), "Status");
                    }
                }
                else
                {
                    var info = await contentWorkflow.GetInfoAsync(content, newStatus);

                    if (info == null)
                    {
                        e(T.Get("contents.statusNotValid"), "Status");
                    }
                }
            });
        }
Example #11
0
        public async Task CanChangeStatus_should_throw_exception_if_publishing_without_pending_changes()
        {
            var content = CreateContent(Status.Published, false);
            var command = new ChangeContentStatus {
                Status = Status.Published
            };

            await ValidationAssert.ThrowsAsync(() => GuardContent.CanChangeStatus(schema, content, contentWorkflow, command, true),
                                               new ValidationError("Content has no changes to publish.", "Status"));
        }
        public void CanChangeContentStatus_should_throw_exception_if_singleton()
        {
            SetupSingleton(true);

            var command = new ChangeContentStatus {
                Status = Status.Draft
            };

            Assert.Throws <DomainException>(() => GuardContent.CanChangeContentStatus(schema, false, Status.Published, command));
        }
Example #13
0
        public void CanChangeContentStatus_should_not_throw_exception_if_publishing_with_pending_changes()
        {
            A.CallTo(() => schema.IsSingleton).Returns(true);

            var command = new ChangeContentStatus {
                Status = Status.Published
            };

            GuardContent.CanChangeContentStatus(schema, true, Status.Published, command);
        }
Example #14
0
        public async Task CanChangeStatus_should_not_throw_exception_if_publishing_with_pending_changes()
        {
            SetupSingleton(true);

            var content = CreateContent(Status.Published, true);
            var command = new ChangeContentStatus {
                Status = Status.Published
            };

            await GuardContent.CanChangeStatus(schema, content, contentWorkflow, command, true);
        }
Example #15
0
        public ContentDomainObject ChangeStatus(ChangeContentStatus command)
        {
            Guard.NotNull(command, nameof(command));

            VerifyCreatedAndNotDeleted();
            VerifyCanChangeStatus(command.Status);

            RaiseEvent(SimpleMapper.Map(command, new ContentStatusChanged()));

            return(this);
        }
        protected Task On(ChangeContentStatus command, CommandContext context)
        {
            return(handler.UpdateAsync <ContentDomainObject>(context, async content =>
            {
                var schemaAndApp = await ResolveSchemaAndAppAsync(command);

                ExecuteScript(command, content, schemaAndApp.SchemaEntity.ScriptChange, command.Status);

                content.ChangeStatus(command);
            }));
        }
Example #17
0
        public async Task CanChangeStatus_should_not_throw_exception_if_singleton_is_published()
        {
            var schema = CreateSchema(true);

            var content = CreateDraftContent(Status.Draft);
            var command = new ChangeContentStatus {
                Status = Status.Published
            };

            await GuardContent.CanChangeStatus(schema, content, contentWorkflow, command);
        }
Example #18
0
        public async Task CanChangeStatus_should_throw_exception_if_singleton()
        {
            var schema = CreateSchema(true);

            var content = CreateContent(Status.Published);
            var command = new ChangeContentStatus {
                Status = Status.Draft
            };

            await Assert.ThrowsAsync <DomainException>(() => GuardContent.CanChangeStatus(schema, content, contentWorkflow, command));
        }
Example #19
0
        public async Task <IActionResult> RestoreContent(string name, Guid id)
        {
            await contentQuery.FindSchemaAsync(App, name);

            var command = new ChangeContentStatus {
                Status = Status.Draft, ContentId = id
            };

            await CommandBus.PublishAsync(command);

            return(NoContent());
        }
Example #20
0
        public static void CanChangeContentStatus(Status status, ChangeContentStatus command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(() => "Cannot change status.", error =>
            {
                if (!StatusFlow.Exists(command.Status) || !StatusFlow.CanChange(status, command.Status))
                {
                    error(new ValidationError($"Content cannot be changed from status {status} to {command.Status}.", nameof(command.Status)));
                }
            });
        }
Example #21
0
        public async Task CanChangeStatus_should_not_throw_exception_if_status_flow_valid()
        {
            var content = CreateContent(Status.Draft, false);
            var command = new ChangeContentStatus {
                Status = Status.Published, User = user
            };

            A.CallTo(() => contentWorkflow.CanMoveToAsync(content, command.Status, user))
            .Returns(true);

            await GuardContent.CanChangeStatus(schema, content, contentWorkflow, command, false);
        }
Example #22
0
        private async Task ChangeCore(ChangeContentStatus c, ContentOperation operation)
        {
            operation.MustHavePermission(Permissions.AppContentsChangeStatus);
            operation.MustNotChangeSingleton(c.Status);

            if (c.Status == Snapshot.EditingStatus())
            {
                return;
            }

            if (c.DoNotValidateWorkflow)
            {
                await operation.CheckStatusAsync(c.Status);
            }
            else
            {
                await operation.CheckTransitionAsync(c.Status);
            }

            if (!c.DoNotScript)
            {
                var newData = await operation.ExecuteChangeScriptAsync(c.Status, GetChange(c.Status));

                if (!newData.Equals(Snapshot.Data))
                {
                    var previousEvent =
                        GetUncomittedEvents().Select(x => x.Payload)
                        .OfType <ContentDataCommand>().FirstOrDefault();

                    if (previousEvent != null)
                    {
                        previousEvent.Data = newData;
                    }
                    else if (!newData.Equals(Snapshot.Data))
                    {
                        Update(c, newData);
                    }
                }
            }

            if (c.CheckReferrers && Snapshot.IsPublished())
            {
                await operation.CheckReferrersAsync();
            }

            if (!c.DoNotValidate && c.Status == Status.Published && operation.SchemaDef.Properties.ValidateOnPublish)
            {
                await operation.ValidateContentAndInputAsync(Snapshot.Data, c.OptimizeValidation, true);
            }

            ChangeStatus(c);
        }
Example #23
0
        protected Task On(ChangeContentStatus command, CommandContext context)
        {
            return(handler.UpdateAsync <ContentDomainObject>(context, async content =>
            {
                GuardContent.CanChangeContentStatus(content.Snapshot.Status, command);

                var operationContext = await CreateContext(command, content, () => "Failed to patch content.");

                await operationContext.ExecuteScriptAsync(x => x.ScriptChange, command.Status);

                content.ChangeStatus(command);
            }));
        }
        public Task PublishAsync()
        {
            var now = clock.GetCurrentInstant();

            return(contentRepository.Value.QueryScheduledWithoutDataAsync(now, content =>
            {
                var command = new ChangeContentStatus {
                    ContentId = content.Id, Status = content.ScheduledTo.Value, Actor = content.ScheduledBy
                };

                return commandBus.Value.PublishAsync(command);
            }));
        }
Example #25
0
 public void ChangeStatus(ChangeContentStatus command)
 {
     if (command.DueTime.HasValue)
     {
         RaiseEvent(SimpleMapper.Map(command, new ContentStatusScheduled {
             DueTime = command.DueTime.Value
         }));
     }
     else
     {
         RaiseEvent(SimpleMapper.Map(command, new ContentStatusChanged()));
     }
 }
Example #26
0
        public async Task CanChangeStatus_should_throw_exception_if_due_date_in_past()
        {
            var content = CreateContent(Status.Draft, false);
            var command = new ChangeContentStatus {
                Status = Status.Published, DueTime = dueTimeInPast, User = user
            };

            A.CallTo(() => contentWorkflow.CanMoveToAsync(content, command.Status, user))
            .Returns(true);

            await ValidationAssert.ThrowsAsync(() => GuardContent.CanChangeStatus(schema, content, contentWorkflow, command, false),
                                               new ValidationError("Due time must be in the future.", "DueTime"));
        }
Example #27
0
        public async Task CanChangeStatus_should_throw_exception_if_status_flow_not_valid()
        {
            var content = CreateContent(Status.Draft, false);
            var command = new ChangeContentStatus {
                Status = Status.Published, User = user
            };

            A.CallTo(() => contentWorkflow.CanMoveToAsync(content, command.Status, user))
            .Returns(false);

            await ValidationAssert.ThrowsAsync(() => GuardContent.CanChangeStatus(schema, content, contentWorkflow, command, false),
                                               new ValidationError("Cannot change status from Draft to Published.", "Status"));
        }
Example #28
0
        public async Task ChangeStatus_should_not_throw_exception_if_referenced_by_other_item_but_forced()
        {
            var command = new ChangeContentStatus {
                Status = Status.Draft, CheckReferrers = false
            };

            await ExecuteCreateAsync();
            await ExecuteChangeStatusAsync(Status.Published);

            A.CallTo(() => contentRepository.HasReferrersAsync(AppId, contentId, SearchScope.Published))
            .Returns(true);

            await PublishAsync(command);
        }
Example #29
0
        public async Task CanChangeStatus_should_throw_exception_if_referenced()
        {
            var schema = CreateSchema(true);

            var content = CreateContent(Status.Published);
            var command = new ChangeContentStatus {
                Status = Status.Draft, User = user
            };

            A.CallTo(() => contentRepository.HasReferrersAsync(appId.Id, content.Id, SearchScope.Published))
            .Returns(true);

            await Assert.ThrowsAsync <DomainException>(() => GuardContent.CanChangeStatus(command, content, contentWorkflow, contentRepository, schema));
        }
Example #30
0
        public static Task CanChangeStatus(ChangeContentStatus command,
                                           IContentEntity content,
                                           IContentWorkflow contentWorkflow,
                                           IContentRepository contentRepository,
                                           ISchemaEntity schema)
        {
            Guard.NotNull(command, nameof(command));

            CheckPermission(content, command, Permissions.AppContentsChangeStatus);

            if (schema.SchemaDef.IsSingleton)
            {
                if (content.NewStatus == null || command.Status != Status.Published)
                {
                    throw new DomainException(T.Get("contents.singletonNotChangeable"));
                }

                return(Task.CompletedTask);
            }

            return(Validate.It(async e =>
            {
                var status = content.NewStatus ?? content.Status;

                if (!await contentWorkflow.CanMoveToAsync(content, status, command.Status, command.User))
                {
                    var values = new { oldStatus = status, newStatus = command.Status };

                    e(T.Get("contents.statusTransitionNotAllowed", values), nameof(command.Status));
                }

                if (content.Status == Status.Published && command.CheckReferrers)
                {
                    var hasReferrer = await contentRepository.HasReferrersAsync(content.AppId.Id, command.ContentId, SearchScope.Published);

                    if (hasReferrer)
                    {
                        throw new DomainException(T.Get("contents.referenced"));
                    }
                }

                if (command.DueTime.HasValue && command.DueTime.Value < SystemClock.Instance.GetCurrentInstant())
                {
                    e(T.Get("contents.statusSchedulingNotInFuture"), nameof(command.DueTime));
                }
            }));
        }