1- using System ;
2- using System . Collections . Generic ;
1+ using System . Collections . Generic ;
32using System . Threading . Tasks ;
43using Nancy ;
54using SqlStreamStore . Streams ;
@@ -8,7 +7,7 @@ namespace SqlStreamStore.HAL
87{
98 public class SqlStreamStoreHalModule : NancyModule
109 {
11- readonly Dictionary < string , int > _directionLookup = new Dictionary < string , int >
10+ private readonly Dictionary < string , int > _directionLookup = new Dictionary < string , int >
1211 {
1312 { "forwards" , Direction . Forwards } ,
1413 { "backwards" , Direction . Backwards }
@@ -25,46 +24,63 @@ public SqlStreamStoreHalModule(SqlStreamStoreHalSettings settings)
2524 var readAllPage = await GetReadAllPage ( settings . Store , settings . PageSize , position , direction ) ;
2625 return Response . AsJson ( HalResponse . GetPage ( readAllPage . Messages , settings . PageSize , Request . Path , direction ) ) ;
2726 } ;
27+
28+ Get [ "stream/{position}" , true ] = async ( args , ct ) =>
29+ {
30+ var message = await settings . Store . ReadAllForwards ( args . Position , 1 ) ;
31+ var model = HalResponse . GetMessage ( message . Messages [ 0 ] ) ;
32+ return FormatterExtensions . AsJson ( Response , model ) ;
33+ } ;
2834
29- Get [ "stream/{position }" , true ] = async ( arg , ct ) =>
35+ Get [ "streams/{streamId }" , true ] = async ( args , ct ) =>
3036 {
31- var message = await settings . Store . ReadAllForwards ( arg . Position , 1 ) ;
32- return FormatterExtensions . AsJson ( Response , HalResponse . GetMessage ( message . Messages [ 0 ] ) ) ;
37+ var direction = GetDirection ( ) ;
38+ var position = GetPosition ( ) ;
39+
40+ ReadStreamPage readAllPage = await GetReadStreamPage ( args . StreamId , settings . Store , settings . PageSize , ( int ? ) position , direction ) ;
41+ return Response . AsJson ( HalResponse . GetPage ( readAllPage . Messages , settings . PageSize , Request . Path , direction ) ) ;
3342 } ;
3443 }
3544
36- private long GetPosition ( )
45+ private long ? GetPosition ( )
3746 {
38- long position ;
47+ string position = Request . Query . Position ;
48+
49+ if ( position == null )
50+ {
51+ return null ;
52+ }
3953
40- if ( ! long . TryParse ( ( string ) Request . Query . Position ?? "0" , out position ) )
54+ long longPosition ;
55+ if ( ! long . TryParse ( position , out longPosition ) )
4156 {
42- throw new Exception ( "position must be a long" ) ;
57+ return null ;
4358 }
4459
45- return position ;
60+ return longPosition ;
4661 }
4762
4863 private int GetDirection ( )
4964 {
50- string direction = Request . Query . Direction ;
51-
52- if ( ! _directionLookup . ContainsKey ( direction ) )
53- {
54- throw new Exception ( "direction parameter must be {forwards} or {backwards}" ) ;
55- }
56-
57- return _directionLookup [ direction ] ;
65+ var direction = ( string ) Request . Query . Direction ?? "forwards" ;
66+ return ! _directionLookup . ContainsKey ( direction ) ? Direction . Forwards : _directionLookup [ direction ] ;
5867 }
5968
60- private Task < ReadAllPage > GetReadAllPage ( IReadonlyStreamStore store , int pageSize , long ? position , int direction )
69+ private static Task < ReadAllPage > GetReadAllPage ( IReadonlyStreamStore store , int pageSize , long ? position , int direction )
6170 {
6271 return direction == Direction . Forwards ?
6372 store . ReadAllForwards ( position ?? Position . Start , pageSize ) :
6473 store . ReadAllBackwards ( position ?? Position . End , pageSize ) ;
6574 }
6675
67- static class Direction
76+ private static Task < ReadStreamPage > GetReadStreamPage ( string streamId , IReadonlyStreamStore store , int pageSize , int ? position , int direction )
77+ {
78+ return direction == Direction . Forwards ?
79+ store . ReadStreamForwards ( streamId , position ?? ( int ) Position . Start , pageSize ) :
80+ store . ReadStreamBackwards ( streamId , position ?? ( int ) Position . End , pageSize ) ;
81+ }
82+
83+ private static class Direction
6884 {
6985 public static int Forwards => 1 ;
7086 public static int Backwards => - 1 ;
0 commit comments