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

Commit c10b608

Browse files
author
Mathew McLoughlin
committed
adding absolute Urls
1 parent adc9188 commit c10b608

6 files changed

Lines changed: 67 additions & 19 deletions

File tree

SqlStreamStore.HAL/App.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
3+
<connectionStrings>
4+
<add name="SqlStreamStore" connectionString="Server=.;Database=SqlStreamStore;Trusted_Connection=True;"/>
5+
</connectionStrings>
36
<startup>
47
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
58
</startup>

SqlStreamStore.HAL/HALResponse.cs

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using Jil;
4+
using Nancy.Helpers;
35
using SqlStreamStore.Streams;
46

57
namespace SqlStreamStore.HAL
@@ -33,16 +35,16 @@ public static HalResponse GetPage(StreamMessage[] messages, int pageSize, string
3335
return new HalResponse
3436
{
3537
Links = links,
36-
Embedded = new { Page = messages.Select(ToPageResponse).ToList() },
38+
Embedded = new { Page = messages.Select(m => ToPageResponse(path, m)).ToList() },
3739
Count = messages.Length
3840
};
3941
}
4042

41-
public static object GetMessage(StreamMessage m)
43+
public static object GetMessage(string path, StreamMessage m)
4244
{
4345
return new
4446
{
45-
_links = Links.CreateItemLink(m.Position),
47+
_links = Links.CreateItemLink(path, m.Position),
4648
m.Position,
4749
m.CreatedUtc,
4850
m.MessageId,
@@ -54,11 +56,11 @@ public static object GetMessage(StreamMessage m)
5456
};
5557
}
5658

57-
static object ToPageResponse(StreamMessage m)
59+
static object ToPageResponse(string path, StreamMessage m)
5860
{
5961
return new
6062
{
61-
_links = Links.CreateItemLink(m.Position),
63+
_links = Links.CreateItemLink(path, m.Position),
6264
m.Position,
6365
m.CreatedUtc,
6466
m.MessageId,
@@ -78,19 +80,51 @@ class Links
7880

7981
public static Links CreatePaginationLinks(string path, long positionOfFirstEvent, long positionOfLastEvent, int numberOfEvents, int pageSize, int direction)
8082
{
83+
var self = new Uri(path)
84+
.AddQuery("position", positionOfFirstEvent)
85+
.AddQuery("direction", direction == 1 ? "forwards" : "backwards")
86+
.ToString();
87+
88+
var next = new Uri(path)
89+
.AddQuery("position", positionOfLastEvent + direction)
90+
.AddQuery("direction", direction == 1 ? "forwards" : "backwards")
91+
.ToString();
92+
8193
return new Links
8294
{
83-
Self = new { Href = path + $"?position={positionOfFirstEvent}&direction={(direction == 1 ? "forwards" : "backwards")}" },
84-
Next = numberOfEvents < pageSize ? null : new { Href = $"{path}?position={positionOfLastEvent + direction}&direction={(direction == 1 ? "forwards" : "backwards")}" }
95+
Self = new { Href = self },
96+
Next = numberOfEvents < pageSize ? null : new { Href = next }
8597
};
8698
}
8799

88-
public static Links CreateItemLink(long position)
100+
public static Links CreateItemLink(string path, long position)
89101
{
102+
var self = new Uri(path)
103+
.AddQuery("position", position)
104+
.ToString();
105+
90106
return new Links
91107
{
92-
Self = new { Href = "/stream/" + position },
108+
Self = new { Href = self }
93109
};
94110
}
95111
}
112+
113+
public static class HttpExtensions
114+
{
115+
public static Uri AddQuery(this Uri uri, string name, object value)
116+
{
117+
var ub = new UriBuilder(uri);
118+
119+
// decodes urlencoded pairs from uri.Query to HttpValueCollection
120+
var httpValueCollection = HttpUtility.ParseQueryString(uri.Query);
121+
122+
httpValueCollection.Add(name, value.ToString());
123+
124+
// urlencodes the whole HttpValueCollection
125+
ub.Query = httpValueCollection.ToString();
126+
127+
return ub.Uri;
128+
}
129+
}
96130
}

SqlStreamStore.HAL/Program.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Owin.Hosting;
22
using SqlStreamStore.Streams;
33
using System;
4+
using System.Configuration;
45
using System.Linq;
56

67
namespace SqlStreamStore.HAL
@@ -10,10 +11,13 @@ class Program
1011
static void Main()
1112
{
1213
var streamStore = new InMemoryStreamStore();
13-
var messages = SeedData.Get(40);
14+
15+
var messages = SeedData.Get(40).ToList();
16+
var messages2 = SeedData.Get(40).ToList();
1417

1518
streamStore.AppendToStream("SomeStream", ExpectedVersion.Any, messages).GetAwaiter().GetResult();
16-
streamStore.AppendToStream("SomeOtherStream", ExpectedVersion.Any, messages).GetAwaiter().GetResult();
19+
streamStore.AppendToStream("SomeOtherStream", ExpectedVersion.Any, messages2).GetAwaiter().GetResult();
20+
streamStore.DeleteMessage("SomeStream", messages.ToList()[21].MessageId).GetAwaiter().GetResult();
1721

1822
var settings = new SqlStreamStoreHalSettings
1923
{

SqlStreamStore.HAL/SqlStreamStore.HAL.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,15 @@
7373
<HintPath>..\packages\SqlStreamStore.0.4.3\lib\Portable-Net45+Win8+WP8+WPA81\SqlStreamStore.dll</HintPath>
7474
<Private>True</Private>
7575
</Reference>
76+
<Reference Include="SqlStreamStore.MsSql, Version=0.4.3.0, Culture=neutral, processorArchitecture=MSIL">
77+
<HintPath>..\packages\SqlStreamStore.MsSql.0.4.3\lib\net451\SqlStreamStore.MsSql.dll</HintPath>
78+
<Private>True</Private>
79+
</Reference>
7680
<Reference Include="System" />
81+
<Reference Include="System.Configuration" />
7782
<Reference Include="System.Core" />
7883
<Reference Include="Microsoft.CSharp" />
84+
<Reference Include="System.Data" />
7985
<Reference Include="System.Runtime.Serialization" />
8086
</ItemGroup>
8187
<ItemGroup>

SqlStreamStore.HAL/SqlStreamStoreHalModule.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,32 @@ public SqlStreamStoreHalModule(SqlStreamStoreHalSettings settings)
1919
Get["stream", true] = async (_, ct) =>
2020
{
2121
var direction = GetDirection();
22-
var position = GetPosition();
22+
var position = GetPosition("position");
2323

2424
var readAllPage = await GetReadAllPage(settings.Store, settings.PageSize, position, direction);
25-
return Response.AsJson(HalResponse.GetPage(readAllPage.Messages, settings.PageSize, Request.Path, direction));
25+
return Response.AsJson(HalResponse.GetPage(readAllPage.Messages, settings.PageSize, Request.Url.SiteBase + Request.Path, direction));
2626
};
2727

2828
Get["stream/{position}", true] = async (args, ct) =>
2929
{
3030
var message = await settings.Store.ReadAllForwards(args.Position, 1);
31-
var model = HalResponse.GetMessage(message.Messages[0]);
31+
var model = HalResponse.GetMessage(Request.Url.SiteBase + Request.Path, message.Messages[0]);
3232
return FormatterExtensions.AsJson(Response, model);
3333
};
3434

3535
Get["streams/{streamId}", true] = async (args, ct) =>
3636
{
3737
var direction = GetDirection();
38-
var position = GetPosition();
38+
var position = GetPosition("version");
3939

4040
ReadStreamPage readAllPage = await GetReadStreamPage(args.StreamId, settings.Store, settings.PageSize, (int?)position, direction);
41-
return Response.AsJson(HalResponse.GetPage(readAllPage.Messages, settings.PageSize, Request.Path, direction));
41+
return Response.AsJson(HalResponse.GetPage(readAllPage.Messages, settings.PageSize, Request.Url.SiteBase + Request.Path, direction));
4242
};
4343
}
4444

45-
private long? GetPosition()
45+
private long? GetPosition(string parameterName)
4646
{
47-
string position = Request.Query.Position;
47+
string position = Request.Query[parameterName];
4848

4949
if (position == null)
5050
{

SqlStreamStore.HAL/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
<package id="Owin" version="1.0" targetFramework="net46" />
1111
<package id="Sigil" version="4.7.0" targetFramework="net46" />
1212
<package id="SqlStreamStore" version="0.4.3" targetFramework="net46" />
13+
<package id="SqlStreamStore.MsSql" version="0.4.3" targetFramework="net46" />
1314
</packages>

0 commit comments

Comments
 (0)