Esempio n. 1
0
        public override Task <object?> ExecuteAsync(IAggregateCommand command)
        {
            switch (command)
            {
            case CreateApp createApp:
                return(CreateReturn(createApp, c =>
                {
                    GuardApp.CanCreate(c);

                    Create(c);

                    return Snapshot;
                }));

            case UpdateApp updateApp:
                return(UpdateReturn(updateApp, c =>
                {
                    GuardApp.CanUpdate(c);

                    Update(c);

                    return Snapshot;
                }));

            case UploadAppImage uploadImage:
                return(UpdateReturn(uploadImage, c =>
                {
                    GuardApp.CanUploadImage(c);

                    UploadImage(c);

                    return Snapshot;
                }));

            case RemoveAppImage removeImage:
                return(UpdateReturn(removeImage, c =>
                {
                    GuardApp.CanRemoveImage(c);

                    RemoveImage(c);

                    return Snapshot;
                }));

            case AssignContributor assignContributor:
                return(UpdateReturnAsync(assignContributor, async c =>
                {
                    await GuardAppContributors.CanAssign(Snapshot.Contributors, Snapshot.Roles, c, userResolver, GetPlan());

                    AssignContributor(c, !Snapshot.Contributors.ContainsKey(assignContributor.ContributorId));

                    return Snapshot;
                }));

            case RemoveContributor removeContributor:
                return(UpdateReturn(removeContributor, c =>
                {
                    GuardAppContributors.CanRemove(Snapshot.Contributors, c);

                    RemoveContributor(c);

                    return Snapshot;
                }));

            case AttachClient attachClient:
                return(UpdateReturn(attachClient, c =>
                {
                    GuardAppClients.CanAttach(Snapshot.Clients, c);

                    AttachClient(c);

                    return Snapshot;
                }));

            case UpdateClient updateClient:
                return(UpdateReturn(updateClient, c =>
                {
                    GuardAppClients.CanUpdate(Snapshot.Clients, c, Snapshot.Roles);

                    UpdateClient(c);

                    return Snapshot;
                }));

            case RevokeClient revokeClient:
                return(UpdateReturn(revokeClient, c =>
                {
                    GuardAppClients.CanRevoke(Snapshot.Clients, c);

                    RevokeClient(c);

                    return Snapshot;
                }));

            case AddWorkflow addWorkflow:
                return(UpdateReturn(addWorkflow, c =>
                {
                    GuardAppWorkflows.CanAdd(c);

                    AddWorkflow(c);

                    return Snapshot;
                }));

            case UpdateWorkflow updateWorkflow:
                return(UpdateReturn(updateWorkflow, c =>
                {
                    GuardAppWorkflows.CanUpdate(Snapshot.Workflows, c);

                    UpdateWorkflow(c);

                    return Snapshot;
                }));

            case DeleteWorkflow deleteWorkflow:
                return(UpdateReturn(deleteWorkflow, c =>
                {
                    GuardAppWorkflows.CanDelete(Snapshot.Workflows, c);

                    DeleteWorkflow(c);

                    return Snapshot;
                }));

            case AddLanguage addLanguage:
                return(UpdateReturn(addLanguage, c =>
                {
                    GuardAppLanguages.CanAdd(Snapshot.Languages, c);

                    AddLanguage(c);

                    return Snapshot;
                }));

            case RemoveLanguage removeLanguage:
                return(UpdateReturn(removeLanguage, c =>
                {
                    GuardAppLanguages.CanRemove(Snapshot.Languages, c);

                    RemoveLanguage(c);

                    return Snapshot;
                }));

            case UpdateLanguage updateLanguage:
                return(UpdateReturn(updateLanguage, c =>
                {
                    GuardAppLanguages.CanUpdate(Snapshot.Languages, c);

                    UpdateLanguage(c);

                    return Snapshot;
                }));

            case AddRole addRole:
                return(UpdateReturn(addRole, c =>
                {
                    GuardAppRoles.CanAdd(Snapshot.Roles, c);

                    AddRole(c);

                    return Snapshot;
                }));

            case DeleteRole deleteRole:
                return(UpdateReturn(deleteRole, c =>
                {
                    GuardAppRoles.CanDelete(Snapshot.Roles, c, Snapshot.Contributors, Snapshot.Clients);

                    DeleteRole(c);

                    return Snapshot;
                }));

            case UpdateRole updateRole:
                return(UpdateReturn(updateRole, c =>
                {
                    GuardAppRoles.CanUpdate(Snapshot.Roles, c);

                    UpdateRole(c);

                    return Snapshot;
                }));

            case AddPattern addPattern:
                return(UpdateReturn(addPattern, c =>
                {
                    GuardAppPatterns.CanAdd(Snapshot.Patterns, c);

                    AddPattern(c);

                    return Snapshot;
                }));

            case DeletePattern deletePattern:
                return(UpdateReturn(deletePattern, c =>
                {
                    GuardAppPatterns.CanDelete(Snapshot.Patterns, c);

                    DeletePattern(c);

                    return Snapshot;
                }));

            case UpdatePattern updatePattern:
                return(UpdateReturn(updatePattern, c =>
                {
                    GuardAppPatterns.CanUpdate(Snapshot.Patterns, c);

                    UpdatePattern(c);

                    return Snapshot;
                }));

            case ChangePlan changePlan:
                return(UpdateReturnAsync(changePlan, async c =>
                {
                    GuardApp.CanChangePlan(c, Snapshot.Plan, appPlansProvider);

                    if (c.FromCallback)
                    {
                        ChangePlan(c);

                        return null;
                    }
                    else
                    {
                        var result = await appPlansBillingManager.ChangePlanAsync(c.Actor.Identifier, Snapshot.NamedId(), c.PlanId, c.Referer);

                        switch (result)
                        {
                        case PlanChangedResult _:
                            ChangePlan(c);
                            break;
                        }

                        return result;
                    }
                }));

            case ArchiveApp archiveApp:
                return(UpdateAsync(archiveApp, async c =>
                {
                    await appPlansBillingManager.ChangePlanAsync(c.Actor.Identifier, Snapshot.NamedId(), null, null);

                    ArchiveApp(c);
                }));

            default:
                throw new NotSupportedException();
            }
        }
Esempio n. 2
0
        protected override Task <object> ExecuteAsync(IAggregateCommand command)
        {
            VerifyNotArchived();

            switch (command)
            {
            case CreateApp createApp:
                return(CreateAsync(createApp, c =>
                {
                    GuardApp.CanCreate(c);

                    Create(c);
                }));

            case AssignContributor assigneContributor:
                return(UpdateReturnAsync(assigneContributor, async c =>
                {
                    await GuardAppContributors.CanAssign(Snapshot.Contributors, c, userResolver, appPlansProvider.GetPlan(Snapshot.Plan?.PlanId), Snapshot.Roles);

                    AssignContributor(c);

                    return EntityCreatedResult.Create(c.ContributorId, Version);
                }));

            case RemoveContributor removeContributor:
                return(UpdateAsync(removeContributor, c =>
                {
                    GuardAppContributors.CanRemove(Snapshot.Contributors, c);

                    RemoveContributor(c);
                }));

            case AttachClient attachClient:
                return(UpdateAsync(attachClient, c =>
                {
                    GuardAppClients.CanAttach(Snapshot.Clients, c);

                    AttachClient(c);
                }));

            case UpdateClient updateClient:
                return(UpdateAsync(updateClient, c =>
                {
                    GuardAppClients.CanUpdate(Snapshot.Clients, c, Snapshot.Roles);

                    UpdateClient(c);
                }));

            case RevokeClient revokeClient:
                return(UpdateAsync(revokeClient, c =>
                {
                    GuardAppClients.CanRevoke(Snapshot.Clients, c);

                    RevokeClient(c);
                }));

            case AddLanguage addLanguage:
                return(UpdateAsync(addLanguage, c =>
                {
                    GuardAppLanguages.CanAdd(Snapshot.LanguagesConfig, c);

                    AddLanguage(c);
                }));

            case RemoveLanguage removeLanguage:
                return(UpdateAsync(removeLanguage, c =>
                {
                    GuardAppLanguages.CanRemove(Snapshot.LanguagesConfig, c);

                    RemoveLanguage(c);
                }));

            case UpdateLanguage updateLanguage:
                return(UpdateAsync(updateLanguage, c =>
                {
                    GuardAppLanguages.CanUpdate(Snapshot.LanguagesConfig, c);

                    UpdateLanguage(c);
                }));

            case AddRole addRole:
                return(UpdateAsync(addRole, c =>
                {
                    GuardAppRoles.CanAdd(Snapshot.Roles, c);

                    AddRole(c);
                }));

            case DeleteRole deleteRole:
                return(UpdateAsync(deleteRole, c =>
                {
                    GuardAppRoles.CanDelete(Snapshot.Roles, c, Snapshot.Contributors, Snapshot.Clients);

                    DeleteRole(c);
                }));

            case UpdateRole updateRole:
                return(UpdateAsync(updateRole, c =>
                {
                    GuardAppRoles.CanUpdate(Snapshot.Roles, c);

                    UpdateRole(c);
                }));

            case AddPattern addPattern:
                return(UpdateAsync(addPattern, c =>
                {
                    GuardAppPatterns.CanAdd(Snapshot.Patterns, c);

                    AddPattern(c);
                }));

            case DeletePattern deletePattern:
                return(UpdateAsync(deletePattern, c =>
                {
                    GuardAppPatterns.CanDelete(Snapshot.Patterns, c);

                    DeletePattern(c);
                }));

            case UpdatePattern updatePattern:
                return(UpdateAsync(updatePattern, c =>
                {
                    GuardAppPatterns.CanUpdate(Snapshot.Patterns, c);

                    UpdatePattern(c);
                }));

            case ChangePlan changePlan:
                return(UpdateReturnAsync(changePlan, async c =>
                {
                    GuardApp.CanChangePlan(c, Snapshot.Plan, appPlansProvider);

                    if (c.FromCallback)
                    {
                        ChangePlan(c);

                        return null;
                    }
                    else
                    {
                        var result = await appPlansBillingManager.ChangePlanAsync(c.Actor.Identifier, Snapshot.Id, Snapshot.Name, c.PlanId);

                        if (result is PlanChangedResult)
                        {
                            ChangePlan(c);
                        }

                        return result;
                    }
                }));

            case ArchiveApp archiveApp:
                return(UpdateAsync(archiveApp, async c =>
                {
                    await appPlansBillingManager.ChangePlanAsync(c.Actor.Identifier, Snapshot.Id, Snapshot.Name, null);

                    ArchiveApp(c);
                }));

            default:
                throw new NotSupportedException();
            }
        }