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

Commit cfb9b4f

Browse files
using new links class on all stream message resource
1 parent 632ef41 commit cfb9b4f

6 files changed

Lines changed: 48 additions & 54 deletions

File tree

src/SqlStreamStore.HAL.Tests/AllStreamMessageTests.cs

Lines changed: 10 additions & 21 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&e=0";
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()
@@ -29,18 +29,11 @@ public async Task read_single_message_all_stream()
2929

3030
var resource = await response.AsHal();
3131

32-
resource.Links.Keys.ShouldBe(new[]
33-
{
34-
Constants.Relations.Self,
35-
Constants.Relations.Message,
36-
Constants.Relations.Feed,
37-
Constants.Relations.Find
38-
});
39-
40-
resource.ShouldLink(Constants.Relations.Self, "0");
41-
resource.ShouldLink(Constants.Relations.Message, "0");
42-
resource.ShouldLink(Constants.Relations.Feed, HeadOfAll);
43-
resource.ShouldLink(Constants.Relations.Find, "../streams/{streamId}", "Find a Stream");
32+
resource.ShouldLink(
33+
TheLinks
34+
.RootedAt("../")
35+
.AddSelf(Constants.Relations.Message, "stream/0")
36+
.Add(Constants.Relations.Feed, HeadOfAll));
4437
}
4538
}
4639

@@ -53,14 +46,10 @@ public async Task read_single_message_does_not_exist_all_stream()
5346

5447
var resource = await response.AsHal();
5548

56-
resource.Links.Keys.ShouldBe(new[]
57-
{
58-
Constants.Relations.Feed,
59-
Constants.Relations.Find
60-
});
61-
62-
resource.ShouldLink(Constants.Relations.Feed, HeadOfAll);
63-
resource.ShouldLink(Constants.Relations.Find, "../streams/{streamId}", "Find a Stream");
49+
resource.ShouldLink(TheLinks
50+
.RootedAt("../")
51+
.AddSelf(Constants.Relations.Message, "stream/0")
52+
.Add(Constants.Relations.Feed, HeadOfAll));
6453
}
6554
}
6655
}

src/SqlStreamStore.HAL.Tests/LinkAssertionExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace SqlStreamStore.HAL.Tests
22
{
3+
using System;
4+
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
35
using Shouldly;
46

57
internal static class LinkAssertionExtensions
@@ -13,5 +15,20 @@ public static void ShouldLink(this Resource resource, string rel, string href, s
1315
Rel = rel,
1416
Title = title
1517
});
18+
19+
public static void ShouldLink(this Resource resource, TheLinks links)
20+
{
21+
var halLinks = links.ToHalLinks();
22+
23+
foreach(var link in halLinks)
24+
{
25+
resource.Links[link.Rel].ShouldContain(new Link
26+
{
27+
Rel = link.Rel,
28+
Href = link.Href,
29+
Title = link.Title
30+
});
31+
}
32+
}
1633
}
1734
}

src/SqlStreamStore.HAL/Resources/AllStreamMessageResource.cs renamed to src/SqlStreamStore.HAL/AllStreamMessage/AllStreamMessageResource.cs

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
namespace SqlStreamStore.HAL.Resources
1+
namespace SqlStreamStore.HAL.AllStreamMessage
22
{
33
using System;
44
using System.Net.Http;
55
using System.Threading;
66
using System.Threading.Tasks;
77
using Halcyon.HAL;
8+
using SqlStreamStore.HAL.Resources;
89
using SqlStreamStore.Streams;
910

1011
internal class AllStreamMessageResource : IResource
@@ -31,12 +32,23 @@ public async Task<Response> Get(
3132
{
3233
var message = await operation.Invoke(_streamStore, cancellationToken);
3334

35+
var links = TheLinks.RootedAt("../")
36+
.Index()
37+
.Find()
38+
.Add(Constants.Relations.Message, $"stream/{message.Position}").Self()
39+
.Add(
40+
Constants.Relations.Feed,
41+
LinkFormatter.FormatBackwardLink(
42+
Constants.Streams.All,
43+
Constants.MaxCount,
44+
Position.End,
45+
false));
46+
3447
if(message.MessageId == Guid.Empty)
3548
{
3649
return new Response(
37-
new HALResponse(new HALModelConfig())
38-
.AddLinks(Links.Feed())
39-
.AddLinks(Links.Find()),
50+
new HALResponse(null)
51+
.AddLinks(links),
4052
404);
4153
}
4254

@@ -53,34 +65,7 @@ public async Task<Response> Get(
5365
message.Type,
5466
payload,
5567
metadata = message.JsonMetadata
56-
}).AddLinks(
57-
Links.Self(message),
58-
Links.Message(message),
59-
Links.Feed(),
60-
Links.Find()));
61-
}
62-
63-
private static class Links
64-
{
65-
public static Link Find() => SqlStreamStore.HAL.Links.Find("../streams/{streamId}");
66-
67-
public static Link Last()
68-
=> new Link(
69-
Constants.Relations.Last,
70-
LinkFormatter.FormatBackwardLink(
71-
$"../{Constants.Streams.All}",
72-
Constants.MaxCount,
73-
Position.End,
74-
false));
75-
76-
public static Link Self(StreamMessage message)
77-
=> new Link(Constants.Relations.Self, $"{message.Position}");
78-
79-
public static Link Message(StreamMessage message)
80-
=> new Link(Constants.Relations.Message, $"{message.Position}");
81-
82-
public static Link Feed()
83-
=> new Link(Constants.Relations.Feed, Last().Href);
68+
}).AddLinks(links));
8469
}
8570
}
8671
}

src/SqlStreamStore.HAL/Resources/ReadAllStreamMessageOperation.cs renamed to src/SqlStreamStore.HAL/AllStreamMessage/ReadAllStreamMessageOperation.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
namespace SqlStreamStore.HAL.Resources
1+
namespace SqlStreamStore.HAL.AllStreamMessage
22
{
33
using System.Linq;
44
using System.Threading;
55
using System.Threading.Tasks;
66
using Microsoft.AspNetCore.Http;
7+
using SqlStreamStore.HAL.Resources;
78
using SqlStreamStore.Streams;
89

910
internal class ReadAllStreamMessageOperation : IStreamStoreOperation<StreamMessage>

src/SqlStreamStore.HAL/AllStreamOptionsMiddleware.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading.Tasks;
55
using Microsoft.AspNetCore.Builder;
66
using Microsoft.AspNetCore.Http;
7+
using SqlStreamStore.HAL.AllStreamMessage;
78
using SqlStreamStore.HAL.Resources;
89
using MidFunc = System.Func<
910
Microsoft.AspNetCore.Http.HttpContext,

src/SqlStreamStore.HAL/ReadAllStreamMiddleware.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace SqlStreamStore.HAL
22
{
33
using Microsoft.AspNetCore.Builder;
44
using Microsoft.AspNetCore.Http;
5+
using SqlStreamStore.HAL.AllStreamMessage;
56
using SqlStreamStore.HAL.Resources;
67
using MidFunc = System.Func<
78
Microsoft.AspNetCore.Http.HttpContext,

0 commit comments

Comments
 (0)