github.com/s7techlab/cckit@v0.10.5/examples/insurance/app/main.go (about)

     1  package app
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/hyperledger/fabric-chaincode-go/shim"
     7  	pb "github.com/hyperledger/fabric-protos-go/peer"
     8  )
     9  
    10  const prefixContractType = "contract_type"
    11  const prefixContract = "contract"
    12  const prefixClaim = "claim"
    13  const prefixUser = "user"
    14  const prefixRepairOrder = "repair_order"
    15  
    16  type SmartContract struct {
    17  }
    18  
    19  var bcFunctions = map[string]func(shim.ChaincodeStubInterface, []string) pb.Response{
    20  	// Insurance Peer
    21  	"contract_type_ls":         listContractTypes,
    22  	"contract_type_create":     createContractType,
    23  	"contract_type_set_active": setActiveContractType,
    24  	"contract_ls":              listContracts,
    25  	"claim_ls":                 listClaims,
    26  	"claim_file":               fileClaim,
    27  	"claim_process":            processClaim,
    28  	"user_authenticate":        authUser,
    29  	"user_get_info":            getUser,
    30  	// Shop Peer
    31  	"contract_create": createContract,
    32  	"user_create":     createUser,
    33  	// Repair Shop Peer
    34  	"repair_order_ls":       listRepairOrders,
    35  	"repair_order_complete": completeRepairOrder,
    36  	// Police Peer
    37  	"theft_claim_ls":      listTheftClaims,
    38  	"theft_claim_process": processTheftClaim,
    39  }
    40  
    41  // Init callback representing the invocation of a chaincode
    42  func (t *SmartContract) Init(stub shim.ChaincodeStubInterface) pb.Response {
    43  	_, args := stub.GetFunctionAndParameters()
    44  
    45  	if len(args) == 1 {
    46  		var contractTypes []struct {
    47  			UUID string `json:"uuid"`
    48  			*ContractType
    49  		}
    50  		err := json.Unmarshal([]byte(args[0]), &contractTypes)
    51  		if err != nil {
    52  			return shim.Error(err.Error())
    53  		}
    54  		for _, ct := range contractTypes {
    55  			contractTypeKey, err := stub.CreateCompositeKey(prefixContractType, []string{ct.UUID})
    56  			if err != nil {
    57  				return shim.Error(err.Error())
    58  			}
    59  			contractTypeAsBytes, err := json.Marshal(ct.ContractType)
    60  			if err != nil {
    61  				return shim.Error(err.Error())
    62  			}
    63  			err = stub.PutState(contractTypeKey, contractTypeAsBytes)
    64  			if err != nil {
    65  				return shim.Error(err.Error())
    66  			}
    67  		}
    68  	}
    69  	return shim.Success(nil)
    70  }
    71  
    72  // Invoke Function accept blockchain code invocations.
    73  func (t *SmartContract) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    74  	function, args := stub.GetFunctionAndParameters()
    75  
    76  	if function == "init" {
    77  		return t.Init(stub)
    78  	}
    79  	bcFunc := bcFunctions[function]
    80  	if bcFunc == nil {
    81  		return shim.Error("Invalid invoke function.")
    82  	}
    83  	return bcFunc(stub, args)
    84  }