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

Commit ca25bb1

Browse files
Mathew McLoughlinMathew McLoughlin
authored andcommitted
fleshing out the json response
1 parent d21f9cc commit ca25bb1

5 files changed

Lines changed: 4156 additions & 27 deletions

File tree

SqlStreamStore.HAL/App.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
1010
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
1111
</dependentAssembly>
12+
<dependentAssembly>
13+
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
14+
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
15+
</dependentAssembly>
1216
</assemblyBinding>
1317
</runtime>
1418
</configuration>

SqlStreamStore.HAL/Program.cs

Lines changed: 130 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44
using SqlStreamStore.Streams;
55
using System;
66
using System.Web.Http;
7+
using System.Web.Http.Dependencies;
8+
using TinyIoC;
79
using MidFunc = System.Func<
810
System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>,
911
System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>>;
12+
using System.Collections.Generic;
13+
using System.Text;
14+
using System.Linq;
15+
using Newtonsoft.Json;
16+
using Newtonsoft.Json.Serialization;
1017

1118
namespace SqlStreamStore.HAL
1219
{
@@ -19,7 +26,7 @@ static void Main(string[] args)
1926

2027
streamStore.AppendToStream("SomeStream", ExpectedVersion.Any, messages);
2128

22-
var settings = new HALSettings { Url = "Stream" };
29+
var settings = new HALSettings { Url = "", Store = streamStore };
2330
var baseUrl = "http://localhost:8080";
2431

2532
using (WebApp.Start(baseUrl, app => app.Use(HALMiddleware.Handle(settings))))
@@ -37,43 +44,148 @@ public static MidFunc Handle(HALSettings settings)
3744
return next =>
3845
{
3946
var config = new HttpConfiguration();
47+
48+
var container = new TinyIoCContainer();
49+
container.Register(settings.Store);
50+
51+
52+
config.DependencyResolver = new Resolver(container);
53+
54+
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
55+
56+
4057
config.Routes.MapHttpRoute(
4158
name: "SqlStreamStoreHAL",
42-
routeTemplate: settings.Url + "/{checkpoint}",
43-
defaults: new { controller = "SqlStreamStoreHAL", checkpoint = RouteParameter.Optional , action = "index" }
59+
routeTemplate: settings.Url + "stream/{position}",
60+
defaults: new { controller = "SqlStreamStoreHAL", position = RouteParameter.Optional, action = "index" }
4461
);
4562

4663

4764
var appBuilder = new AppBuilder();
4865
appBuilder.UseWebApi(config);
49-
//appBuilder.Run(context =>
50-
//{
51-
// context.Response.ContentType = "text/plain";
52-
53-
// string output = string.Format(
54-
// "I'm running on {0} nFrom assembly {1}",
55-
// Environment.OSVersion,
56-
// System.Reflection.Assembly.GetEntryAssembly().FullName
57-
// );
58-
59-
// return context.Response.WriteAsync(output);
60-
//});
66+
6167
return appBuilder.Build();
6268
};
6369
}
6470
}
6571

6672
public class SqlStreamStoreHALController : ApiController
6773
{
74+
private IReadonlyStreamStore _stream;
75+
76+
public SqlStreamStoreHALController(IReadonlyStreamStore stream)
77+
{
78+
_stream = stream;
79+
}
80+
6881
[HttpGet]
69-
public string Index()
82+
public IHttpActionResult Index(long? position = null)
7083
{
71-
return "test";
84+
85+
86+
var readAllPage = _stream.ReadAllForwards(position ?? Position.Start, 20).GetAwaiter().GetResult();
87+
88+
var stringBuilder = new StringBuilder();
89+
90+
foreach (var e in readAllPage.Messages)
91+
{
92+
stringBuilder.AppendLine(e.JsonData + Environment.NewLine);
93+
}
94+
95+
var response = new HALStream
96+
{
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+
};
100+
101+
return Ok(response);
72102
}
73103
}
74104

75-
internal class HALSettings
105+
class Resolver : IDependencyResolver
76106
{
107+
private TinyIoCContainer _container;
108+
109+
public Resolver(TinyIoCContainer container)
110+
{
111+
_container = container;
112+
}
113+
114+
public IDependencyScope BeginScope()
115+
{
116+
return this;
117+
}
118+
119+
public void Dispose()
120+
{
121+
}
122+
123+
public Object GetService(Type serviceType)
124+
{
125+
if (!_container.CanResolve(serviceType))
126+
{
127+
return null;
128+
}
129+
130+
return _container.Resolve(serviceType);
131+
}
132+
133+
public IEnumerable<Object> GetServices(Type serviceType)
134+
{
135+
if (!_container.CanResolve(serviceType))
136+
{
137+
return Enumerable.Empty<object>();
138+
}
139+
140+
return _container.ResolveAll(serviceType);
141+
}
142+
}
143+
144+
class HALSettings
145+
{
146+
public IReadonlyStreamStore Store { get; set; }
77147
public string Url { get; set; }
78148
}
149+
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+
}
186+
187+
class Embedded
188+
{
189+
public List<object> Stream { get; set; }
190+
}
79191
}

SqlStreamStore.HAL/SqlStreamStore.HAL.csproj

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,26 @@
3737
<HintPath>..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
3838
<Private>True</Private>
3939
</Reference>
40-
<Reference Include="Microsoft.Owin.Host.HttpListener, Version=2.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
41-
<HintPath>..\packages\Microsoft.Owin.Host.HttpListener.2.0.2\lib\net45\Microsoft.Owin.Host.HttpListener.dll</HintPath>
40+
<Reference Include="Microsoft.Owin.Host.HttpListener, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
41+
<HintPath>..\packages\Microsoft.Owin.Host.HttpListener.3.0.1\lib\net45\Microsoft.Owin.Host.HttpListener.dll</HintPath>
4242
<Private>True</Private>
4343
</Reference>
44-
<Reference Include="Microsoft.Owin.Hosting, Version=2.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
45-
<HintPath>..\packages\Microsoft.Owin.Hosting.2.0.2\lib\net45\Microsoft.Owin.Hosting.dll</HintPath>
44+
<Reference Include="Microsoft.Owin.Hosting, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
45+
<HintPath>..\packages\Microsoft.Owin.Hosting.3.0.1\lib\net45\Microsoft.Owin.Hosting.dll</HintPath>
4646
<Private>True</Private>
4747
</Reference>
48-
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
49-
<HintPath>..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
48+
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
49+
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
5050
<Private>True</Private>
5151
</Reference>
5252
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
5353
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
5454
<Private>True</Private>
5555
</Reference>
56+
<Reference Include="Sigil, Version=4.6.1.0, Culture=neutral, PublicKeyToken=2d06c3494341c8ab, processorArchitecture=MSIL">
57+
<HintPath>..\packages\Sigil.4.6.1\lib\net45\Sigil.dll</HintPath>
58+
<Private>True</Private>
59+
</Reference>
5660
<Reference Include="SqlStreamStore, Version=0.4.3.0, Culture=neutral, processorArchitecture=MSIL">
5761
<HintPath>..\packages\SqlStreamStore.0.4.3\lib\Portable-Net45+Win8+WP8+WPA81\SqlStreamStore.dll</HintPath>
5862
<Private>True</Private>
@@ -63,6 +67,7 @@
6367
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
6468
<Private>True</Private>
6569
</Reference>
70+
<Reference Include="System.Runtime.Serialization" />
6671
<Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6772
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
6873
<Private>True</Private>
@@ -82,6 +87,7 @@
8287
<Compile Include="Program.cs" />
8388
<Compile Include="Properties\AssemblyInfo.cs" />
8489
<Compile Include="SeedData.cs" />
90+
<Compile Include="TinyIoC.cs" />
8591
</ItemGroup>
8692
<ItemGroup>
8793
<None Include="App.config" />

0 commit comments

Comments
 (0)