Beispiel #1
0
        public static Links StreamMessageNavigation(
            this Links links,
            StreamMessage message,
            ReadStreamMessageByStreamVersionOperation operation)
        {
            links.Add(Constants.Relations.First, $"{StreamId(operation)}/0");

            if (operation.StreamVersion > 0)
            {
                links.Add(Constants.Relations.Previous, $"{StreamId(operation)}/{operation.StreamVersion - 1}");
            }

            if (message.StreamId != default)
            {
                links.Add(Constants.Relations.Next, $"{StreamId(operation)}/{operation.StreamVersion + 1}");
            }

            return(links.Add(Constants.Relations.Last, $"{StreamId(operation)}/-1")
                   .Add(
                       Constants.Relations.Feed,
                       Links.FormatBackwardLink(
                           StreamId(operation),
                           Constants.MaxCount,
                           StreamVersion.End,
                           false),
                       operation.StreamId)
                   .Add(
                       Constants.Relations.Message,
                       $"{StreamId(operation)}/{operation.StreamVersion}",
                       $"{operation.StreamId}@{operation.StreamVersion}").Self());
        }
Beispiel #2
0
        private static MidFunc GetStreamMessage(StreamMessageResource streamMessages) => async(context, next) =>
        {
            var operation = new ReadStreamMessageByStreamVersionOperation(context.Request);

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

            await context.WriteResponse(response);
        };
        public async Task <Response> Get(
            ReadStreamMessageByStreamVersionOperation operation,
            CancellationToken cancellationToken)
        {
            var message = await operation.Invoke(_streamStore, cancellationToken);

            var links = Links
                        .FromPath(operation.Path)
                        .Index()
                        .Find()
                        .Browse()
                        .StreamMessageNavigation(message, operation);

            if (message.MessageId == Guid.Empty)
            {
                return(new HalJsonResponse(
                           new HALResponse(new
                {
                    operation.StreamId,
                    operation.StreamVersion
                })
                           .AddLinks(links),
                           404));
            }

            if (operation.StreamVersion == StreamVersion.End)
            {
                return(new HalJsonResponse(new HALResponse(new object()), 307)
                {
                    Headers =
                    {
                        [Constants.Headers.Location] = new[] { $"{message.StreamVersion}" }
                    }
                });
            }

            var payload = await message.GetJsonData(cancellationToken);

            var eTag = ETag.FromStreamVersion(message.StreamVersion);

            return(new HalJsonResponse(
                       new StreamMessageHALResponse(message, payload)
                       .AddEmbeddedResource(
                           Constants.Relations.DeleteMessage,
                           DeleteStreamMessage)
                       .AddLinks(links))
            {
                Headers =
                {
                    eTag,
                    CacheControl.OneYear
                }
            });
        }
Beispiel #4
0
        public static Links StreamMessageNavigation(
            this Links links,
            StreamMessage message,
            ReadStreamMessageByStreamVersionOperation operation)
        {
            links.Add(
                Constants.Relations.First,
                LinkFormatter.StreamMessageByStreamVersion(operation.StreamId, 0));

            if (operation.StreamVersion > 0)
            {
                links.Add(
                    Constants.Relations.Previous,
                    LinkFormatter.StreamMessageByStreamVersion(operation.StreamId, operation.StreamVersion - 1));
            }

            if (message.StreamId != default)
            {
                links.Add(
                    Constants.Relations.Next,
                    LinkFormatter.StreamMessageByStreamVersion(operation.StreamId, operation.StreamVersion + 1));
            }

            return(links
                   .Add(
                       Constants.Relations.Last,
                       LinkFormatter.StreamMessageByStreamVersion(operation.StreamId, -1))
                   .Add(
                       Constants.Relations.Feed,
                       LinkFormatter.ReadStreamBackwards(
                           operation.StreamId,
                           StreamVersion.End,
                           Constants.MaxCount,
                           false),
                       operation.StreamId)
                   .Add(
                       Constants.Relations.Message,
                       LinkFormatter.StreamMessageByStreamVersion(operation.StreamId, operation.StreamVersion),
                       $"{operation.StreamId}@{operation.StreamVersion}")
                   .Self());
        }
Beispiel #5
0
 private static string StreamId(ReadStreamMessageByStreamVersionOperation operation)
 => $"streams/{operation.StreamId}";