@@ -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}
0 commit comments