public async Task <Response> GetPage(
            ReadAllStreamOperation operation,
            CancellationToken cancellationToken)
        {
            var page = await operation.Invoke(_streamStore, cancellationToken);

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

            var payloads = await Task.WhenAll(streamMessages
                                              .Select(message => operation.EmbedPayload
                    ? message.GetJsonData(cancellationToken)
                    : Task.FromResult <string>(null))
                                              .ToArray());

            var response = new Response(
                new HALResponse(new
            {
                page.FromPosition,
                page.NextPosition,
                page.IsEnd
            })
                .AddLinks(Links.Self(operation))
                .AddLinks(Links.Navigation(page, operation))
                .AddLinks(Links.Feed(operation))
                .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.Message.Self(message)))));

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

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

            return(response);
        }
Пример #2
0
        public async Task <Response> Get(
            ReadAllStreamOperation operation,
            CancellationToken cancellationToken)
        {
            if (_useCanonicalUrls && !operation.IsUriCanonical)
            {
                return(new Response(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 Response(
                new HALResponse(new
            {
                page.FromPosition,
                page.NextPosition,
                page.IsEnd
            })
                .AddLinks(Links.Self(operation))
                .AddLinks(Links.Navigation(page, operation))
                .AddLinks(Links.Feed(operation))
                .AddLinks(Links.Find())
                .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.Message.Self(message),
                            Links.Message.Feed(message)))));

            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);
        }