44using SqlStreamStore . Streams ;
55using System ;
66using System . Web . Http ;
7+ using System . Web . Http . Dependencies ;
8+ using TinyIoC ;
79using 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
1118namespace 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}
0 commit comments