Exemplo n.º 1
0
        public override Task <object?> ExecuteAsync(IAggregateCommand command)
        {
            switch (command)
            {
            case UpsertContent uspertContent:
            {
                if (Version > EtagVersion.Empty)
                {
                    var updateContent = SimpleMapper.Map(uspertContent, new UpdateContent());

                    return(ExecuteAsync(updateContent));
                }
                else
                {
                    var createContent = SimpleMapper.Map(uspertContent, new CreateContent());

                    return(ExecuteAsync(createContent));
                }
            }

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

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

                    var status = await context.GetInitialStatusAsync();

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

                    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)
                    {
                        c.Data = await context.ExecuteScriptAndTransformAsync(s => s.Change,
                                                                              new ScriptVars
                        {
                            Operation = "Published",
                            Data = c.Data,
                            Status = Status.Published,
                            StatusOld = default
                        });
Exemplo n.º 2
0
        public override Task <CommandResult> ExecuteAsync(IAggregateCommand command)
        {
            switch (command)
            {
            case UpsertContent upsertContent:
                return(UpsertReturnAsync(upsertContent, async c =>
                {
                    await LoadContext(c, c.OptimizeValidation);

                    if (Version > EtagVersion.Empty && !IsDeleted())
                    {
                        await UpdateCore(c.AsUpdate(), x => c.Data, false);
                    }
                    else
                    {
                        await CreateCore(c.AsCreate());
                    }

                    if (Is.OptionalChange(Snapshot.EditingStatus, c.Status))
                    {
                        await ChangeCore(c.AsChange(c.Status.Value));
                    }

                    return Snapshot;
                }));

            case CreateContent createContent:
                return(CreateReturnAsync(createContent, async c =>
                {
                    await LoadContext(c, false);

                    await CreateCore(c);

                    if (Is.OptionalChange(Snapshot.EditingStatus, c.Status))
                    {
                        await ChangeCore(c.AsChange(c.Status.Value));
                    }

                    return Snapshot;
                }));

            case ValidateContent validate:
                return(UpdateReturnAsync(validate, async c =>
                {
                    await LoadContext(c, false);

                    GuardContent.CanValidate(c, Snapshot);

                    await context.ValidateContentAndInputAsync(Snapshot.Data);

                    return true;
                }));

            case CreateContentDraft createDraft:
                return(UpdateReturnAsync(createDraft, async c =>
                {
                    await LoadContext(c, false);

                    GuardContent.CanCreateDraft(c, Snapshot);

                    var status = await context.GetInitialStatusAsync();

                    CreateDraft(c, status);

                    return Snapshot;
                }));

            case DeleteContentDraft deleteDraft:
                return(UpdateReturnAsync(deleteDraft, async c =>
                {
                    await LoadContext(c, false);

                    GuardContent.CanDeleteDraft(c, Snapshot);

                    DeleteDraft(c);

                    return Snapshot;
                }));

            case PatchContent patchContent:
                return(UpdateReturnAsync(patchContent, async c =>
                {
                    await LoadContext(c, c.OptimizeValidation);

                    await UpdateCore(c, c.Data.MergeInto, true);

                    return Snapshot;
                }));

            case UpdateContent updateContent:
                return(UpdateReturnAsync(updateContent, async c =>
                {
                    await LoadContext(c, c.OptimizeValidation);

                    await UpdateCore(c, x => c.Data, false);

                    return Snapshot;
                }));

            case ChangeContentStatus changeContentStatus:
                return(UpdateReturnAsync(changeContentStatus, async c =>
                {
                    try
                    {
                        await LoadContext(c, c.OptimizeValidation);

                        if (c.DueTime > SystemClock.Instance.GetCurrentInstant())
                        {
                            ScheduleStatus(c, c.DueTime.Value);
                        }
                        else
                        {
                            await ChangeCore(c);
                        }
                    }
                    catch (Exception)
                    {
                        if (Snapshot.ScheduleJob != null && Snapshot.ScheduleJob.Id == c.StatusJobId)
                        {
                            CancelChangeStatus(c);
                        }
                        else
                        {
                            throw;
                        }
                    }

                    return Snapshot;
                }));

            case DeleteContent deleteContent when(deleteContent.Permanent):
                return(DeletePermanentAsync(deleteContent, async c =>
                {
                    await DeleteCore(c);
                }));

            case DeleteContent deleteContent:
                return(UpdateAsync(deleteContent, async c =>
                {
                    await DeleteCore(c);
                }));

            default:
                throw new NotSupportedException();
            }
        }