private static MidFunc AppendStream(StreamResource stream) =>
        async(context, next) =>
        {
            var operation = await AppendStreamOperation.Create(context.Request, context.RequestAborted);

            var response = await stream.Post(operation, context.RequestAborted);

            await context.WriteResponse(response);
        };
Beispiel #2
0
        public async Task <Response> Post(
            AppendStreamOperation operation,
            CancellationToken cancellationToken)
        {
            if (operation.ExpectedVersion < Constants.Headers.MinimumExpectedVersion)
            {
                return(new HalJsonResponse(new HALResponse(new
                {
                    type = typeof(WrongExpectedVersionException).Name,
                    title = "Wrong expected version.",
                    detail =
                        $"Expected header '{Constants.Headers.ExpectedVersion}' to have an expected version => {ExpectedVersion.NoStream}."
                }),
                                           400));
            }

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

            var links = Links
                        .FromOperation(operation)
                        .Index()
                        .Find()
                        .Browse()
                        .Add(Constants.Relations.Feed, LinkFormatter.Stream(operation.StreamId)).Self();

            var response = new HalJsonResponse(
                new HALResponse(result)
                .AddLinks(links),
                operation.ExpectedVersion == ExpectedVersion.NoStream
                    ? 201
                    : 200);

            if (response.StatusCode == 201)
            {
                response.Headers[Constants.Headers.Location] =
                    new[] { $"{_relativePathToRoot}{LinkFormatter.Stream(operation.StreamId)}" };
            }

            return(response);
        }