// ============================================================
// createAuthDataItem - create a new auth data item, store into chaincode state
// ============================================================
create a new auth data item, store into chaincode state
// ============================================================ // createAuthDataItem - create a new auth data item store into chaincode state // ============================================================
func (t *AuthChaincode) createAuthDataItem(stub shim.ChaincodeStubInterface, args []string) pb.Response {
        var err error
        if len(args) != 5 {
                return shim.Error("Incorrect number of arguments. Expecting 4")
        }
        fmt.Println("- start create auth data item")
        if len(args[0]) <= 0 {
                return shim.Error("1st argument must be a non-empty string")
        }
        if len(args[1]) <= 0 {
                return shim.Error("2nd argument must be a non-empty string")
        }
        if len(args[2]) <= 0 {
                return shim.Error("3rd argument must be a non-empty string")
        }
        if len(args[3]) <= 0 {
                return shim.Error("4th argument must be a non-empty string")
        }
        if len(args[4]) <= 0 {
                return shim.Error("4th argument must be a non-empty string")
        }
        fingerId := args[0]
        authId := args[1]
        sourceDeptId := args[2]
        sourceDept := args[3]
        granteeId := args[4]
        granteeName := args[5]
        sceneId := args[6]
        sceneName := args[7]
        modelId := args[8]
        modelName := args[9]
        objectId := args[10]
        objectName := args[11]
        fieldId := args[12]
        fieldName := args[13]
        useRule := args[14]
        authDataItemAsBytes, err := stub.GetState(fingerId)
        if err != nil {
                return shim.Error("Failed to get auth data item: " + err.Error())
        } else if authDataItemAsBytes != nil {
                fmt.Println("This auth data item already exists: " + fingerId)
                return shim.Error("This auth data item already exists: " + fingerId)
        }
        authDataItem := &AuthDataItem{fingerId, authId, sourceDeptId, sourceDept, granteeId, granteeName, sceneId, sceneName, modelId, modelName, objectId, objectName, fieldId, fieldName, useRule}
        authDataItemJSONasBytes, err := json.Marshal(authDataItem)
        if err != nil {
                return shim.Error(err.Error())
        }
        err = stub.PutState(authId, authDataItemJSONasBytes)
        if err != nil {
                return shim.Error(err.Error())
        }
        indexName := "authId~sourceDeptId~granteeId~sceneId~modelId~objectId~fieldId"
        indexKey, err := stub.CreateCompositeKey(indexName, []string{authDataItem.AuthId, authDataItem.SourceDeptId, authDataItem.GranteeId, authDataItem.SceneId, authDataItem.ModelId, authDataItem.ObjectId, authDataItem.FieldId})
        if err != nil {
                return shim.Error(err.Error())
        }
        value := []byte{0x00}
        stub.PutState(indexKey, value)
        fmt.Println("- end create auth data item")
        return shim.Success(nil)
}
func ( t * AuthChaincode ) createAuthDataItem ( stub shim . ChaincodeStubInterface , args [ ] string ) pb . Response { var err error if len ( args ) != 5 { return shim . Error ( " " ) } fmt . Println ( " " ) if len ( args [ 0 ] ) <= 0 { return shim . Error ( " " ) } if len ( args [ 1 ] ) <= 0 { return shim . Error ( " " ) } if len ( args [ 2 ] ) <= 0 { return shim . Error ( " " ) } if len ( args [ 3 ] ) <= 0 { return shim . Error ( " " ) } if len ( args [ 4 ] ) <= 0 { return shim . Error ( " " ) } fingerId := args [ 0 ] authId := args [ 1 ] sourceDeptId := args [ 2 ] sourceDept := args [ 3 ] granteeId := args [ 4 ] granteeName := args [ 5 ] sceneId := args [ 6 ] sceneName := args [ 7 ] modelId := args [ 8 ] modelName := args [ 9 ] objectId := args [ 10 ] objectName := args [ 11 ] fieldId := args [ 12 ] fieldName := args [ 13 ] useRule := args [ 14 ] authDataItemAsBytes , err := stub . GetState ( fingerId ) if err != nil { return shim . Error ( " " + err . Error ( ) ) } else if authDataItemAsBytes != nil { fmt . Println ( " " + fingerId ) return shim . Error ( " " + fingerId ) } authDataItem := & AuthDataItem { fingerId , authId , sourceDeptId , sourceDept , granteeId , granteeName , sceneId , sceneName , modelId , modelName , objectId , objectName , fieldId , fieldName , useRule } authDataItemJSONasBytes , err := json . Marshal ( authDataItem ) if err != nil { return shim . Error ( err . Error ( ) ) } err = stub . PutState ( authId , authDataItemJSONasBytes ) if err != nil { return shim . Error ( err . Error ( ) ) } indexName := " " indexKey , err := stub . CreateCompositeKey ( indexName , [ ] string { authDataItem . AuthId , authDataItem . SourceDeptId , authDataItem . GranteeId , authDataItem . SceneId , authDataItem . ModelId , authDataItem . ObjectId , authDataItem . FieldId } ) if err != nil { return shim . Error ( err . Error ( ) ) } value := [ ] byte { 0x00 } stub . PutState ( indexKey , value ) fmt . Println ( " " ) return shim . Success ( nil ) }
create a new auth data item, store into chaincode state
create a new auth data item store into chaincode state
// ==== Input sanitation ==== // ==== Check if auth data item already exists ==== // ==== Create auth data item object and marshal to JSON ==== // === Save auth scene to state === //  ==== Index the marble to enable color-based range queries, e.g. return all blue marbles ==== //  An 'index' is a normal key/value entry in state. //  The key is a composite key, with the elements that you want to range query on listed first. //  In our case, the composite key is based on indexName~color~name. //  This will enable very efficient state range queries based on composite keys matching indexName~color~* //  Save index entry to state. Only the key name is needed, no need to store a duplicate copy of the marble. //  Note - passing a 'nil' value will effectively delete the key from state, therefore we pass null character as value // ==== Auth info saved and indexed. Return success ====