private static MidFunc GetStream(StreamResource streams) => async(context, next) =>
        {
            var operation = new ReadStreamOperation(context.Request);

            var response = await streams.Get(operation, context.RequestAborted);

            await context.WriteResponse(response);
        };
        public static Links StreamsNavigation(this Links links, ReadStreamPage page, ReadStreamOperation operation)
        {
            var baseAddress = $"streams/{operation.StreamId}";

            var first = Links.FormatForwardLink(
                baseAddress,
                operation.MaxCount,
                StreamVersion.Start,
                operation.EmbedPayload);

            var last = Links.FormatBackwardLink(
                baseAddress,
                operation.MaxCount,
                StreamVersion.End,
                operation.EmbedPayload);

            links.Add(Constants.Relations.First, first);

            if (operation.Self != first && !page.IsEnd)
            {
                links.Add(
                    Constants.Relations.Previous,
                    Links.FormatBackwardLink(
                        baseAddress,
                        operation.MaxCount,
                        page.Messages.Min(m => m.StreamVersion) - 1,
                        operation.EmbedPayload));
            }

            links.Add(Constants.Relations.Feed, operation.Self, operation.StreamId).Self();

            if (operation.Self != last && !page.IsEnd)
            {
                links.Add(
                    Constants.Relations.Next,
                    Links.FormatForwardLink(
                        baseAddress,
                        operation.MaxCount,
                        page.Messages.Max(m => m.StreamVersion) + 1,
                        operation.EmbedPayload));
            }

            links.Add(Constants.Relations.Last, last)
            .Add(Constants.Relations.Metadata,
                 $"{baseAddress}/metadata");

            return(links);
        }
        public static Links StreamsNavigation(this Links links, ReadStreamPage page, ReadStreamOperation operation)
        {
            var first = LinkFormatter.ReadStreamForwards(
                operation.StreamId,
                StreamVersion.Start,
                operation.MaxCount,
                operation.EmbedPayload);

            var last = LinkFormatter.ReadStreamBackwards(
                operation.StreamId,
                StreamVersion.End,
                operation.MaxCount,
                operation.EmbedPayload);

            links.Add(Constants.Relations.First, first);

            if (operation.Self != first && !page.IsEnd)
            {
                links.Add(
                    Constants.Relations.Previous,
                    LinkFormatter.ReadStreamBackwards(
                        operation.StreamId,
                        page.Messages.Min(m => m.StreamVersion) - 1,
                        operation.MaxCount,
                        operation.EmbedPayload));
            }

            links.Add(Constants.Relations.Feed, operation.Self, operation.StreamId).Self();

            if (operation.Self != last && !page.IsEnd)
            {
                links.Add(
                    Constants.Relations.Next,
                    LinkFormatter.ReadStreamForwards(
                        operation.StreamId,
                        page.Messages.Max(m => m.StreamVersion) + 1,
                        operation.MaxCount,
                        operation.EmbedPayload));
            }

            links.Add(Constants.Relations.Last, last)
            .Add(Constants.Relations.Metadata, LinkFormatter.StreamMetadata(operation.StreamId));

            return(links);
        }
Ejemplo n.º 4
0
        public async Task <Response> Get(ReadStreamOperation operation, CancellationToken cancellationToken)
        {
            if (_useCanonicalUrls && !operation.IsUriCanonical)
            {
                return(new PermanentRedirectResponse($"../{operation.Self}"));
            }

            var page = await operation.Invoke(_streamStore, cancellationToken);

            var streamMessages = page.Messages.OrderByDescending(m => m.Position).ToArray();

            var payloads = await Task.WhenAll(
                Array.ConvertAll(
                    streamMessages,
                    message => operation.EmbedPayload
                        ? message.GetJsonData(cancellationToken)
                        : SkippedPayload.Instance));

            var response = new HalJsonResponse(
                new HALResponse(new
            {
                page.LastStreamVersion,
                page.LastStreamPosition,
                page.FromStreamVersion,
                page.NextStreamVersion,
                page.IsEnd
            })
                .AddLinks(Links
                          .FromOperation(operation)
                          .Index()
                          .Find()
                          .Browse()
                          .StreamsNavigation(page, operation))
                .AddEmbeddedResource(
                    Constants.Relations.AppendToStream,
                    append)
                .AddEmbeddedResource(
                    Constants.Relations.DeleteStream,
                    delete)
                .AddEmbeddedCollection(
                    Constants.Relations.Message,
                    streamMessages.Zip(
                        payloads,
                        (message, payload) => new StreamMessageHALResponse(message, payload)
                        .AddLinks(
                            Links
                            .FromOperation(operation)
                            .Add(
                                Constants.Relations.Message,
                                LinkFormatter.StreamMessageByStreamVersion(
                                    message.StreamId,
                                    message.StreamVersion),
                                $"{message.StreamId}@{message.StreamVersion}")
                            .Self()
                            .Add(
                                Constants.Relations.Feed,
                                LinkFormatter.Stream(message.StreamId),
                                message.StreamId)))),
                page.Status == PageReadStatus.StreamNotFound ? 404 : 200);

            if (page.TryGetETag(out var eTag))
            {
                response.Headers.Add(eTag);
            }

            return(response);
        }
Ejemplo n.º 5
0
        public async Task <Response> Get(ReadStreamOperation operation, CancellationToken cancellationToken)
        {
            if (!operation.IsUriCanonical)
            {
                return(new HalJsonResponse(new HALResponse(null), 308)
                {
                    Headers = { [Constants.Headers.Location] = new[] { $"../{operation.Self}" } }
                });
            }

            var page = await operation.Invoke(_streamStore, cancellationToken);

            var streamMessages = page.Messages.OrderByDescending(m => m.Position).ToArray();

            var payloads = await Task.WhenAll(
                Array.ConvertAll(
                    streamMessages,
                    message => operation.EmbedPayload
                        ? message.GetJsonData(cancellationToken)
                        : SkippedPayload.Instance));

            var response = new HalJsonResponse(
                new HALResponse(new
            {
                page.LastStreamVersion,
                page.LastStreamPosition,
                page.FromStreamVersion,
                page.NextStreamVersion,
                page.IsEnd
            })
                .AddLinks(Links
                          .FromOperation(operation)
                          .Index()
                          .Find()
                          .Browse()
                          .StreamsNavigation(page, operation))
                .AddEmbeddedResource(
                    Constants.Relations.AppendToStream,
                    append)
                .AddEmbeddedResource(
                    Constants.Relations.DeleteStream,
                    delete)
                .AddEmbeddedCollection(
                    Constants.Relations.Message,
                    streamMessages.Zip(
                        payloads,
                        (message, payload) => new HALResponse(new
            {
                message.MessageId,
                message.CreatedUtc,
                message.Position,
                message.StreamId,
                message.StreamVersion,
                message.Type,
                payload,
                metadata = message.JsonMetadata
            })
                        .AddLinks(
                            Links
                            .FromOperation(operation)
                            .Add(
                                Constants.Relations.Message,
                                $"{Constants.Streams.Stream}/{message.StreamId}/{message.StreamVersion}",
                                $"{message.StreamId}@{message.StreamVersion}")
                            .Self()
                            .Add(
                                Constants.Relations.Feed,
                                $"streams/{message.StreamId}",
                                message.StreamId)))),
                page.Status == PageReadStatus.StreamNotFound ? 404 : 200);

            if (page.TryGetETag(out var eTag))
            {
                response.Headers.Add(eTag);
            }

            return(response);
        }