-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Implement History in the manner of Search
- Loading branch information
1 parent
f6e2d9e
commit e9f2ad4
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |