Пример #1
0
        public async override Task HandleAsync(CommandContext context, Func <Task> next)
        {
            switch (context.Command)
            {
            case CreateAsset createAsset:
            {
                createAsset.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(createAsset.File.OpenRead());

                await assetStore.UploadTemporaryAsync(context.ContextId.ToString(), createAsset.File.OpenRead());

                try
                {
                    var result = await ExecuteCommandAsync(createAsset) as AssetSavedResult;

                    context.Complete(EntityCreatedResult.Create(createAsset.AssetId, result.Version));

                    await assetStore.CopyTemporaryAsync(context.ContextId.ToString(), createAsset.AssetId.ToString(), result.FileVersion, null);
                }
                finally
                {
                    await assetStore.DeleteTemporaryAsync(context.ContextId.ToString());
                }

                break;
            }

            case UpdateAsset updateAsset:
            {
                updateAsset.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(updateAsset.File.OpenRead());

                await assetStore.UploadTemporaryAsync(context.ContextId.ToString(), updateAsset.File.OpenRead());

                try
                {
                    var result = await ExecuteCommandAsync(updateAsset) as AssetSavedResult;

                    context.Complete(result);

                    await assetStore.CopyTemporaryAsync(context.ContextId.ToString(), updateAsset.AssetId.ToString(), result.FileVersion, null);
                }
                finally
                {
                    await assetStore.DeleteTemporaryAsync(context.ContextId.ToString());
                }

                break;
            }

            default:
                await base.HandleAsync(context, next);

                break;
            }
        }
Пример #2
0
        public async Task Should_return_single_content_when_creating_content()
        {
            var query = @"
                mutation {
                  createMySchemaContent(data: <DATA>) {
                    <FIELDS>
                  }
                }".Replace("<DATA>", GetDataString()).Replace("<FIELDS>", TestContent.AllFields);

            commandContext.Complete(content);

            var result = await sut.QueryAsync(requestContext, new GraphQLQuery { Query = query });

            var expected = new
            {
                data = new
                {
                    createMySchemaContent = TestContent.Response(content)
                }
            };

            AssertResult(expected, result);

            A.CallTo(() => commandBus.PublishAsync(
                         A <CreateContent> .That.Matches(x =>
                                                         x.SchemaId.Equals(schemaId) &&
                                                         x.ExpectedVersion == EtagVersion.Any &&
                                                         x.Data.Equals(content.Data))))
            .MustHaveHappened();
        }
Пример #3
0
        public async Task HandleAsync(CommandContext context, NextDelegate next)
        {
            if (context.Command is AssignContributor assignContributor && ShouldResolve(assignContributor))
            {
                IUser?user;

                var created = false;

                if (assignContributor.Invite)
                {
                    (user, created) = await userResolver.CreateUserIfNotExistsAsync(assignContributor.ContributorId, true);
                }
                else
                {
                    user = await userResolver.FindByIdOrEmailAsync(assignContributor.ContributorId);
                }

                if (user != null)
                {
                    assignContributor.ContributorId = user.Id;
                }

                await next(context);

                if (created && context.PlainResult is IAppEntity app)
                {
                    context.Complete(new InvitedResult {
                        App = app
                    });
                }
            }
Пример #4
0
        protected Task On(ChangePlan command, CommandContext context)
        {
            if (!appPlansProvider.IsConfiguredPlan(command.PlanId))
            {
                var error =
                    new ValidationError($"The plan '{command.PlanId}' does not exists",
                                        nameof(CreateApp.Name));

                throw new ValidationException("Cannot change plan", error);
            }

            return(handler.UpdateAsync <AppDomainObject>(context, async a =>
            {
                if (command.FromCallback)
                {
                    a.ChangePlan(command);
                }
                else
                {
                    var result = await appPlansBillingManager.ChangePlanAsync(command.Actor.Identifier, a.Id, a.Name, command.PlanId);

                    if (result is PlanChangedResult)
                    {
                        a.ChangePlan(command);
                    }

                    context.Complete(result);
                }
            }));
        }
Пример #5
0
        private async Task ExecuteCommandAsync(CommandContext context, CommentsCommand commentsCommand)
        {
            var grain = grainFactory.GetGrain <ICommentsGrain>(commentsCommand.CommentsId);

            var result = await grain.ExecuteAsync(commentsCommand.AsJ());

            context.Complete(result.Value);
        }
        protected Task On(AddField command, CommandContext context)
        {
            return(handler.UpdateAsync <SchemaDomainObject>(context, s =>
            {
                s.AddField(command);

                context.Complete(EntityCreatedResult.Create(s.Schema.FieldsById.Values.First(x => x.Name == command.Name).Id, s.Version));
            }));
        }
        protected async Task On(CreateContent command, CommandContext context)
        {
            await ValidateAsync(command, () => "Failed to create content", true);

            await handler.CreateAsync <ContentDomainObject>(context, c =>
            {
                c.Create(command);

                context.Complete(EntityCreatedResult.Create(command.Data, c.Version));
            });
        }
Пример #8
0
        public override async Task HandleAsync(CommandContext context, NextDelegate next)
        {
            await base.HandleAsync(context, next);

            if (context.PlainResult is IRuleEntity rule && NotEnriched(context))
            {
                var enriched = await ruleEnricher.EnrichAsync(rule, contextProvider.Context);

                context.Complete(enriched);
            }
        }
Пример #9
0
        private async Task HandleCoreAsync(CommandContext context, NextDelegate next)
        {
            await base.HandleAsync(context, next);

            if (context.PlainResult is IAssetEntity asset && !(context.PlainResult is IEnrichedAssetEntity))
            {
                var enriched = await assetEnricher.EnrichAsync(asset, contextProvider.Context);

                context.Complete(enriched);
            }
        }
        public override async Task HandleAsync(CommandContext context, Func <Task> next)
        {
            await base.HandleAsync(context, next);

            if (context.PlainResult is IContentEntity content && NotEnriched(context))
            {
                var enriched = await contentEnricher.EnrichAsync(content, contextProvider.Context);

                context.Complete(enriched);
            }
        }
Пример #11
0
        protected Task On(AddField command, CommandContext context)
        {
            return(handler.UpdateSyncedAsync <SchemaDomainObject>(context, s =>
            {
                GuardSchemaField.CanAdd(s.Snapshot.SchemaDef, command);

                s.Add(command);

                context.Complete(EntityCreatedResult.Create(s.Snapshot.SchemaDef.FieldsById.Values.First(x => x.Name == command.Name).Id, s.Version));
            }));
        }
Пример #12
0
        protected Task On(CreateSchema command, CommandContext context)
        {
            return(handler.CreateSyncedAsync <SchemaDomainObject>(context, async s =>
            {
                await GuardSchema.CanCreate(command, appProvider);

                s.Create(command);

                context.Complete(EntityCreatedResult.Create(command.SchemaId, s.Version));
            }));
        }
Пример #13
0
        protected async Task On(CreateApp command, CommandContext context)
        {
            var app = await handler.CreateSyncedAsync <AppDomainObject>(context, async a =>
            {
                await GuardApp.CanCreate(command, appProvider);

                a.Create(command);

                context.Complete(EntityCreatedResult.Create(command.AppId, a.Version));
            });
        }
Пример #14
0
        public async Task Should_add_etag_header_to_response()
        {
            var command = new CreateContent();
            var context = new CommandContext(command, commandBus);

            context.Complete(new EntitySavedResult(17));

            await sut.HandleAsync(context);

            Assert.Equal(new StringValues("17"), httpContextAccessor.HttpContext.Response.Headers[HeaderNames.ETag]);
        }
Пример #15
0
        private async Task <IEnrichedAssetEntity?> HandleCoreAsync(CommandContext context, bool created, NextDelegate next)
        {
            await base.HandleAsync(context, next);

            if (context.PlainResult is IAssetEntity asset && !(context.PlainResult is IEnrichedAssetEntity))
            {
                var enriched = await assetEnricher.EnrichAsync(asset, contextProvider.Context);

                if (created)
                {
                    context.Complete(new AssetCreatedResult(enriched, false));
                }
                else
                {
                    context.Complete(enriched);
                }

                return(enriched);
            }

            return(null);
        }
Пример #16
0
        public async Task Should_add_schema_to_index_on_create()
        {
            var command = new CreateSchema {
                SchemaId = schemaId.Id, Name = schemaId.Name, AppId = appId
            };
            var context = new CommandContext(command, commandBus);

            context.Complete();

            await sut.HandleAsync(context);

            A.CallTo(() => index.AddSchemaAsync(schemaId.Id, schemaId.Name))
            .MustHaveHappened();
        }
Пример #17
0
        public async Task HandleAsync(CommandContext context, Func <Task> next)
        {
            if (context.Command is AssignContributor assignContributor && ShouldInvite(assignContributor))
            {
                var created = await userResolver.CreateUserIfNotExists(assignContributor.ContributorId, true);

                await next();

                if (created && context.PlainResult is IAppEntity app)
                {
                    context.Complete(new InvitedResult {
                        App = app
                    });
                }
            }
Пример #18
0
        protected async Task On(UpdateContent command, CommandContext context)
        {
            await handler.UpdateAsync <ContentDomainObject>(context, async content =>
            {
                var schemaAndApp = await ResolveSchemaAndAppAsync(command);

                ExecuteScriptAndTransform(command, content, schemaAndApp.SchemaEntity.ScriptUpdate, "Update");

                await ValidateAsync(schemaAndApp, command, () => "Failed to update content", false);

                content.Update(command);

                context.Complete(new ContentDataChangedResult(content.Data, content.Version));
            });
        }
Пример #19
0
        protected async Task On(PatchContent command, CommandContext context)
        {
            await handler.UpdateAsync <ContentDomainObject>(context, async content =>
            {
                GuardContent.CanPatch(command);

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

                await operationContext.ValidateAsync(true);
                await operationContext.ExecuteScriptAndTransformAsync(x => x.ScriptUpdate, "Patch");

                content.Patch(command);

                context.Complete(new ContentDataChangedResult(content.Snapshot.Data, content.Version));
            });
        }
        protected async Task On(CreateSchema command, CommandContext context)
        {
            if (await schemas.FindSchemaByNameAsync(command.AppId.Id, command.Name) != null)
            {
                var error =
                    new ValidationError($"A schema with name '{command.Name}' already exists", "Name",
                                        nameof(CreateSchema.Name));

                throw new ValidationException("Cannot create a new schema", error);
            }

            await handler.CreateAsync <SchemaDomainObject>(context, s =>
            {
                s.Create(command);

                context.Complete(EntityCreatedResult.Create(s.Id, s.Version));
            });
        }
Пример #21
0
        protected async Task On(CreateApp command, CommandContext context)
        {
            if (await appRepository.FindAppAsync(command.Name) != null)
            {
                var error =
                    new ValidationError($"An app with name '{command.Name}' already exists",
                                        nameof(CreateApp.Name));

                throw new ValidationException("Cannot create a new app", error);
            }

            await handler.CreateAsync <AppDomainObject>(context, a =>
            {
                a.Create(command);

                context.Complete(EntityCreatedResult.Create(a.Id, a.Version));
            });
        }
Пример #22
0
        public async Task HandleAsync(CommandContext context, NextDelegate next)
        {
            if (context.Command is CreateContents createContents)
            {
                var result = new ImportResult();

                if (createContents.Datas != null && createContents.Datas.Count > 0)
                {
                    var command = SimpleMapper.Map(createContents, new CreateContent());

                    foreach (var data in createContents.Datas)
                    {
                        try
                        {
                            command.ContentId = Guid.NewGuid();
                            command.Data      = data;

                            var content = serviceProvider.GetRequiredService <ContentDomainObject>();

                            content.Setup(command.ContentId);

                            await content.ExecuteAsync(command);

                            result.Add(new ImportResultItem {
                                ContentId = command.ContentId
                            });
                        }
                        catch (Exception ex)
                        {
                            result.Add(new ImportResultItem {
                                Exception = ex
                            });
                        }
                    }
                }

                context.Complete(result);
            }
            else
            {
                await next(context);
            }
        }
        public async Task HandleAsync(CommandContext context, NextDelegate next)
        {
            if (context.Command is AssignContributor assignContributor && ShouldInvite(assignContributor))
            {
                var(user, created) = await userResolver.CreateUserIfNotExistsAsync(assignContributor.ContributorId, true);

                if (user != null)
                {
                    assignContributor.ContributorId = user.Id;
                }

                await next(context);

                if (created && context.PlainResult is IAppEntity app)
                {
                    context.Complete(new InvitedResult {
                        App = app
                    });
                }
            }
        public async Task HandleAsync(CommandContext context, NextDelegate next)
        {
            await next(context);

            if (context.IsCompleted && context.Command is CreateApp createApp)
            {
                var appId = NamedId.Of(createApp.AppId, createApp.Name);

                var publish = new Func <IAppCommand, Task>(async command =>
                {
                    command.AppId = appId;

                    var newContext = await context.CommandBus.PublishAsync(command);

                    context.Complete(newContext.PlainResult);
                });

                await publish(new AttachClient { Id = "default", Role = Role.Owner });
            }
        }
        public async Task Should_invite_user_and_change_result()
        {
            var command = new AssignContributor {
                ContributorId = "*****@*****.**", IsInviting = true
            };
            var context = new CommandContext(command, commandBus);

            A.CallTo(() => userResolver.CreateUserIfNotExists("*****@*****.**", true))
            .Returns(true);

            var result = A.Fake <IAppEntity>();

            context.Complete(result);

            await sut.HandleAsync(context);

            Assert.Same(context.Result <InvitedResult>().App, result);

            A.CallTo(() => userResolver.CreateUserIfNotExists("*****@*****.**", true))
            .MustHaveHappened();
        }
        public async Task Should_invite_user_and_not_change_result_if_not_added()
        {
            var command = new AssignContributor {
                ContributorId = "*****@*****.**", IsInviting = true
            };
            var context = new CommandContext(command, commandBus);

            A.CallTo(() => userResolver.CreateUserIfNotExists("*****@*****.**", true))
            .Returns(false);

            var result = Mocks.App(NamedId.Of(Guid.NewGuid(), "my-app"));

            context.Complete(result);

            await sut.HandleAsync(context);

            Assert.Same(context.Result <IAppEntity>(), result);

            A.CallTo(() => userResolver.CreateUserIfNotExists("*****@*****.**", true))
            .MustHaveHappened();
        }
        public async Task Should_invite_user_and_not_change_result_if_not_added()
        {
            var command = new AssignContributor {
                ContributorId = "*****@*****.**", IsInviting = true
            };
            var context = new CommandContext(command, commandBus);

            A.CallTo(() => userResolver.CreateUserIfNotExists("*****@*****.**"))
            .Returns(false);

            var result = EntityCreatedResult.Create("13", 13L);

            context.Complete(result);

            await sut.HandleAsync(context);

            Assert.Same(context.Result <EntityCreatedResult <string> >(), result);

            A.CallTo(() => userResolver.CreateUserIfNotExists("*****@*****.**"))
            .MustHaveHappened();
        }
Пример #28
0
        protected async Task On(CreateContent command, CommandContext context)
        {
            await handler.CreateAsync <ContentDomainObject>(context, async content =>
            {
                GuardContent.CanCreate(command);

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

                if (command.Publish)
                {
                    await operationContext.ExecuteScriptAsync(x => x.ScriptChange, "Published");
                }

                await operationContext.ExecuteScriptAndTransformAsync(x => x.ScriptCreate, "Create");
                await operationContext.EnrichAsync();
                await operationContext.ValidateAsync(false);

                content.Create(command);

                context.Complete(EntityCreatedResult.Create(command.Data, content.Version));
            });
        }
Пример #29
0
        protected async Task On(CreateContent command, CommandContext context)
        {
            await handler.CreateAsync <ContentDomainObject>(context, async content =>
            {
                var schemaAndApp = await ResolveSchemaAndAppAsync(command);

                ExecuteScriptAndTransform(command, content, schemaAndApp.SchemaEntity.ScriptCreate, "Create");

                if (command.Publish)
                {
                    ExecuteScript(command, content, schemaAndApp.SchemaEntity.ScriptChange, "Published");
                }

                command.Data.Enrich(schemaAndApp.SchemaEntity.SchemaDef, schemaAndApp.AppEntity.PartitionResolver);

                await ValidateAsync(schemaAndApp, command, () => "Failed to create content", false);

                content.Create(command);

                context.Complete(EntityCreatedResult.Create(command.Data, content.Version));
            });
        }
Пример #30
0
        protected async Task On(UpdateAsset command, CommandContext context)
        {
            command.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(command.File.OpenRead());

            try
            {
                var asset = await handler.UpdateAsync <AssetDomainObject>(context, async a =>
                {
                    a.Update(command);

                    await assetStore.UploadTemporaryAsync(context.ContextId.ToString(), command.File.OpenRead());

                    context.Complete(new AssetSavedResult(a.Version, a.FileVersion));
                });

                await assetStore.CopyTemporaryAsync(context.ContextId.ToString(), asset.Id.ToString(), asset.FileVersion, null);
            }
            finally
            {
                await assetStore.DeleteTemporaryAsync(context.ContextId.ToString());
            }
        }