1- using System . Linq ;
1+ using System ;
2+ using System . Linq ;
23using Jil ;
4+ using Nancy . Helpers ;
35using SqlStreamStore . Streams ;
46
57namespace SqlStreamStore . HAL
@@ -33,16 +35,16 @@ public static HalResponse GetPage(StreamMessage[] messages, int pageSize, string
3335 return new HalResponse
3436 {
3537 Links = links ,
36- Embedded = new { Page = messages . Select ( ToPageResponse ) . ToList ( ) } ,
38+ Embedded = new { Page = messages . Select ( m => ToPageResponse ( path , m ) ) . ToList ( ) } ,
3739 Count = messages . Length
3840 } ;
3941 }
4042
41- public static object GetMessage ( StreamMessage m )
43+ public static object GetMessage ( string path , StreamMessage m )
4244 {
4345 return new
4446 {
45- _links = Links . CreateItemLink ( m . Position ) ,
47+ _links = Links . CreateItemLink ( path , m . Position ) ,
4648 m . Position ,
4749 m . CreatedUtc ,
4850 m . MessageId ,
@@ -54,11 +56,11 @@ public static object GetMessage(StreamMessage m)
5456 } ;
5557 }
5658
57- static object ToPageResponse ( StreamMessage m )
59+ static object ToPageResponse ( string path , StreamMessage m )
5860 {
5961 return new
6062 {
61- _links = Links . CreateItemLink ( m . Position ) ,
63+ _links = Links . CreateItemLink ( path , m . Position ) ,
6264 m . Position ,
6365 m . CreatedUtc ,
6466 m . MessageId ,
@@ -78,19 +80,51 @@ class Links
7880
7981 public static Links CreatePaginationLinks ( string path , long positionOfFirstEvent , long positionOfLastEvent , int numberOfEvents , int pageSize , int direction )
8082 {
83+ var self = new Uri ( path )
84+ . AddQuery ( "position" , positionOfFirstEvent )
85+ . AddQuery ( "direction" , direction == 1 ? "forwards" : "backwards" )
86+ . ToString ( ) ;
87+
88+ var next = new Uri ( path )
89+ . AddQuery ( "position" , positionOfLastEvent + direction )
90+ . AddQuery ( "direction" , direction == 1 ? "forwards" : "backwards" )
91+ . ToString ( ) ;
92+
8193 return new Links
8294 {
83- Self = new { Href = path + $ "?position= { positionOfFirstEvent } &direction= { ( direction == 1 ? "forwards" : "backwards" ) } " } ,
84- Next = numberOfEvents < pageSize ? null : new { Href = $ " { path } ?position= { positionOfLastEvent + direction } &direction= { ( direction == 1 ? "forwards" : "backwards" ) } " }
95+ Self = new { Href = self } ,
96+ Next = numberOfEvents < pageSize ? null : new { Href = next }
8597 } ;
8698 }
8799
88- public static Links CreateItemLink ( long position )
100+ public static Links CreateItemLink ( string path , long position )
89101 {
102+ var self = new Uri ( path )
103+ . AddQuery ( "position" , position )
104+ . ToString ( ) ;
105+
90106 return new Links
91107 {
92- Self = new { Href = "/stream/" + position } ,
108+ Self = new { Href = self }
93109 } ;
94110 }
95111 }
112+
113+ public static class HttpExtensions
114+ {
115+ public static Uri AddQuery ( this Uri uri , string name , object value )
116+ {
117+ var ub = new UriBuilder ( uri ) ;
118+
119+ // decodes urlencoded pairs from uri.Query to HttpValueCollection
120+ var httpValueCollection = HttpUtility . ParseQueryString ( uri . Query ) ;
121+
122+ httpValueCollection . Add ( name , value . ToString ( ) ) ;
123+
124+ // urlencodes the whole HttpValueCollection
125+ ub . Query = httpValueCollection . ToString ( ) ;
126+
127+ return ub . Uri ;
128+ }
129+ }
96130}
0 commit comments