Пример #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 static async Task <StreamMeta> GetStreamMeta(
            this EventStoreClient client, string stream
            )
        {
            var streamMeta = await client.GetStreamMetadataAsync(stream).ConfigureAwait(false);

            var streamDeleted = streamMeta.StreamDeleted ||
                                streamMeta.Metadata.TruncateBefore == StreamPosition.End;

            return(new StreamMeta(
                       streamDeleted,
                       streamMeta.Metadata.MaxAge,
                       streamMeta.Metadata.MaxCount,
                       streamMeta.MetastreamRevision !.Value.ToInt64()
                       ));
        }
 public Task <StreamMetadataResult> GetStreamMetadataAsync(string streamName, Action <EventStoreClientOperationOptions> configureOperationOptions = null,
                                                           UserCredentials userCredentials = null, CancellationToken cancellationToken = new CancellationToken())
 {
     return(Client.GetStreamMetadataAsync(streamName, configureOperationOptions, userCredentials, cancellationToken));
 }