Example #1
0
        private static async Task <NamedId <Guid> > CreatePagesSchemaAsync(Func <ICommand, Task> publish)
        {
            var schema =
                SchemaBuilder.Create("Pages")
                .AddString("Title", f => f
                           .Length(100)
                           .Required()
                           .ShowInList()
                           .Hints("The title of the page."))
                .AddString("Text", f => f
                           .AsRichText()
                           .Length(100)
                           .Required()
                           .Hints("The text of the page."))
                .AddString("Slug", f => f
                           .Disabled()
                           .Label("Slug (Autogenerated)")
                           .Hints("Autogenerated slug that can be used to identity the page."))
                .Build();

            await publish(schema);

            var schemaId = NamedId.Of(schema.SchemaId, schema.Name);

            await publish(new ConfigureScripts
            {
                SchemaId     = schemaId.Id,
                ScriptCreate = Scripts.Slug,
                ScriptUpdate = Scripts.Slug
            });

            return(schemaId);
        }
Example #2
0
        public async Task HandleAsync(CommandContext context, NextDelegate next)
        {
            await next(context);

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

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

                    return(context.CommandBus.PublishAsync(command));
                });

                var schema =
                    SchemaBuilder.Create("Pages")
                    .AddString("Title", f => f
                               .Length(100)
                               .Required())
                    .AddString("Slug", f => f
                               .Length(100)
                               .Required()
                               .Disabled())
                    .AddString("Text", f => f
                               .Length(1000)
                               .Required()
                               .AsRichText())
                    .Build();

                await publish(schema);
            }
        }
Example #3
0
        private static async Task <NamedId <Guid> > CreatePostsSchemaAsync(Func <ICommand, Task> publish)
        {
            var schema =
                SchemaBuilder.Create("Posts")
                .AddString("Title", f => f
                           .Length(100)
                           .Required()
                           .ShowInList()
                           .Hints("The title of the post."))
                .AddString("Text", f => f
                           .AsRichText()
                           .Length(100)
                           .Required()
                           .Hints("The text of the post."))
                .AddString("Slug", f => f
                           .Disabled()
                           .Label("Slug (Autogenerated)")
                           .Hints("Autogenerated slug that can be used to identity the post."))
                .WithScripts(DefaultScripts.GenerateSlug)
                .Build();

            await publish(schema);

            return(NamedId.Of(schema.SchemaId, schema.Name));
        }
Example #4
0
        public async Task Should_compute_cache_independent_from_db()
        {
            var app = CreateApp(DomainId.NewGuid(), 1);

            var schema1 = CreateSchema(DomainId.NewGuid(), 2);
            var schema2 = CreateSchema(DomainId.NewGuid(), 3);

            var timestamp = SystemClock.Instance.GetCurrentInstant().WithoutMs();

            var computedHash = await _.SchemasHash.ComputeHashAsync(app, new[] { schema1, schema2 });

            await _.SchemasHash.On(new[]
            {
                Envelope.Create <IEvent>(new SchemaCreated
                {
                    AppId    = NamedId.Of(app.Id, "my-app"),
                    SchemaId = NamedId.Of(schema1.Id, "my-schema")
                }).SetEventStreamNumber(schema1.Version).SetTimestamp(timestamp),

                Envelope.Create <IEvent>(new SchemaCreated
                {
                    AppId    = NamedId.Of(app.Id, "my-app"),
                    SchemaId = NamedId.Of(schema2.Id, "my-schema")
                }).SetEventStreamNumber(schema2.Version).SetTimestamp(timestamp)
            });

            var(dbTime, dbHash) = await _.SchemasHash.GetCurrentHashAsync(app);

            Assert.Equal(dbHash, computedHash);
            Assert.Equal(dbTime, timestamp);
        }
        public async Task HandleAsync(CommandContext context, NextDelegate next)
        {
            if (context.IsCompleted && context.Command is CreateApp createApp && IsRightTemplate(createApp))
            {
                var appId = NamedId.Of(createApp.AppId, createApp.Name);

                var publish = new Func <ICommand, Task>(command =>
                {
                    if (command is IAppCommand appCommand)
                    {
                        appCommand.AppId = appId;
                    }

                    return(context.CommandBus.PublishAsync(command));
                });

                await Task.WhenAll(
                    CreateApiResourcesSchemaAsync(publish),
                    CreateAuthenticationSchemeSchemaAsync(publish),
                    CreateClientsSchemaAsync(publish),
                    CreateIdentityResourcesSchemaAsync(publish),
                    CreateSettingsSchemaAsync(publish),
                    CreateUsersSchemaAsync(publish));
            }

            await next(context);
        }
        public async Task HandleAsync(CommandContext context, NextDelegate next)
        {
            await next(context);

            if (context.IsCompleted && context.Command is CreateSchema {
                Type : SchemaType.Singleton
            } createSchema)
            {
                var schemaId = NamedId.Of(createSchema.SchemaId, createSchema.Name);

                var data = new ContentData();

                var contentId = schemaId.Id;
                var content   = new CreateContent
                {
                    Data          = data,
                    ContentId     = contentId,
                    DoNotScript   = true,
                    DoNotValidate = true,
                    SchemaId      = schemaId,
                    Status        = Status.Published
                };

                SimpleMapper.Map(createSchema, content);

                await context.CommandBus.PublishAsync(content);
            }
        }
Example #7
0
 protected TextIndexerTestsBase()
 {
     app =
         Mocks.App(NamedId.Of(DomainId.NewGuid(), "my-app"),
                   Language.DE,
                   Language.EN);
 }
        public async Task HandleAsync(CommandContext context, Func <Task> next)
        {
            if (context.IsCompleted && context.Command is CreateApp createApp && IsRightTemplate(createApp))
            {
                var appId = NamedId.Of(createApp.AppId, createApp.Name);

                var publish = new Func <ICommand, Task>(command =>
                {
                    if (command is IAppCommand appCommand)
                    {
                        appCommand.AppId = appId;
                    }

                    return(context.CommandBus.PublishAsync(command));
                });

                await Task.WhenAll(
                    CreateBasicsAsync(publish),
                    CreateEducationSchemaAsync(publish),
                    CreateExperienceSchemaAsync(publish),
                    CreateProjectsSchemaAsync(publish),
                    CreatePublicationsSchemaAsync(publish),
                    CreateSkillsSchemaAsync(publish));
            }

            await next();
        }
        public void Should_create_events_if_nested_fields_reordered()
        {
            var id1 = NamedId.Of(1, "f1");
            var id2 = NamedId.Of(2, "f1");

            var sourceSchema =
                new Schema("source")
                .AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
                          .AddString(10, "f1")
                          .AddString(11, "f2"));

            var targetSchema =
                new Schema("target")
                .AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
                          .AddString(1, "f2")
                          .AddString(2, "f1"));

            var events = sourceSchema.Synchronize(targetSchema, idGenerator);

            events.ShouldHaveSameEvents(
                new SchemaFieldsReordered {
                FieldIds = new List <long> {
                    11, 10
                }, ParentFieldId = arrayId
            }
                );
        }
        private static async Task <NamedId <Guid> > CreateEducationSchemaAsync(Func <ICommand, Task> publish)
        {
            var schema =
                SchemaBuilder.Create("Education")
                .AddString("Degree", f => f
                           .Required()
                           .ShowInList()
                           .Hints("The degree you got or achieved."))
                .AddString("School", f => f
                           .Required()
                           .ShowInList()
                           .Hints("The school or university."))
                .AddAssets("Logo", f => f
                           .MustBeImage()
                           .Hints("The logo of the school or university."))
                .AddDateTime("From", f => f
                             .Required()
                             .Hints("The start date."))
                .AddDateTime("To", f => f
                             .Hints("The end date, keep empty if you still study there."))
                .Build();

            await publish(schema);

            return(NamedId.Of(schema.SchemaId, schema.Name));
        }
        private static async Task <NamedId <Guid> > CreateExperienceSchemaAsync(Func <ICommand, Task> publish)
        {
            var schema =
                SchemaBuilder.Create("Experience")
                .AddString("Position", f => f
                           .Required()
                           .ShowInList()
                           .Hints("Your position in this job."))
                .AddString("Company", f => f
                           .Required()
                           .ShowInList()
                           .Hints("The company or organization you worked for."))
                .AddAssets("Logo", f => f
                           .MustBeImage()
                           .Hints("The logo of the company or organization you worked for."))
                .AddDateTime("From", f => f
                             .Required()
                             .Hints("The start date."))
                .AddDateTime("To", f => f
                             .Hints("The end date, keep empty if you still work there."))
                .Build();

            await publish(schema);

            return(NamedId.Of(schema.SchemaId, schema.Name));
        }
        private static async Task <NamedId <Guid> > CreateProjectsSchemaAsync(Func <ICommand, Task> publish)
        {
            var schema =
                SchemaBuilder.Create("Projects")
                .AddString("Name", f => f
                           .Required()
                           .ShowInList()
                           .Hints("The name of your project."))
                .AddString("Description", f => f
                           .AsTextArea()
                           .Required()
                           .Hints("Describe your project."))
                .AddAssets("Image", f => f
                           .MustBeImage()
                           .Required()
                           .Hints("An image or screenshot for your project."))
                .AddString("Label", f => f
                           .AsTextArea()
                           .Hints("An optional label to categorize your project, e.g. 'Open Source'."))
                .AddString("Link", f => f
                           .Hints("An optional link to your project."))
                .AddNumber("Year", f => f
                           .Hints("The year, when you realized the project, used for sorting only."))
                .Build();

            await publish(schema);

            return(NamedId.Of(schema.SchemaId, schema.Name));
        }
Example #13
0
        public async Task Should_not_create_job_if_too_old()
        {
            var e = new ContentCreated {
                SchemaId = NamedId.Of(Guid.NewGuid(), "my-schema"), AppId = NamedId.Of(Guid.NewGuid(), "my-event")
            };

            var now = SystemClock.Instance.GetCurrentInstant();

            var ruleConfig   = new Rule(new ContentChangedTrigger(), new WebhookAction());
            var ruleEnvelope = Envelope.Create(e);

            ruleEnvelope.SetTimestamp(now.Minus(Duration.FromDays(3)));

            var actionData        = new JObject();
            var actionDescription = "MyDescription";

            var eventName = "MySchemaCreatedEvent";

            A.CallTo(() => clock.GetCurrentInstant())
            .Returns(now);

            A.CallTo(() => ruleTriggerHandler.Triggers(A <Envelope <AppEvent> > .Ignored, ruleConfig.Trigger))
            .Returns(true);

            A.CallTo(() => ruleActionHandler.CreateJobAsync(A <Envelope <AppEvent> > .Ignored, eventName, ruleConfig.Action))
            .Returns((actionDescription, actionData));

            var job = await sut.CreateJobAsync(ruleConfig, ruleEnvelope);

            Assert.Null(job);
        }
Example #14
0
        private Envelope <IEvent> E(ContentEvent contentEvent)
        {
            contentEvent.ContentId = contentId;
            contentEvent.SchemaId  = NamedId.Of(schemaId, "my-schema");

            return(new Envelope <IEvent>(contentEvent));
        }
Example #15
0
        public void Should_create_events_if_nested_field_created()
        {
            var sourceSchema =
                new Schema("source");

            var targetSchema =
                new Schema("target")
                .AddArray(arrayId.Id, arrayId.Name, Partitioning.Invariant, f => f
                          .AddString(nestedId.Id, nestedId.Name))
                .HideField(nestedId.Id, arrayId.Id);

            var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);

            var id1 = NamedId.Of(50L, arrayId.Name);
            var id2 = NamedId.Of(51L, stringId.Name);

            events.ShouldHaveSameEvents(
                new FieldAdded {
                FieldId = id1, Name = arrayId.Name, Partitioning = Partitioning.Invariant.Key, Properties = new ArrayFieldProperties()
            },
                new FieldAdded {
                FieldId = id2, Name = stringId.Name, ParentFieldId = id1, Properties = new StringFieldProperties()
            },
                new FieldHidden {
                FieldId = id2, ParentFieldId = id1
            }
                );
        }
Example #16
0
        public void Should_create_events_if_fields_reordered_after_sync()
        {
            var id1 = NamedId.Of(1, "f1");
            var id2 = NamedId.Of(2, "f1");

            var sourceSchema =
                new Schema("source")
                .AddString(10, "f1", Partitioning.Invariant)
                .AddString(11, "f2", Partitioning.Invariant);

            var targetSchema =
                new Schema("target")
                .AddString(1, "f3", Partitioning.Invariant)
                .AddString(2, "f1", Partitioning.Invariant);

            var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);

            events.ShouldHaveSameEvents(
                new FieldDeleted {
                FieldId = NamedId.Of(11L, "f2")
            },
                new FieldAdded {
                FieldId = NamedId.Of(50L, "f3"), Name = "f3", Partitioning = Partitioning.Invariant.Key, Properties = new StringFieldProperties()
            },
                new SchemaFieldsReordered {
                FieldIds = new List <long> {
                    50, 10
                }
            }
                );
        }
Example #17
0
        public void Should_create_events_if_fields_reordered()
        {
            var id1 = NamedId.Of(1, "f1");
            var id2 = NamedId.Of(2, "f1");

            var sourceSchema =
                new Schema("source")
                .AddString(10, "f1", Partitioning.Invariant)
                .AddString(11, "f2", Partitioning.Invariant);

            var targetSchema =
                new Schema("target")
                .AddString(1, "f2", Partitioning.Invariant)
                .AddString(2, "f1", Partitioning.Invariant);

            var events = sourceSchema.Synchronize(targetSchema, jsonSerializer, idGenerator);

            events.ShouldHaveSameEvents(
                new SchemaFieldsReordered {
                FieldIds = new List <long> {
                    11, 10
                }
            }
                );
        }
Example #18
0
        public void Create(CreateApp command)
        {
            var appId = NamedId.Of(command.AppId, command.Name);

            var events = new List <AppEvent>
            {
                CreateInitalEvent(command.Name),
                CreateInitialLanguage()
            };

            if (command.Actor.IsSubject)
            {
                events.Add(CreateInitialOwner(command.Actor));
            }

            foreach (var(key, value) in initialPatterns)
            {
                events.Add(CreateInitialPattern(key, value));
            }

            foreach (var @event in events)
            {
                @event.Actor = command.Actor;
                @event.AppId = appId;

                RaiseEvent(@event);
            }
        }
Example #19
0
        public void Create(CreateSchema command)
        {
            var @event = SimpleMapper.Map(command, new SchemaCreated {
                SchemaId = NamedId.Of(command.SchemaId, command.Name)
            });

            if (command.Fields != null)
            {
                @event.Fields = new List <SchemaCreatedField>();

                foreach (var commandField in command.Fields)
                {
                    var eventField = SimpleMapper.Map(commandField, new SchemaCreatedField());

                    @event.Fields.Add(eventField);

                    if (commandField.Nested != null)
                    {
                        eventField.Nested = new List <SchemaCreatedNestedField>();

                        foreach (var nestedField in commandField.Nested)
                        {
                            var eventNestedField = SimpleMapper.Map(nestedField, new SchemaCreatedNestedField());

                            eventField.Nested.Add(eventNestedField);
                        }
                    }
                }
            }

            RaiseEvent(@event);
        }
        public async Task Should_not_override_app_id_and_name()
        {
            var customId = NamedId.Of(DomainId.NewGuid(), "other-app");

            var context = await HandleAsync(new CreateContent { AppId = customId });

            Assert.Equal(customId, ((IAppCommand)context.Command).AppId);
        }
Example #21
0
        public async Task Should_remove_events_from_streams()
        {
            var app = Mocks.App(NamedId.Of(appId, "my-app"));

            await((IDeleter)sut).DeleteAppAsync(app, ct);

            A.CallTo(() => requestLogStore.DeleteAsync($"^[a-z]-{app.Id}", ct))
            .MustNotHaveHappened();
        }
Example #22
0
        public async Task Should_remove_events_from_streams()
        {
            var app = Mocks.App(NamedId.Of(DomainId.NewGuid(), "my-app"));

            await sut.DeleteAppAsync(app, ct);

            A.CallTo(() => eventStore.DeleteAsync($"^[a-zA-Z0-9]-{app.Id}", ct))
            .MustNotHaveHappened();
        }
Example #23
0
        public async Task Should_remove_events_from_streams()
        {
            var app = Mocks.App(NamedId.Of(DomainId.NewGuid(), "my-app"));

            await sut.DeleteAppAsync(app, ct);

            A.CallTo(() => tagService.ClearAsync(app.Id, TagGroups.Assets))
            .MustHaveHappened();
        }
Example #24
0
        public async Task Should_clear_grain_when_contributor_removed()
        {
            var app = Mocks.App(NamedId.Of(DomainId.NewGuid(), "my-app"));

            await((IDeleter)sut).DeleteContributorAsync(app.Id, "user1", default);

            A.CallTo(() => grain.ClearAsync())
            .MustHaveHappened();
        }
Example #25
0
        public async Task Should_clear_grain_when_app_deleted()
        {
            var app = Mocks.App(NamedId.Of(DomainId.NewGuid(), "my-app"));

            await((IDeleter)sut).DeleteAppAsync(app, default);

            A.CallTo(() => grain.ClearAsync())
            .MustHaveHappened();
        }
Example #26
0
        private void RaiseEvent(AppEvent @event)
        {
            if (@event.AppId == null)
            {
                @event.AppId = NamedId.Of(Snapshot.Id, Snapshot.Name);
            }

            RaiseEvent(Envelope.Create(@event));
        }
Example #27
0
        private NamedId <long> GetFieldId(long?id)
        {
            if (id.HasValue && Snapshot.SchemaDef.FieldsById.TryGetValue(id.Value, out var field))
            {
                return(NamedId.Of(field.Id, field.Name));
            }

            return(null);
        }
Example #28
0
        private static ISchemaEntity CreateSchema(bool validateOnPublish)
        {
            var schema = new Schema("my-schema", new SchemaProperties
            {
                ValidateOnPublish = validateOnPublish
            });

            return(Mocks.Schema(NamedId.Of(DomainId.NewGuid(), "my-app"), NamedId.Of(DomainId.NewGuid(), schema.Name), schema));
        }
Example #29
0
        public async Task Should_remove_events_from_streams()
        {
            var app = Mocks.App(NamedId.Of(DomainId.NewGuid(), "my-app"));

            await sut.DeleteAppAsync(app, ct);

            A.CallTo(() => usageTracker.DeleteAsync(app.Id.ToString(), ct))
            .MustHaveHappened();
        }
Example #30
0
        public async Task Should_not_override_schema_id_and_name()
        {
            httpContext.Features.Set <ISchemaFeature>(new SchemaFeature(Mocks.Schema(appId, schemaId)));

            var customId = NamedId.Of(DomainId.NewGuid(), "other-app");

            var context = await HandleAsync(new CreateContent { SchemaId = customId });

            Assert.Equal(customId, ((ISchemaCommand)context.Command).SchemaId);
        }