コード例 #1
0
        public static IApplicationBuilder UseStreamMetadata(this IApplicationBuilder builder,
                                                            EventStoreClient eventStore)
        {
            var options = new JsonSerializerOptions {
                Converters =
                {
                    new TimeSpanConverter(),
                    new NullableTimeSpanConverter(),
                    new JsonDocumentConverter()
                },
                IgnoreNullValues     = true,
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            };

            return(builder.MapMethods(
                       "/streams/{streamId}/metadata",
                       (HttpMethod.Get, GetMetadata),
                       (HttpMethod.Post, PostMetadata)));

            async ValueTask <Response> GetMetadata(HttpContext context)
            {
                var streamId = context.Request.GetStreamId() !;

                var metadataResult = await eventStore.GetStreamMetadataAsync(streamId,
                                                                             userCredentials : context.GetUserCredentials(),
                                                                             cancellationToken : context.RequestAborted);

                var resource = new StreamMetadataResource(streamId, metadataResult.Metadata);

                return(new HALResponse(StreamMetadataRepresentation.Instance, resource)
                {
                    StatusCode = metadataResult == StreamMetadataResult.None(streamId)
                                                ? HttpStatusCode.NotFound
                                                : HttpStatusCode.OK
                });
            }

            async ValueTask <Response> PostMetadata(HttpContext context)
            {
                var streamId = context.Request.GetStreamId();

                var dto = await JsonSerializer.DeserializeAsync <StreamMetadataDto>(context.Request.Body, options);

                var metadata = new Client.StreamMetadata(
                    dto.MaxCount, dto.MaxAge, dto.TruncateBefore.HasValue
                                                ? new StreamPosition(dto.TruncateBefore.Value)
                                                : new StreamPosition?(), dto.CacheControl,
                    new StreamAcl(dto.Acl?.ReadRoles, dto.Acl?.WriteRoles, dto.Acl?.DeleteRoles, dto.Acl?.MetaReadRoles,
                                  dto.Acl?.MetaWriteRoles), dto.CustomMetadata);

                var result = await eventStore.SetStreamMetadataAsync(streamId !, StreamState.Any,
                                                                     metadata,
                                                                     userCredentials: context.GetUserCredentials(),
                                                                     cancellationToken : context.RequestAborted);

                return(new HALResponse(StreamMetadataResultRepresentation.Instance,
                                       (streamId, result)));
            }
        }
コード例 #2
0
        public async Task getting_for_an_existing_stream_and_no_metadata_exists()
        {
            var stream = _fixture.GetStreamName();

            await _fixture.Client.AppendToStreamAsync(stream, AnyStreamRevision.NoStream, _fixture.CreateTestEvents());

            var actual = await _fixture.Client.GetStreamMetadataAsync(stream);

            Assert.Equal(StreamMetadataResult.None(stream), actual);
        }