Esempio n. 1
0
        private static MidFunc GetStream(AllStreamResource allStream) => async(context, next) =>
        {
            var operation = new ReadAllStreamOperation(context.Request);

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

            await context.WriteResponse(response);
        };
Esempio n. 2
0
        public static Links AllStreamNavigation(
            this Links links,
            ReadAllPage page,
            ReadAllStreamOperation operation)
        {
            var first = Links.FormatForwardLink(
                Constants.Streams.All,
                operation.MaxCount,
                Position.Start,
                operation.EmbedPayload);

            var last = Links.FormatBackwardLink(
                Constants.Streams.All,
                operation.MaxCount,
                Position.End,
                operation.EmbedPayload);

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

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

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

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

            links.Add(Constants.Relations.Last, last);

            return(links);
        }
Esempio n. 3
0
        public async Task <Response> Get(
            ReadAllStreamOperation operation,
            CancellationToken cancellationToken)
        {
            if (_useCanonicalUrls && !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.FromPosition,
                page.NextPosition,
                page.IsEnd
            })
                .AddLinks(
                    Links
                    .FromOperation(operation)
                    .Index()
                    .Find()
                    .Browse()
                    .AllStreamNavigation(page, operation))
                .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)))));

            if (operation.FromPositionInclusive == Position.End)
            {
                var headPosition = streamMessages.Length > 0
                    ? streamMessages[0].Position
                    : Position.End;

                response.Headers[Constants.Headers.HeadPosition] = new[] { $"{headPosition}" };
            }

            if (page.TryGetETag(operation.FromPositionInclusive, out var eTag))
            {
                response.Headers.Add(eTag);
                response.Headers.Add(CacheControl.NoCache);
            }
            else
            {
                response.Headers.Add(CacheControl.OneYear);
            }

            return(response);
        }