Exemplo n.º 1
0
        public async Task <IEnrichedContentEntity?> FindAsync(Context context, string schemaIdOrName, DomainId id, long version = EtagVersion.Any,
                                                              CancellationToken ct = default)
        {
            Guard.NotNull(context);

            using (Telemetry.Activities.StartActivity("ContentQueryService/FindAsync"))
            {
                var schema = await GetSchemaOrThrowAsync(context, schemaIdOrName, ct);

                IContentEntity?content;

                if (id.ToString().Equals(SingletonId, StringComparison.Ordinal))
                {
                    id = schema.Id;
                }

                if (version > EtagVersion.Empty)
                {
                    content = await contentLoader.GetAsync(context.App.Id, id, version);
                }
                else
                {
                    content = await FindCoreAsync(context, id, schema, ct);
                }

                if (content == null || content.SchemaId.Id != schema.Id)
                {
                    return(null);
                }

                return(await TransformAsync(context, content, ct));
            }
        }
Exemplo n.º 2
0
        public async Task <IEnrichedContentEntity?> FindAsync(Context context, string schemaIdOrName, DomainId id, long version = EtagVersion.Any)
        {
            Guard.NotNull(context, nameof(context));

            using (Profiler.TraceMethod <ContentQueryService>())
            {
                var schema = await GetSchemaOrThrowAsync(context, schemaIdOrName);

                IContentEntity?content;

                if (version > EtagVersion.Empty)
                {
                    content = await contentLoader.GetAsync(context.App.Id, id, version);
                }
                else
                {
                    content = await contentRepository.FindContentAsync(context.App, schema, id, context.Scope());
                }

                if (content == null || content.SchemaId.Id != schema.Id)
                {
                    return(null);
                }

                return(await TransformAsync(context, content));
            }
        }
        public async Task Should_create_enriched_events(ContentEvent @event, EnrichedContentEventType type)
        {
            @event.AppId = appId;

            var envelope = Envelope.Create <AppEvent>(@event).SetEventStreamNumber(12);

            A.CallTo(() => contentLoader.GetAsync(appId.Id, @event.ContentId, 12))
            .Returns(new ContentEntity {
                AppId = appId, SchemaId = schemaMatch
            });

            var result = await sut.CreateEnrichedEventsAsync(envelope);

            var enrichedEvent = result.Single() as EnrichedContentEvent;

            Assert.Equal(type, enrichedEvent !.Type);
        }
        public async Task Should_return_content_by_id_and_version()
        {
            var requestContext = CreateContext();

            var content = CreateContent(DomainId.NewGuid());

            A.CallTo(() => contentVersionLoader.GetAsync(appId.Id, content.Id, 13))
            .Returns(content);

            var result = await sut.FindAsync(requestContext, schemaId.Name, content.Id, 13);

            AssertContent(content, result);
        }
        public async Task Should_enrich_events(ContentEvent @event, EnrichedContentEventType type)
        {
            var envelope = Envelope.Create <AppEvent>(@event).SetEventStreamNumber(12);

            A.CallTo(() => contentLoader.GetAsync(@event.ContentId, 12))
            .Returns(new ContentEntity {
                SchemaId = SchemaMatch
            });

            var result = await sut.CreateEnrichedEventAsync(envelope) as EnrichedContentEvent;

            Assert.Equal(type, result !.Type);
        }
Exemplo n.º 6
0
        public async Task FindContentAsync_should_return_content_by_version()
        {
            var ctx = CreateContext(isFrontend: false, allowSchema: true);

            var content = CreateContent(contentId);

            A.CallTo(() => contentVersionLoader.GetAsync(appId.Id, contentId, 13))
            .Returns(content);

            var result = await sut.FindAsync(ctx, schemaId.Name, contentId, 13);

            Assert.Equal(contentTransformed, result !.Data);
            Assert.Equal(content.Id, result.Id);
        }
        protected override async Task <EnrichedContentEvent?> CreateEnrichedEventAsync(Envelope <ContentEvent> @event)
        {
            var result = new EnrichedContentEvent();

            var content = await contentLoader.GetAsync(@event.Headers.AggregateId(), @event.Headers.EventStreamNumber());

            SimpleMapper.Map(content, result);

            result.Data = content.Data ?? content.DataDraft;

            switch (@event.Payload)
            {
            case ContentCreated _:
                result.Type = EnrichedContentEventType.Created;
                break;

            case ContentDeleted _:
                result.Type = EnrichedContentEventType.Deleted;
                break;

            case ContentChangesPublished _:
            case ContentUpdated _:
                result.Type = EnrichedContentEventType.Updated;
                break;

            case ContentStatusChanged contentStatusChanged:
                switch (contentStatusChanged.Change)
                {
                case StatusChange.Published:
                    result.Type = EnrichedContentEventType.Published;
                    break;

                case StatusChange.Unpublished:
                    result.Type = EnrichedContentEventType.Unpublished;
                    break;

                default:
                    result.Type = EnrichedContentEventType.StatusChanged;
                    break;
                }

                break;
            }

            result.Name = $"{content.SchemaId.Name.ToPascalCase()}{result.Type}";

            return(result);
        }
Exemplo n.º 8
0
        public async Task Should_return_versioned_content_from_repository_and_transform()
        {
            var content = CreateContent(contentId);

            SetupUser(true);
            SetupSchemaFound();
            SetupSchemaScripting(contentId);

            A.CallTo(() => contentVersionLoader.GetAsync(contentId, 10))
            .Returns(content);

            var ctx = requestContext;

            var result = await sut.FindContentAsync(ctx, schemaId.Name, contentId, 10);

            Assert.Equal(contentTransformed, result.Data);
            Assert.Equal(content.Id, result.Id);
        }
Exemplo n.º 9
0
        protected override async Task <EnrichedContentEvent?> CreateEnrichedEventAsync(Envelope <ContentEvent> @event)
        {
            var result = new EnrichedContentEvent();

            var content =
                await contentLoader.GetAsync(
                    @event.Payload.AppId.Id,
                    @event.Payload.ContentId,
                    @event.Headers.EventStreamNumber());

            if (content == null)
            {
                throw new DomainObjectNotFoundException(@event.Payload.ContentId.ToString());
            }

            SimpleMapper.Map(content, result);

            switch (@event.Payload)
            {
            case ContentCreated _:
                result.Type = EnrichedContentEventType.Created;
                break;

            case ContentDeleted _:
                result.Type = EnrichedContentEventType.Deleted;
                break;

            case ContentStatusChanged statusChanged:
            {
                switch (statusChanged.Change)
                {
                case StatusChange.Published:
                    result.Type = EnrichedContentEventType.Published;
                    break;

                case StatusChange.Unpublished:
                    result.Type = EnrichedContentEventType.Unpublished;
                    break;

                default:
                    result.Type = EnrichedContentEventType.StatusChanged;
                    break;
                }

                break;
            }

            case ContentUpdated _:
            {
                result.Type = EnrichedContentEventType.Updated;

                var previousContent =
                    await contentLoader.GetAsync(
                        content.AppId.Id,
                        content.Id,
                        content.Version - 1);

                if (previousContent == null)
                {
                    throw new DomainObjectNotFoundException(@event.Payload.ContentId.ToString());
                }

                result.DataOld = previousContent.Data;
                break;
            }
            }

            result.Name = $"{content.SchemaId.Name.ToPascalCase()}{result.Type}";

            return(result);
        }
Exemplo n.º 10
0
        public async IAsyncEnumerable <EnrichedEvent> CreateEnrichedEventsAsync(Envelope <AppEvent> @event, RuleContext context,
                                                                                [EnumeratorCancellation] CancellationToken ct = default)
        {
            var contentEvent = (ContentEvent)@event.Payload;

            var result = new EnrichedContentEvent();

            var content =
                await contentLoader.GetAsync(
                    contentEvent.AppId.Id,
                    contentEvent.ContentId,
                    @event.Headers.EventStreamNumber());

            if (content != null)
            {
                SimpleMapper.Map(content, result);
            }

            switch (@event.Payload)
            {
            case ContentCreated:
                result.Type = EnrichedContentEventType.Created;
                break;

            case ContentDeleted:
                result.Type = EnrichedContentEventType.Deleted;
                break;

            case ContentStatusChanged e when e.Change == StatusChange.Published:
                result.Type = EnrichedContentEventType.Published;
                break;

            case ContentStatusChanged e when e.Change == StatusChange.Unpublished:
                result.Type = EnrichedContentEventType.Unpublished;
                break;

            case ContentStatusChanged e when e.Change == StatusChange.Change:
                result.Type = EnrichedContentEventType.StatusChanged;
                break;

            case ContentUpdated:
            {
                result.Type = EnrichedContentEventType.Updated;

                if (content != null)
                {
                    var previousContent =
                        await contentLoader.GetAsync(
                            content.AppId.Id,
                            content.Id,
                            content.Version - 1);

                    if (previousContent != null)
                    {
                        result.DataOld = previousContent.Data;
                    }
                }

                break;
            }
            }

            yield return(result);
        }
Exemplo n.º 11
0
 private Task <IContentEntity> FindByVersionAsync(Guid id, long version)
 {
     return(contentVersionLoader.GetAsync(id, version));
 }
Exemplo n.º 12
0
 private Task <IContentEntity?> FindByVersionAsync(Context context, DomainId id, long version)
 {
     return(contentVersionLoader.GetAsync(context.App.Id, id, version));
 }