Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit ea05d76

Browse files
Merge branch 'uri-templates'
2 parents 88cc6a5 + 3133d22 commit ea05d76

17 files changed

Lines changed: 97 additions & 39 deletions

src/SqlStreamStore.HAL.Tests/AllStreamMessageTests.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public AllStreamMessageTests()
1515

1616
public void Dispose() => _fixture.Dispose();
1717
private readonly SqlStreamStoreHalMiddlewareFixture _fixture;
18-
private const string HeadOfAll = "../stream?d=b&m=20&p=-1";
18+
private const string HeadOfAll = "../stream?d=b&m=20&p=-1&e=0";
1919

2020
[Fact]
2121
public async Task read_single_message_all_stream()
@@ -33,12 +33,14 @@ public async Task read_single_message_all_stream()
3333
{
3434
Constants.Relations.Self,
3535
Constants.Relations.Message,
36-
Constants.Relations.Feed
36+
Constants.Relations.Feed,
37+
Constants.Relations.Find
3738
});
3839

3940
resource.ShouldLink(Constants.Relations.Self, "0");
4041
resource.ShouldLink(Constants.Relations.Message, "0");
4142
resource.ShouldLink(Constants.Relations.Feed, HeadOfAll);
43+
resource.ShouldLink(Constants.Relations.Find, "../streams/{streamId}", "Find a Stream");
4244
}
4345
}
4446

@@ -51,9 +53,14 @@ public async Task read_single_message_does_not_exist_all_stream()
5153

5254
var resource = await response.AsHal();
5355

54-
resource.Links.Keys.ShouldBe(new[] { Constants.Relations.Feed });
56+
resource.Links.Keys.ShouldBe(new[]
57+
{
58+
Constants.Relations.Feed,
59+
Constants.Relations.Find
60+
});
5561

5662
resource.ShouldLink(Constants.Relations.Feed, HeadOfAll);
63+
resource.ShouldLink(Constants.Relations.Find, "../streams/{streamId}", "Find a Stream");
5764
}
5865
}
5966
}

src/SqlStreamStore.HAL.Tests/CanonicalUrlTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ IEnumerable<string[]> GetPermutations(string[] items, int length)
3131

3232
var d = $"d={(forward ? 'f' : 'b')}";
3333
var m = "m=20";
34-
var e = prefetch ? "e" : null;
34+
var e = $"e={(prefetch ? '1' : '0')}";
3535
var p = "p=0";
3636

3737
var parameters = new[]

src/SqlStreamStore.HAL.Tests/SqlStreamStore.HAL.Tests.csproj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020
</ItemGroup>
2121
<ItemGroup>
2222
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.1.1" />
23-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
23+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
2424
<PackageReference Include="Shouldly" Version="3.0.0" />
25-
<PackageReference Include="xunit" Version="2.3.1" />
26-
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
27-
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
25+
<PackageReference Include="xunit" Version="2.4.0" />
26+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
2827
</ItemGroup>
2928
</Project>

src/SqlStreamStore.HAL.Tests/StreamMessageTests.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public StreamMessageTests()
1515

1616
public void Dispose() => _fixture.Dispose();
1717
private readonly SqlStreamStoreHalMiddlewareFixture _fixture;
18-
private const string HeadOfStream = "../a-stream?d=b&m=20&p=-1";
18+
private const string HeadOfStream = "../a-stream?d=b&m=20&p=-1&e=0";
1919

2020
[Fact]
2121
public async Task read_single_message_stream()
@@ -36,7 +36,8 @@ public async Task read_single_message_stream()
3636
Constants.Relations.Next,
3737
Constants.Relations.Last,
3838
Constants.Relations.Feed,
39-
Constants.Relations.Message
39+
Constants.Relations.Message,
40+
Constants.Relations.Find
4041
});
4142

4243
resource.ShouldLink(Constants.Relations.Self, "0");
@@ -45,6 +46,7 @@ public async Task read_single_message_stream()
4546
resource.ShouldLink(Constants.Relations.Last, "-1");
4647
resource.ShouldLink(Constants.Relations.Feed, HeadOfStream);
4748
resource.ShouldLink(Constants.Relations.Message, "0");
49+
resource.ShouldLink(Constants.Relations.Find, "../../streams/{streamId}", "Find a Stream");
4850
}
4951
}
5052

@@ -63,14 +65,16 @@ public async Task read_single_message_does_not_exist_stream()
6365
Constants.Relations.First,
6466
Constants.Relations.Last,
6567
Constants.Relations.Feed,
66-
Constants.Relations.Message
68+
Constants.Relations.Message,
69+
Constants.Relations.Find
6770
});
6871

6972
resource.ShouldLink(Constants.Relations.Self, "0");
7073
resource.ShouldLink(Constants.Relations.First, "0");
7174
resource.ShouldLink(Constants.Relations.Last, "-1");
7275
resource.ShouldLink(Constants.Relations.Feed, HeadOfStream);
7376
resource.ShouldLink(Constants.Relations.Message, "0");
77+
resource.ShouldLink(Constants.Relations.Find, "../../streams/{streamId}", "Find a Stream");
7478
}
7579
}
7680

src/SqlStreamStore.HAL.Tests/StreamNavigationTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
public class StreamNavigationTests : IDisposable
1212
{
13-
private const string FirstLinkQuery = "d=f&m=20&p=0";
14-
private const string LastLinkQuery = "d=b&m=20&p=-1";
13+
private const string FirstLinkQuery = "d=f&m=20&p=0&e=0";
14+
private const string LastLinkQuery = "d=b&m=20&p=-1&e=0";
1515

1616
private readonly SqlStreamStoreHalMiddlewareFixture _fixture;
1717

@@ -81,7 +81,7 @@ await _fixture.HttpClient.GetAsync($"{baseAddress}{firstResponse.Headers.Locatio
8181

8282
resource.ShouldLink(Constants.Relations.Last, $"{stream}?{LastLinkQuery}");
8383

84-
resource.ShouldLink(Constants.Relations.Previous, $"{stream}?d=b&m=20&p=9");
84+
resource.ShouldLink(Constants.Relations.Previous, $"{stream}?d=b&m=20&p=9&e=0");
8585

8686
resource.ShouldLink(Constants.Relations.First, $"{stream}?{FirstLinkQuery}");
8787

@@ -136,7 +136,7 @@ public async Task read_first_link_when_multiple_pages(string stream, string base
136136

137137
resource.ShouldLink(Constants.Relations.Last, $"{stream}?{LastLinkQuery}");
138138

139-
resource.ShouldLink(Constants.Relations.Next, $"{stream}?d=f&m=20&p=20");
139+
resource.ShouldLink(Constants.Relations.Next, $"{stream}?d=f&m=20&p=20&e=0");
140140

141141
resource.ShouldLink(Constants.Relations.First, $"{stream}?{FirstLinkQuery}");
142142

src/SqlStreamStore.HAL/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static class Relations
3030
public const string Metadata = "streamStore:metadata";
3131
public const string AppendToStream = "streamStore:append";
3232
public const string Delete = "streamStore:delete";
33+
public const string Find = "streamStore:find";
3334
}
3435

3536
public static class Streams
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace SqlStreamStore.HAL
2+
{
3+
using Halcyon.HAL;
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.AspNetCore.Http;
6+
using MidFunc = System.Func<
7+
Microsoft.AspNetCore.Http.HttpContext,
8+
System.Func<System.Threading.Tasks.Task>,
9+
System.Threading.Tasks.Task
10+
>;
11+
12+
internal static class IndexMiddleware
13+
{
14+
public static IApplicationBuilder UseIndex(this IApplicationBuilder builder)
15+
=> builder.MapWhen(IsIndex, inner => inner.Use(Index));
16+
17+
private static bool IsIndex(HttpContext context)
18+
=> (context.Request.Path.Value ?? "/") == "/";
19+
20+
private static MidFunc Index => async (context, next) =>
21+
{
22+
var response = new Response(new HALResponse(null)
23+
.AddLinks(new Link(Constants.Relations.Feed, "stream"))
24+
.AddLinks(new Link(Constants.Relations.Self, string.Empty))
25+
.AddLinks(Links.Index(string.Empty))
26+
.AddLinks(Links.Find("streams/{streamId}")));
27+
28+
using(new OptionalHeadRequestWrapper(context))
29+
{
30+
await context.WriteHalResponse(response);
31+
}
32+
};
33+
}
34+
}

src/SqlStreamStore.HAL/Links.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SqlStreamStore.HAL
2+
{
3+
using Halcyon.HAL;
4+
5+
internal static class Links
6+
{
7+
public static Link Find(string href) => new Link(Constants.Relations.Find, href, "Find a Stream", replaceParameters: false);
8+
public static Link Index(string href) => new Link(Constants.Relations.Index, href, "Index", replaceParameters: false);
9+
}
10+
}

src/SqlStreamStore.HAL/Resources/AllStreamMessageResource.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public async Task<Response> Get(
3535
{
3636
return new Response(
3737
new HALResponse(new HALModelConfig())
38-
.AddLinks(Links.Feed()),
38+
.AddLinks(Links.Feed())
39+
.AddLinks(Links.Find()),
3940
404);
4041
}
4142

@@ -55,11 +56,14 @@ public async Task<Response> Get(
5556
}).AddLinks(
5657
Links.Self(message),
5758
Links.Message(message),
58-
Links.Feed()));
59+
Links.Feed(),
60+
Links.Find()));
5961
}
6062

6163
private static class Links
6264
{
65+
public static Link Find() => SqlStreamStore.HAL.Links.Find("../streams/{streamId}");
66+
6367
public static Link Last()
6468
=> new Link(
6569
Constants.Relations.Last,

src/SqlStreamStore.HAL/Resources/AllStreamResource.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public async Task<Response> Get(
6060
.AddLinks(Links.Self(operation))
6161
.AddLinks(Links.Navigation(page, operation))
6262
.AddLinks(Links.Feed(operation))
63+
.AddLinks(Links.Find())
6364
.AddEmbeddedCollection(
6465
Constants.Relations.Message,
6566
streamMessages.Zip(
@@ -93,6 +94,8 @@ public async Task<Response> Get(
9394

9495
private static class Links
9596
{
97+
public static Link Find() => SqlStreamStore.HAL.Links.Find("../streams/{streamId}");
98+
9699
public static Link Self(ReadAllStreamOperation operation)
97100
=> new Link(Constants.Relations.Self, operation.Self);
98101

0 commit comments

Comments
 (0)