public async Task Should_provide_initial_status()
        {
            var context = CreateContext(CreateContent(Status.Draft), normalSchema);

            A.CallTo(() => contentWorkflow.GetInitialStatusAsync(context.Schema))
            .Returns(Status.Archived);

            Assert.Equal(Status.Archived, await context.GetInitialStatusAsync());
        }
Beispiel #2
0
        public override Task <object?> ExecuteAsync(IAggregateCommand command)
        {
            VerifyNotDeleted();

            switch (command)
            {
            case CreateContent createContent:
                return(CreateReturnAsync(createContent, async c =>
                {
                    await LoadContext(c.AppId, c.SchemaId, c, c.OptimizeValidation);

                    await GuardContent.CanCreate(context.Schema, contentWorkflow, c);

                    var status = await contentWorkflow.GetInitialStatusAsync(context.Schema);

                    if (!c.DoNotValidate)
                    {
                        await context.ValidateInputAsync(c.Data);
                    }

                    if (!c.DoNotScript)
                    {
                        c.Data = await context.ExecuteScriptAndTransformAsync(s => s.Create,
                                                                              new ScriptVars
                        {
                            Operation = "Create",
                            Data = c.Data,
                            Status = status,
                            StatusOld = default
                        });
                    }

                    await context.GenerateDefaultValuesAsync(c.Data);

                    if (!c.DoNotValidate)
                    {
                        await context.ValidateContentAsync(c.Data);
                    }

                    if (!c.DoNotScript && c.Publish)
                    {
                        await context.ExecuteScriptAsync(s => s.Change,
                                                         new ScriptVars
                        {
                            Operation = "Published",
                            Data = c.Data,
                            Status = Status.Published,
                            StatusOld = default
                        });
Beispiel #3
0
        protected override Task <object> ExecuteAsync(IAggregateCommand command)
        {
            VerifyNotDeleted();

            switch (command)
            {
            case CreateContent createContent:
                return(CreateReturnAsync(createContent, async c =>
                {
                    var ctx = await CreateContext(c.AppId.Id, c.SchemaId.Id, c, () => "Failed to create content.");

                    var status = (await contentWorkflow.GetInitialStatusAsync(ctx.Schema)).Status;

                    await GuardContent.CanCreate(ctx.Schema, contentWorkflow, c);

                    c.Data = await ctx.ExecuteScriptAndTransformAsync(s => s.Create,
                                                                      new ScriptContext
                    {
                        Operation = "Create",
                        Data = c.Data,
                        Status = status,
                        StatusOld = default
                    });

                    await ctx.EnrichAsync(c.Data);

                    if (!c.DoNotValidate)
                    {
                        await ctx.ValidateAsync(c.Data);
                    }

                    if (c.Publish)
                    {
                        await ctx.ExecuteScriptAsync(s => s.Change,
                                                     new ScriptContext
                        {
                            Operation = "Published",
                            Data = c.Data,
                            Status = Status.Published,
                            StatusOld = status
                        });
                    }

                    Create(c, status);

                    return Snapshot;
                }));
Beispiel #4
0
        protected override Task <object> ExecuteAsync(IAggregateCommand command)
        {
            VerifyNotDeleted();

            switch (command)
            {
            case CreateContent createContent:
                return(CreateReturnAsync(createContent, async c =>
                {
                    var ctx = await CreateContext(c.AppId.Id, c.SchemaId.Id, Guid.Empty, () => "Failed to create content.");

                    GuardContent.CanCreate(ctx.Schema, c);

                    await ctx.ExecuteScriptAndTransformAsync(s => s.Create, "Create", c, c.Data);
                    await ctx.EnrichAsync(c.Data);

                    if (!c.DoNotValidate)
                    {
                        await ctx.ValidateAsync(c.Data);
                    }

                    if (c.Publish)
                    {
                        await ctx.ExecuteScriptAsync(s => s.Change, "Published", c, c.Data);
                    }

                    var statusInfo = await contentWorkflow.GetInitialStatusAsync(ctx.Schema);

                    Create(c, statusInfo.Status);

                    return Snapshot;
                }));

            case UpdateContent updateContent:
                return(UpdateReturnAsync(updateContent, async c =>
                {
                    await GuardContent.CanUpdate(Snapshot, contentWorkflow, c);

                    return await UpdateAsync(c, x => c.Data, false);
                }));

            case PatchContent patchContent:
                return(UpdateReturnAsync(patchContent, async c =>
                {
                    await GuardContent.CanPatch(Snapshot, contentWorkflow, c);

                    return await UpdateAsync(c, c.Data.MergeInto, true);
                }));

            case ChangeContentStatus changeContentStatus:
                return(UpdateReturnAsync(changeContentStatus, async c =>
                {
                    try
                    {
                        var ctx = await CreateContext(Snapshot.AppId.Id, Snapshot.SchemaId.Id, Snapshot.Id, () => "Failed to change content.");

                        await GuardContent.CanChangeStatus(ctx.Schema, Snapshot, contentWorkflow, c);

                        if (c.DueTime.HasValue)
                        {
                            ScheduleStatus(c);
                        }
                        else
                        {
                            if (Snapshot.IsPending && Snapshot.Status == Status.Published && c.Status == Status.Published)
                            {
                                ConfirmChanges(c);
                            }
                            else
                            {
                                StatusChange reason;

                                if (c.Status == Status.Published)
                                {
                                    reason = StatusChange.Published;
                                }
                                else if (Snapshot.Status == Status.Published)
                                {
                                    reason = StatusChange.Unpublished;
                                }
                                else
                                {
                                    reason = StatusChange.Change;
                                }

                                await ctx.ExecuteScriptAsync(s => s.Change, reason, c, Snapshot.Data);

                                ChangeStatus(c, reason);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        if (c.JobId.HasValue && Snapshot?.ScheduleJob.Id == c.JobId)
                        {
                            CancelScheduling(c);
                        }
                        else
                        {
                            throw;
                        }
                    }

                    return Snapshot;
                }));

            case DiscardChanges discardChanges:
                return(UpdateReturn(discardChanges, c =>
                {
                    GuardContent.CanDiscardChanges(Snapshot.IsPending, c);

                    DiscardChanges(c);

                    return Snapshot;
                }));

            case DeleteContent deleteContent:
                return(UpdateAsync(deleteContent, async c =>
                {
                    var ctx = await CreateContext(Snapshot.AppId.Id, Snapshot.SchemaId.Id, Snapshot.Id, () => "Failed to delete content.");

                    GuardContent.CanDelete(ctx.Schema, c);

                    await ctx.ExecuteScriptAsync(s => s.Delete, "Delete", c, Snapshot.Data);

                    Delete(c);
                }));

            default:
                throw new NotSupportedException();
            }
        }
 public Task <Status> GetInitialStatusAsync()
 {
     return(contentWorkflow.GetInitialStatusAsync(schema));
 }