@@ -16,6 +16,7 @@ import Shim ( start
16
16
)
17
17
18
18
import Peer.ProposalResponse as Pb
19
+ import Peer.ChaincodeShim as Pb
19
20
import Ledger.Queryresult.KvQueryResult as Pb
20
21
21
22
import Data.Text ( Text
@@ -177,22 +178,23 @@ getMarblesByRange s params = if Prelude.length params == 2
177
178
trace (show resultBytes) (pure $ successPayload Nothing )
178
179
else pure $ errorPayload " Incorrect arguments. Need a start key and an end key"
179
180
181
+ -- TODO: include retrieval of next set of results using the returned bookmark (next TODO)
180
182
getMarblesByRangeWithPagination :: DefaultChaincodeStub -> [Text ] -> IO Pb. Response
181
183
getMarblesByRangeWithPagination s params = if Prelude. length params == 4
182
184
then do
183
185
e <- getStateByRangeWithPagination s (params !! 0 ) (params !! 1 ) (read (unpack $ params !! 2 ) :: Int ) (params !! 3 )
184
186
case e of
185
187
Left _ -> pure $ errorPayload " Failed to get marbles"
186
- Right _ -> pure $ successPayload $ Just " The payload"
188
+ Right (sqi, metadata) -> do
189
+ resultBytes <- generateResultBytesForPagination (sqi, metadata) " "
190
+ trace (show resultBytes) (pure $ successPayload Nothing )
187
191
else pure $ errorPayload " Incorrect arguments. Need start key, end key, pageSize and bookmark"
188
192
189
193
generateResultBytes :: StateQueryIterator -> Text -> IO (Either Error BSU. ByteString )
190
194
generateResultBytes sqi text = do
191
195
hasNextBool <- hasNext sqi
192
- if hasNextBool then do
196
+ if (trace $ " hasNext in generateResultBytes: " ++ show hasNextBool) hasNextBool then do
193
197
eeKV <- next sqi
194
- -- TODO: We need to check that the Either Error KV returned from next
195
- -- is correct and append the showable version of KVs instead of "abc".
196
198
case eeKV of
197
199
Left e -> pure $ Left e
198
200
Right kv ->
@@ -203,6 +205,21 @@ generateResultBytes sqi text = do
203
205
generateResultBytes sqi (append text (makeKVString kv))
204
206
else pure $ Right $ TSE. encodeUtf8 text
205
207
208
+ generateResultBytesForPagination :: (StateQueryIterator , Pb. QueryResponseMetadata ) -> Text -> IO (Either Error BSU. ByteString )
209
+ generateResultBytesForPagination (sqi, md) text = do
210
+ hasNextBool <- hasNext sqi
211
+ if (trace $ " hasNext in generateResultBytesForPagination: " ++ show hasNextBool) hasNextBool then do
212
+ eeKV <- next sqi
213
+ case eeKV of
214
+ Left e -> pure $ Left e
215
+ Right kv ->
216
+ let
217
+ makeKVString :: Pb. KV -> Text
218
+ makeKVString kv_ = pack " Key: " <> TL. toStrict (Pb. kvKey kv_) <> pack " , Value: " <> TSE. decodeUtf8 (kvValue kv_)
219
+ in
220
+ generateResultBytesForPagination (sqi, md) (append text (makeKVString kv))
221
+ else pure $ Right $ TSE. encodeUtf8 text
222
+
206
223
parseMarble :: [Text ] -> Marble
207
224
parseMarble params = Marble { objectType = " marble"
208
225
, name = params !! 0
0 commit comments