@@ -16,6 +16,7 @@ import Shim ( start
1616 )
1717
1818import Peer.ProposalResponse as Pb
19+ import Peer.ChaincodeShim as Pb
1920import Ledger.Queryresult.KvQueryResult as Pb
2021
2122import Data.Text ( Text
@@ -177,22 +178,23 @@ getMarblesByRange s params = if Prelude.length params == 2
177178 trace (show resultBytes) (pure $ successPayload Nothing )
178179 else pure $ errorPayload " Incorrect arguments. Need a start key and an end key"
179180
181+ -- TODO: include retrieval of next set of results using the returned bookmark (next TODO)
180182getMarblesByRangeWithPagination :: DefaultChaincodeStub -> [Text ] -> IO Pb. Response
181183getMarblesByRangeWithPagination s params = if Prelude. length params == 4
182184 then do
183185 e <- getStateByRangeWithPagination s (params !! 0 ) (params !! 1 ) (read (unpack $ params !! 2 ) :: Int ) (params !! 3 )
184186 case e of
185187 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 )
187191 else pure $ errorPayload " Incorrect arguments. Need start key, end key, pageSize and bookmark"
188192
189193generateResultBytes :: StateQueryIterator -> Text -> IO (Either Error BSU. ByteString )
190194generateResultBytes sqi text = do
191195 hasNextBool <- hasNext sqi
192- if hasNextBool then do
196+ if (trace $ " hasNext in generateResultBytes: " ++ show hasNextBool) hasNextBool then do
193197 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".
196198 case eeKV of
197199 Left e -> pure $ Left e
198200 Right kv ->
@@ -203,6 +205,21 @@ generateResultBytes sqi text = do
203205 generateResultBytes sqi (append text (makeKVString kv))
204206 else pure $ Right $ TSE. encodeUtf8 text
205207
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+
206223parseMarble :: [Text ] -> Marble
207224parseMarble params = Marble { objectType = " marble"
208225 , name = params !! 0
0 commit comments