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

Commit f665007

Browse files
author
Mathew McLoughlin
committed
more tidying and adding in single message view
1 parent 56254eb commit f665007

4 files changed

Lines changed: 111 additions & 104 deletions

File tree

SqlStreamStore.HAL/HALResponse.cs

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,24 @@
55

66
namespace SqlStreamStore.HAL
77
{
8-
class HALResponse
8+
class HalResponse
99
{
1010
[JsonProperty(PropertyName = "_links")]
1111
public Links Links { get; private set; }
1212

13-
public int Count { get; set; }
14-
13+
public int Count { get; private set; }
1514

1615
[JsonProperty(PropertyName = "_embedded")]
17-
public Embedded Embedded { get; private set; }
16+
public object Embedded { get; private set; }
1817

19-
public static HALResponse Create(StreamMessage[] messages, int pageSize, string path, int direction)
18+
public static HalResponse GetPage(StreamMessage[] messages, int pageSize, string path, int direction)
2019
{
2120
if (messages.Length <= 0)
2221
{
2322
return null;
2423
}
2524

26-
var links = Links.CreateLinks(
25+
var links = Links.CreatePaginationLinks(
2726
path,
2827
messages.First().Position,
2928
messages.Last().Position,
@@ -32,65 +31,58 @@ public static HALResponse Create(StreamMessage[] messages, int pageSize, string
3231
direction
3332
);
3433

35-
return new HALResponse
34+
return new HalResponse
3635
{
3736
Links = links,
38-
Embedded = new Embedded { Page = messages.Select(m => m).Cast<object>().ToList() },
37+
Embedded = new { Page = messages.Select(ToResponse).ToList() },
3938
Count = messages.Length
4039
};
4140
}
42-
}
4341

44-
class Links
45-
{
46-
public Link Self { get; private set; }
47-
48-
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
49-
public Link Next { get; private set; }
42+
public static object GetMessage(StreamMessage message)
43+
{
44+
return ToResponse(message);
45+
}
5046

51-
public static Links CreateLinks(string path, long positionOfFirstEvent, long positionOfLastEvent, int numberOfEvents, int pageSize, int direction)
47+
static object ToResponse(StreamMessage m)
5248
{
53-
return new Links
49+
return new
5450
{
55-
Self = Link.CreateSelfLink(path, positionOfFirstEvent),
56-
Next = Link.CreateNextLink(path, positionOfLastEvent + direction, pageSize, numberOfEvents),
51+
Links = Links.CreateItemLink(m.Position),
52+
m.Position,
53+
m.CreatedUtc,
54+
m.MessageId,
55+
JsonData = JsonConvert.DeserializeObject(m.JsonData),
56+
m.JsonMetadata,
57+
m.StreamVersion,
58+
m.StreamId,
59+
m.Type
5760
};
5861
}
5962
}
6063

61-
class Link
64+
class Links
6265
{
63-
public string Href { get; }
66+
public object Self { get; private set; }
6467

65-
Link(string path, long position)
66-
{
67-
Href = path + "?position=" + position;
68-
}
68+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
69+
public object Next { get; private set; }
6970

70-
public static Link CreateSelfLink(string path, long position)
71+
public static Links CreatePaginationLinks(string path, long positionOfFirstEvent, long positionOfLastEvent, int numberOfEvents, int pageSize, int direction)
7172
{
72-
return new Link(path, position);
73+
return new Links
74+
{
75+
Self = new { Href = path + "?position=" + positionOfFirstEvent },
76+
Next = numberOfEvents < pageSize ? null : new { Href = path + "?position=" + positionOfLastEvent + direction }
77+
};
7378
}
7479

75-
public static Link CreateNextLink(string path, long position, int pageSize, int actualPageSize)
80+
public static Links CreateItemLink(long position)
7681
{
77-
if (actualPageSize < pageSize)
82+
return new Links
7883
{
79-
return null;
80-
}
81-
82-
return new Link(path, position);
84+
Self = new { Href = "/streamMessage?position=" + position },
85+
};
8386
}
8487
}
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-
}
88+
}

SqlStreamStore.HAL/Program.cs

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>,
1111
System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>>;
1212
using System.Collections.Generic;
13-
using System.Text;
13+
using System.IO;
1414
using System.Linq;
15-
using Newtonsoft.Json;
1615
using Newtonsoft.Json.Serialization;
1716

1817
namespace SqlStreamStore.HAL
@@ -28,7 +27,6 @@ static void Main(string[] args)
2827

2928
var settings = new HALSettings
3029
{
31-
Url = "Something",
3230
Store = streamStore,
3331
PageSize = 20
3432
};
@@ -55,15 +53,21 @@ public static MidFunc Handle(HALSettings settings)
5553
container.Register(settings);
5654

5755
config.DependencyResolver = new Resolver(container);
58-
5956
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
6057

58+
var baseUrl = settings.BaseUrl == null ? "" : settings.BaseUrl + "/";
59+
6160
config.Routes.MapHttpRoute(
62-
name: "SqlStreamStoreHAL",
63-
routeTemplate: settings.Url + "/stream/{direction}/{position}",
64-
defaults: new { controller = "SqlStreamStoreHAL", position = RouteParameter.Optional, action = "index", direction = "forwards" }
65-
);
61+
"SqlStreamStoreHAL.Paged",
62+
Path.Combine(baseUrl, "stream/{direction}/{position}"),
63+
new { controller = "SqlStreamStoreHAL", position = RouteParameter.Optional, action = "index", direction = "forwards" }
64+
);
6665

66+
config.Routes.MapHttpRoute(
67+
"SqlStreamStoreHAL.Message",
68+
Path.Combine(baseUrl, "streanMessage/{position}"),
69+
new { controller = "SqlStreamStoreHAL", action = "message", position = RouteParameter.Optional }
70+
);
6771

6872
var appBuilder = new AppBuilder();
6973
appBuilder.UseWebApi(config);
@@ -73,54 +77,6 @@ public static MidFunc Handle(HALSettings settings)
7377
}
7478
}
7579

76-
public class SqlStreamStoreHALController : ApiController
77-
{
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;
86-
87-
private readonly int _pageSize;
88-
89-
public SqlStreamStoreHALController(HALSettings settings)
90-
{
91-
_store = settings.Store;
92-
_pageSize = settings.PageSize;
93-
}
94-
95-
[HttpGet]
96-
public IHttpActionResult Index(string direction, long? position = null)
97-
{
98-
var dir = _directionLookup[direction];
99-
100-
var readAllPage = GetStream(position, dir);
101-
102-
var response = HALResponse.Create(readAllPage.Messages, _pageSize, Request.RequestUri.AbsolutePath, dir);
103-
104-
return Ok(response);
105-
}
106-
107-
private ReadAllPage GetStream(long? position, int direction)
108-
{
109-
ReadAllPage readAllPage;
110-
111-
if (direction == Direction.Forwards)
112-
{
113-
readAllPage = _store.ReadAllForwards(position ?? Position.Start, _pageSize).GetAwaiter().GetResult();
114-
}
115-
else
116-
{
117-
readAllPage = _store.ReadAllBackwards(position ?? Position.End, _pageSize).GetAwaiter().GetResult();
118-
}
119-
120-
return readAllPage;
121-
}
122-
}
123-
12480
class Resolver : IDependencyResolver
12581
{
12682
private TinyIoCContainer _container;
@@ -139,7 +95,7 @@ public void Dispose()
13995
{
14096
}
14197

142-
public Object GetService(Type serviceType)
98+
public object GetService(Type serviceType)
14399
{
144100
if (!_container.CanResolve(serviceType))
145101
{
@@ -149,7 +105,7 @@ public Object GetService(Type serviceType)
149105
return _container.Resolve(serviceType);
150106
}
151107

152-
public IEnumerable<Object> GetServices(Type serviceType)
108+
public IEnumerable<object> GetServices(Type serviceType)
153109
{
154110
if (!_container.CanResolve(serviceType))
155111
{
@@ -164,7 +120,7 @@ public class HALSettings
164120
{
165121
public IReadonlyStreamStore Store { get; set; }
166122

167-
public string Url { get; set; }
123+
public string BaseUrl { get; set; }
168124

169125
public int PageSize { get; set; }
170126
}

SqlStreamStore.HAL/SqlStreamStore.HAL.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<Compile Include="Program.cs" />
8989
<Compile Include="Properties\AssemblyInfo.cs" />
9090
<Compile Include="SeedData.cs" />
91+
<Compile Include="SqlStreamStoreHALController.cs" />
9192
<Compile Include="TinyIoC.cs" />
9293
</ItemGroup>
9394
<ItemGroup>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Collections.Generic;
2+
using System.Web.Http;
3+
using SqlStreamStore.Streams;
4+
5+
namespace SqlStreamStore.HAL
6+
{
7+
public class SqlStreamStoreHalController : ApiController
8+
{
9+
readonly Dictionary<string, int> _directionLookup = new Dictionary<string, int>
10+
{
11+
{ "forwards", Direction.Forwards },
12+
{ "backwards", Direction.Backwards }
13+
14+
};
15+
16+
readonly IReadonlyStreamStore _store;
17+
18+
readonly int _pageSize;
19+
20+
public SqlStreamStoreHalController(HALSettings settings)
21+
{
22+
_store = settings.Store;
23+
_pageSize = settings.PageSize;
24+
}
25+
26+
[HttpGet]
27+
public IHttpActionResult Index(string direction, long? position = null)
28+
{
29+
var dir = _directionLookup[direction];
30+
var readAllPage = GetStream(position, dir);
31+
var response = HalResponse.GetPage(readAllPage.Messages, _pageSize, Request.RequestUri.AbsolutePath, dir);
32+
33+
return Ok(response);
34+
}
35+
36+
[HttpGet]
37+
public IHttpActionResult Message(long position)
38+
{
39+
var message = _store.ReadAllForwards(position, 1).GetAwaiter().GetResult();
40+
dynamic response = HalResponse.GetMessage(message.Messages[0]);
41+
42+
return Ok(response);
43+
}
44+
45+
private ReadAllPage GetStream(long? position, int direction)
46+
{
47+
return direction == Direction.Forwards ?
48+
_store.ReadAllForwards(position ?? Position.Start, _pageSize).GetAwaiter().GetResult() :
49+
_store.ReadAllBackwards(position ?? Position.End, _pageSize).GetAwaiter().GetResult();
50+
}
51+
52+
static class Direction
53+
{
54+
public static int Forwards => 1;
55+
public static int Backwards => -1;
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)