public async Task <Response> Get( GetStreamMetadataOperation operation, CancellationToken cancellationToken) { var result = await operation.Invoke(_streamStore, cancellationToken); var response = new HalJsonResponse(new HALResponse(new { result.StreamId, result.MetadataStreamVersion, result.MaxAge, result.MaxCount, result.MetadataJson }) .AddLinks( Links .FromOperation(operation) .Index() .Find() .Browse() .StreamMetadataNavigation(operation)) .AddEmbeddedResource( Constants.Relations.Metadata, metadata), result.MetadataStreamVersion >= 0 ? 200 : 404) { Headers = { ETag.FromStreamVersion(result.MetadataStreamVersion) } }; return(response); }
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); }
public async Task <Response> Post( SetStreamMetadataOperation operation, CancellationToken cancellationToken) { await operation.Invoke(_streamStore, cancellationToken); var response = new HalJsonResponse(new HALResponse(new { operation.StreamId, operation.MaxAge, operation.MaxCount, operation.MetadataJson }) .AddLinks( Links .FromOperation(operation) .Index() .Find() .StreamMetadataNavigation(operation))); return(response); }
public async Task <Response> Get(ReadStreamOperation operation, CancellationToken cancellationToken) { if (_useCanonicalUrls && !operation.IsUriCanonical) { return(new PermanentRedirectResponse($"../{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.LastStreamVersion, page.LastStreamPosition, page.FromStreamVersion, page.NextStreamVersion, page.IsEnd }) .AddLinks(Links .FromOperation(operation) .Index() .Find() .Browse() .StreamsNavigation(page, operation)) .AddEmbeddedResource( Constants.Relations.AppendToStream, append) .AddEmbeddedResource( Constants.Relations.DeleteStream, delete) .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)))), page.Status == PageReadStatus.StreamNotFound ? 404 : 200); if (page.TryGetETag(out var eTag)) { response.Headers.Add(eTag); } return(response); }
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); }
public async Task <Response> Get(ReadStreamOperation operation, CancellationToken cancellationToken) { if (!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.LastStreamVersion, page.LastStreamPosition, page.FromStreamVersion, page.NextStreamVersion, page.IsEnd }) .AddLinks(Links .FromOperation(operation) .Index() .Find() .Browse() .StreamsNavigation(page, operation)) .AddEmbeddedResource( Constants.Relations.AppendToStream, append) .AddEmbeddedResource( Constants.Relations.DeleteStream, delete) .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 .FromOperation(operation) .Add( Constants.Relations.Message, $"{Constants.Streams.Stream}/{message.StreamId}/{message.StreamVersion}", $"{message.StreamId}@{message.StreamVersion}") .Self() .Add( Constants.Relations.Feed, $"streams/{message.StreamId}", message.StreamId)))), page.Status == PageReadStatus.StreamNotFound ? 404 : 200); if (page.TryGetETag(out var eTag)) { response.Headers.Add(eTag); } return(response); }