Skip to content

Commit

Permalink
✨ Implement History in the manner of Search
Browse files Browse the repository at this point in the history
  • Loading branch information
andremacedopv committed Jul 7, 2023
1 parent f6e2d9e commit e9f2ad4
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions assets/history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package assets

import (
"encoding/json"
"net/http"

"github.com/goledgerdev/cc-tools/errors"
sw "github.com/goledgerdev/cc-tools/stubwrapper"
"github.com/hyperledger/fabric-chaincode-go/shim"
pb "github.com/hyperledger/fabric-protos-go/peer"
)

type HistoryResponse struct {
Result []map[string]interface{} `json:"result"`
Metadata *pb.QueryResponseMetadata `json:"metadata"`
}

func History(stub *sw.StubWrapper, key string, resolve bool) (*HistoryResponse, errors.ICCError) {
var resultsIterator shim.HistoryQueryIteratorInterface
// var responseMetadata *pb.QueryResponseMetadata

resultsIterator, err := stub.GetHistoryForKey(key)
if err != nil {
return nil, errors.WrapErrorWithStatus(err, "failed to get history for key", http.StatusInternalServerError)
}
defer resultsIterator.Close()

historyResult := make([]map[string]interface{}, 0)

for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return nil, errors.WrapErrorWithStatus(err, "error iterating response", 500)
}

var data map[string]interface{}

err = json.Unmarshal(queryResponse.Value, &data)
if err != nil {
return nil, errors.WrapErrorWithStatus(err, "failed to unmarshal queryResponse values", 500)
}

if resolve {
key, err := NewKey(data)
if err != nil {
return nil, errors.WrapError(err, "failed to create key object to resolve result")
}
asset, err := key.GetRecursive(stub)
if err != nil {
return nil, errors.WrapError(err, "failed to resolve result")
}
data = asset
}

historyResult = append(historyResult, data)
}

response := HistoryResponse{
Result: historyResult,
}

return &response, nil
}

0 comments on commit e9f2ad4

Please sign in to comment.