예제 #1
0
        public async Task <Response> GetPage(ReadStreamOptions options, CancellationToken cancellationToken)
        {
            var operation = options.GetReadOperation();

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

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

            return(new Response(
                       new HALResponse(new object())
                       .AddLinks(StreamLinks.Self(options))
                       .AddLinks(StreamLinks.Navigation(page, options.Self))
                       .AddLinks(StreamLinks.Feed(page))
                       .AddEmbeddedCollection(
                           Relations.Message,
                           page.Messages.Zip(payloads,
                                             (message, payload) => new HALResponse(new
            {
                message.MessageId,
                message.CreatedUtc,
                message.Position,
                message.StreamId,
                message.StreamVersion,
                message.Type,
                payload
            }).AddLinks(StreamMessageLinks.Self(message))))));
        }
예제 #2
0
        public async Task <Response> GetMessage(
            ReadStreamMessageOptions options,
            CancellationToken cancellationToken)
        {
            var operation = options.GetReadOperation();

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

            if (message.MessageId == Guid.Empty)
            {
                return(new Response(
                           new HALResponse(new
                {
                    options.StreamId,
                    options.StreamVersion
                })
                           .AddLinks(StreamMessageLinks.Self(options))
                           .AddLinks(StreamMessageLinks.Navigation(options))
                           .AddLinks(StreamLinks.Feed(options)),
                           404));
            }

            var payload = await message.GetJsonData(cancellationToken);

            return(new Response(
                       new HALResponse(new
            {
                message.MessageId,
                message.CreatedUtc,
                message.Position,
                message.StreamId,
                message.StreamVersion,
                message.Type,
                payload
            })
                       .AddLinks(StreamMessageLinks.Self(options))
                       .AddLinks(StreamMessageLinks.Navigation(options, message))
                       .AddLinks(StreamLinks.Feed(options))));
        }