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

Commit 56254eb

Browse files
author
Mathew McLoughlin
committed
can go fowards and backwards. Removed prev link
1 parent ca25bb1 commit 56254eb

3 files changed

Lines changed: 140 additions & 63 deletions

File tree

SqlStreamStore.HAL/HALResponse.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Newtonsoft.Json;
4+
using SqlStreamStore.Streams;
5+
6+
namespace SqlStreamStore.HAL
7+
{
8+
class HALResponse
9+
{
10+
[JsonProperty(PropertyName = "_links")]
11+
public Links Links { get; private set; }
12+
13+
public int Count { get; set; }
14+
15+
16+
[JsonProperty(PropertyName = "_embedded")]
17+
public Embedded Embedded { get; private set; }
18+
19+
public static HALResponse Create(StreamMessage[] messages, int pageSize, string path, int direction)
20+
{
21+
if (messages.Length <= 0)
22+
{
23+
return null;
24+
}
25+
26+
var links = Links.CreateLinks(
27+
path,
28+
messages.First().Position,
29+
messages.Last().Position,
30+
messages.Length,
31+
pageSize,
32+
direction
33+
);
34+
35+
return new HALResponse
36+
{
37+
Links = links,
38+
Embedded = new Embedded { Page = messages.Select(m => m).Cast<object>().ToList() },
39+
Count = messages.Length
40+
};
41+
}
42+
}
43+
44+
class Links
45+
{
46+
public Link Self { get; private set; }
47+
48+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
49+
public Link Next { get; private set; }
50+
51+
public static Links CreateLinks(string path, long positionOfFirstEvent, long positionOfLastEvent, int numberOfEvents, int pageSize, int direction)
52+
{
53+
return new Links
54+
{
55+
Self = Link.CreateSelfLink(path, positionOfFirstEvent),
56+
Next = Link.CreateNextLink(path, positionOfLastEvent + direction, pageSize, numberOfEvents),
57+
};
58+
}
59+
}
60+
61+
class Link
62+
{
63+
public string Href { get; }
64+
65+
Link(string path, long position)
66+
{
67+
Href = path + "?position=" + position;
68+
}
69+
70+
public static Link CreateSelfLink(string path, long position)
71+
{
72+
return new Link(path, position);
73+
}
74+
75+
public static Link CreateNextLink(string path, long position, int pageSize, int actualPageSize)
76+
{
77+
if (actualPageSize < pageSize)
78+
{
79+
return null;
80+
}
81+
82+
return new Link(path, position);
83+
}
84+
}
85+
86+
class Embedded
87+
{
88+
public List<object> Page { get; set; }
89+
}
90+
91+
public static class Direction
92+
{
93+
public static int Forwards => 1;
94+
public static int Backwards => -1;
95+
}
96+
}

SqlStreamStore.HAL/Program.cs

Lines changed: 43 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ static void Main(string[] args)
2626

2727
streamStore.AppendToStream("SomeStream", ExpectedVersion.Any, messages);
2828

29-
var settings = new HALSettings { Url = "", Store = streamStore };
29+
var settings = new HALSettings
30+
{
31+
Url = "Something",
32+
Store = streamStore,
33+
PageSize = 20
34+
};
35+
3036
var baseUrl = "http://localhost:8080";
3137

3238
using (WebApp.Start(baseUrl, app => app.Use(HALMiddleware.Handle(settings))))
@@ -46,18 +52,16 @@ public static MidFunc Handle(HALSettings settings)
4652
var config = new HttpConfiguration();
4753

4854
var container = new TinyIoCContainer();
49-
container.Register(settings.Store);
50-
55+
container.Register(settings);
5156

5257
config.DependencyResolver = new Resolver(container);
5358

5459
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
5560

56-
5761
config.Routes.MapHttpRoute(
5862
name: "SqlStreamStoreHAL",
59-
routeTemplate: settings.Url + "stream/{position}",
60-
defaults: new { controller = "SqlStreamStoreHAL", position = RouteParameter.Optional, action = "index" }
63+
routeTemplate: settings.Url + "/stream/{direction}/{position}",
64+
defaults: new { controller = "SqlStreamStoreHAL", position = RouteParameter.Optional, action = "index", direction = "forwards" }
6165
);
6266

6367

@@ -71,34 +75,49 @@ public static MidFunc Handle(HALSettings settings)
7175

7276
public class SqlStreamStoreHALController : ApiController
7377
{
74-
private IReadonlyStreamStore _stream;
78+
private readonly Dictionary<string, int> _directionLookup = new Dictionary<string, int>
79+
{
80+
{"forwards", 1},
81+
{"backwards", -1}
82+
83+
};
84+
85+
private readonly IReadonlyStreamStore _store;
7586

76-
public SqlStreamStoreHALController(IReadonlyStreamStore stream)
87+
private readonly int _pageSize;
88+
89+
public SqlStreamStoreHALController(HALSettings settings)
7790
{
78-
_stream = stream;
91+
_store = settings.Store;
92+
_pageSize = settings.PageSize;
7993
}
8094

8195
[HttpGet]
82-
public IHttpActionResult Index(long? position = null)
96+
public IHttpActionResult Index(string direction, long? position = null)
8397
{
84-
98+
var dir = _directionLookup[direction];
99+
100+
var readAllPage = GetStream(position, dir);
85101

86-
var readAllPage = _stream.ReadAllForwards(position ?? Position.Start, 20).GetAwaiter().GetResult();
102+
var response = HALResponse.Create(readAllPage.Messages, _pageSize, Request.RequestUri.AbsolutePath, dir);
87103

88-
var stringBuilder = new StringBuilder();
104+
return Ok(response);
105+
}
89106

90-
foreach (var e in readAllPage.Messages)
107+
private ReadAllPage GetStream(long? position, int direction)
108+
{
109+
ReadAllPage readAllPage;
110+
111+
if (direction == Direction.Forwards)
91112
{
92-
stringBuilder.AppendLine(e.JsonData + Environment.NewLine);
113+
readAllPage = _store.ReadAllForwards(position ?? Position.Start, _pageSize).GetAwaiter().GetResult();
93114
}
94-
95-
var response = new HALStream
115+
else
96116
{
97-
Links = Links.CreateLinks(Request.RequestUri.AbsolutePath, position ?? Position.Start, 20),
98-
Embedded = new Embedded { Stream = readAllPage.Messages.Select(m => m).Cast<object>().ToList() }
99-
};
117+
readAllPage = _store.ReadAllBackwards(position ?? Position.End, _pageSize).GetAwaiter().GetResult();
118+
}
100119

101-
return Ok(response);
120+
return readAllPage;
102121
}
103122
}
104123

@@ -141,51 +160,12 @@ public IEnumerable<Object> GetServices(Type serviceType)
141160
}
142161
}
143162

144-
class HALSettings
163+
public class HALSettings
145164
{
146165
public IReadonlyStreamStore Store { get; set; }
147-
public string Url { get; set; }
148-
}
149166

150-
class HALStream
151-
{
152-
[JsonProperty(PropertyName = "_links")]
153-
public Links Links { get; set; }
154-
155-
[JsonProperty(PropertyName = "_embedded")]
156-
public Embedded Embedded { get; set; }
157-
}
158-
class Links
159-
{
160-
public Link Self { get; set; }
161-
162-
public Link Next { get; set; }
163-
164-
public Link Prev { get; set; }
165-
166-
public class Link
167-
{
168-
public string Href { get; private set; }
169-
170-
public static Link Create(string href)
171-
{
172-
return new Link { Href = href };
173-
}
174-
}
175-
176-
public static Links CreateLinks(string path, long position, long pageSize)
177-
{
178-
return new Links
179-
{
180-
Self = Link.Create(path + (position != Position.Start ? "?position=" + position : "")),
181-
Next = Link.Create(path + "?position=" + (position + 1 + pageSize)),
182-
Prev = Link.Create(position == Position.Start ? (string)null : path + "?position=" + (position - 1))
183-
};
184-
}
185-
}
167+
public string Url { get; set; }
186168

187-
class Embedded
188-
{
189-
public List<object> Stream { get; set; }
169+
public int PageSize { get; set; }
190170
}
191171
}

SqlStreamStore.HAL/SqlStreamStore.HAL.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Reference Include="System.Xml" />
8585
</ItemGroup>
8686
<ItemGroup>
87+
<Compile Include="HALResponse.cs" />
8788
<Compile Include="Program.cs" />
8889
<Compile Include="Properties\AssemblyInfo.cs" />
8990
<Compile Include="SeedData.cs" />

0 commit comments

Comments
 (0)