Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/SqlStreamStore.HAL/AllStream/AllStreamResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public async Task<Response> Get(
.FromOperation(operation)
.Index()
.Find()
.Browse()
.AllStreamNavigation(page, operation))
.AddEmbeddedCollection(
Constants.Relations.Message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public async Task<Response> Get(
.FromOperation(operation)
.Index()
.Find()
.Browse()
.Add(
Constants.Relations.Message,
$"stream/{message.Position}",
Expand Down Expand Up @@ -62,7 +63,7 @@ public async Task<Response> Get(
message.Type,
payload,
metadata = message.JsonMetadata
}).AddLinks(links));
}).AddLinks(links));
}
}
}
2 changes: 2 additions & 0 deletions src/SqlStreamStore.HAL/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static class Relations
public const string DeleteStream = StreamStorePrefix + ":delete-stream";
public const string DeleteMessage = StreamStorePrefix + ":delete-message";
public const string Find = StreamStorePrefix + ":find";
public const string Browse = StreamStorePrefix + ":feed-browser";
}

public static class Streams
Expand All @@ -71,6 +72,7 @@ public static class Streams
public static PathString AllStreamPath = new PathString($"/{All}");
public static PathString StreamsPath = new PathString($"/{Stream}");
public static PathString IndexPath = new PathString("/");
public static PathString StreamBrowserPath = StreamsPath;
}

public static class ReadDirection
Expand Down
4 changes: 1 addition & 3 deletions src/SqlStreamStore.HAL/Index/IndexResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace SqlStreamStore.HAL.Index
using System.Reflection;
using Halcyon.HAL;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

internal class IndexResource : IResource
Expand Down Expand Up @@ -43,8 +42,7 @@ private static string GetVersion(Type type)
.FromPath(PathString.Empty)
.Index().Self()
.Find()
.Browse()
.Add(Constants.Relations.Feed, Constants.Streams.All)));

public override string ToString() => _data.ToString(Formatting.None);
}
}
3 changes: 3 additions & 0 deletions src/SqlStreamStore.HAL/LinksExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ public static Links Index(this Links links) =>

public static Links Find(this Links links)
=> links.Add(Constants.Relations.Find, "streams/{streamId}", "Find a Stream");

public static Links Browse(this Links links)
=> links.Add(Constants.Relations.Browse, "streams{?p,t,m}", "Browse Streams");
}
}
3 changes: 3 additions & 0 deletions src/SqlStreamStore.HAL/SqlStreamStoreHalMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using SqlStreamStore.HAL.Docs;
using SqlStreamStore.HAL.Index;
using SqlStreamStore.HAL.Logging;
using SqlStreamStore.HAL.StreamBrowser;
using SqlStreamStore.HAL.StreamMessage;
using SqlStreamStore.HAL.StreamMetadata;
using SqlStreamStore.HAL.Streams;
Expand Down Expand Up @@ -62,6 +63,7 @@ public static IApplicationBuilder UseSqlStreamStoreHal(
var index = new IndexResource(streamStore);
var allStream = new AllStreamResource(streamStore, options.UseCanonicalUrls);
var allStreamMessages = new AllStreamMessageResource(streamStore);
var streamBrowser = new StreamBrowserResource(streamStore);
var streams = new StreamResource(streamStore);
var streamMetadata = new StreamMetadataResource(streamStore);
var streamMessages = new StreamMessageResource(streamStore);
Expand All @@ -83,6 +85,7 @@ public static IApplicationBuilder UseSqlStreamStoreHal(
.UseIndex(index)
.UseAllStream(allStream)
.UseAllStreamMessage(allStreamMessages)
.UseStreamBrowser(streamBrowser)
.UseStreams(streams)
.UseStreamMetadata(streamMetadata)
.UseStreamMessages(streamMessages);
Expand Down
58 changes: 58 additions & 0 deletions src/SqlStreamStore.HAL/StreamBrowser/ListStreamsOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace SqlStreamStore.HAL.StreamBrowser
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using SqlStreamStore.Streams;

internal class ListStreamsOperation : IStreamStoreOperation<ListStreamsPage>
{
public Pattern Pattern { get; }
public string ContinuationToken { get; }
public int MaxCount { get; }
public string PatternType { get; }
public PathString Path { get; }

public ListStreamsOperation(HttpRequest request)
{
Path = request.Path;
if(request.Query.TryGetValueCaseInsensitive('t', out var patternType))
{
PatternType = patternType;
}

if(!request.Query.TryGetValueCaseInsensitive('p', out var pattern))
{
Pattern = Pattern.Anything();
}
else
{
switch(PatternType)
{
case "s":
Pattern = Pattern.StartsWith(pattern);
break;
case "e":
Pattern = Pattern.EndsWith(pattern);
break;
default:
Pattern = Pattern.Anything();
break;
}
}

if(request.Query.TryGetValueCaseInsensitive('c', out var continuationToken))
{
ContinuationToken = continuationToken;
}

MaxCount = request.Query.TryGetValueCaseInsensitive('m', out var m)
&& int.TryParse(m, out var maxCount)
? maxCount
: 100;
}

public Task<ListStreamsPage> Invoke(IStreamStore streamStore, CancellationToken cancellationToken)
=> streamStore.ListStreams(Pattern, MaxCount, ContinuationToken, cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace SqlStreamStore.HAL.StreamBrowser
{
using SqlStreamStore.Streams;

internal static class StreamBrowserLinkExtensions
{
public static Links StreamBrowserNavigation(
this Links links,
ListStreamsPage listStreamsPage,
ListStreamsOperation operation)
{
if(operation.ContinuationToken != null)
{
links.Add(
Constants.Relations.Next,
FormatLink(operation, listStreamsPage.ContinuationToken));
}

if(listStreamsPage.ContinuationToken != null)
{
links.Add(
Constants.Relations.Next,
FormatLink(operation, listStreamsPage.ContinuationToken));
}


return links
.Add(
Constants.Relations.Browse,
FormatLink(operation, listStreamsPage.ContinuationToken))
.Self();
}

private static string FormatLink(ListStreamsOperation operation, string continuationToken) =>
$"streams?p={operation.Pattern.Value}&t={operation.PatternType}&c={continuationToken}&m={operation.MaxCount}";
}
}
41 changes: 41 additions & 0 deletions src/SqlStreamStore.HAL/StreamBrowser/StreamBrowserMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace SqlStreamStore.HAL.StreamBrowser
{
using System;
using System.Net.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using MidFunc = System.Func<
Microsoft.AspNetCore.Http.HttpContext,
System.Func<System.Threading.Tasks.Task>,
System.Threading.Tasks.Task
>;

internal static class StreamBrowserMiddleware
{
public static IApplicationBuilder UseStreamBrowser(
this IApplicationBuilder builder,
StreamBrowserResource streamBrowser)
=> builder.MapWhen(IsMatch, Configure(streamBrowser));

private static bool IsMatch(HttpContext context)
=> context.Request.Path.IsStreamBrowser();

public static bool IsStreamBrowser(this PathString requestPath)
=> requestPath == Constants.Streams.StreamBrowserPath;

private static Action<IApplicationBuilder> Configure(StreamBrowserResource streamBrowser)
=> builder => builder
.UseMiddlewareLogging(typeof(StreamBrowserMiddleware))
.MapWhen(HttpMethod.Get, inner => inner.Use(BrowseStreams(streamBrowser)));

private static MidFunc BrowseStreams(StreamBrowserResource streamBrowser)
=> async (context, next) =>
{
var response = await streamBrowser.Get(
new ListStreamsOperation(context.Request),
context.RequestAborted);

await context.WriteResponse(response);
};
}
}
44 changes: 44 additions & 0 deletions src/SqlStreamStore.HAL/StreamBrowser/StreamBrowserResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace SqlStreamStore.HAL.StreamBrowser
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Halcyon.HAL;
using SqlStreamStore.HAL.StreamBrowser;

internal class StreamBrowserResource
{
private readonly IStreamStore _streamStore;

public StreamBrowserResource(IStreamStore streamStore)
{
_streamStore = streamStore;
}

public async Task<Response> Get(ListStreamsOperation operation, CancellationToken cancellationToken)
{
var listStreamsPage = await operation.Invoke(_streamStore, cancellationToken);

return new HalJsonResponse(new HALResponse(new
{
listStreamsPage.ContinuationToken
})
.AddLinks(
Links
.FromOperation(operation)
.Index()
.Find()
.StreamBrowserNavigation(listStreamsPage, operation))
.AddEmbeddedCollection(
Constants.Relations.Feed,
Array.ConvertAll(
listStreamsPage.StreamIds,
streamId => new HALResponse(null)
.AddLinks(
Links
.FromOperation(operation)
.Add(Constants.Relations.Feed, $"{Constants.Streams.Stream}/{streamId}", streamId)
.Self()))));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public async Task<Response> Get(
.FromPath(operation.Path)
.Index()
.Find()
.Browse()
.StreamMessageNavigation(message, operation);

if(message.MessageId == Guid.Empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public async Task<Response> Get(
.FromOperation(operation)
.Index()
.Find()
.Browse()
.StreamMetadataNavigation(operation))
.AddEmbeddedResource(
Constants.Relations.Metadata,
Expand Down
2 changes: 2 additions & 0 deletions src/SqlStreamStore.HAL/Streams/StreamResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public async Task<Response> Post(
.FromOperation(operation)
.Index()
.Find()
.Browse()
.Add(Constants.Relations.Feed, $"streams/{operation.StreamId}").Self();

var response = new HalJsonResponse(
Expand Down Expand Up @@ -98,6 +99,7 @@ public async Task<Response> Get(ReadStreamOperation operation, CancellationToken
.FromOperation(operation)
.Index()
.Find()
.Browse()
.StreamsNavigation(page, operation))
.AddEmbeddedResource(
Constants.Relations.AppendToStream,
Expand Down