Exemple #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)));
            }
        }
Exemple #2
0
 public StreamMetadataResource(string streamId, Client.StreamMetadata metadata)
 {
     StreamId = streamId;
     Metadata = metadata;
 }