github.com/s7techlab/cckit@v0.10.5/examples/insurance/app/invoke_shop.go (about) 1 package app 2 3 import ( 4 "encoding/json" 5 6 "time" 7 8 "github.com/hyperledger/fabric-chaincode-go/shim" 9 pb "github.com/hyperledger/fabric-protos-go/peer" 10 ) 11 12 func createContract(stub shim.ChaincodeStubInterface, args []string) pb.Response { 13 if len(args) != 1 { 14 return shim.Error("Invalid argument count.") 15 } 16 17 dto := struct { 18 UUID string `json:"uuid"` 19 ContractTypeUUID string `json:"contract_type_uuid"` 20 Username string `json:"username"` 21 Password string `json:"password"` 22 FirstName string `json:"first_name"` 23 LastName string `json:"last_name"` 24 Item Item `json:"item"` 25 StartDate time.Time `json:"start_date"` 26 EndDate time.Time `json:"end_date"` 27 }{} 28 29 err := json.Unmarshal([]byte(args[0]), &dto) 30 if err != nil { 31 return shim.Error(err.Error()) 32 } 33 34 // Create new user if necessary 35 var u User 36 requestUserCreate := len(dto.Username) > 0 && len(dto.Password) > 0 37 userKey, err := stub.CreateCompositeKey(prefixUser, []string{dto.Username}) 38 if requestUserCreate { 39 // Check if a user with the same username exists 40 if err != nil { 41 return shim.Error(err.Error()) 42 } 43 userAsBytes, _ := stub.GetState(userKey) 44 if userAsBytes == nil { 45 // Create new user 46 u = User{ 47 Username: dto.Username, 48 Password: dto.Password, 49 FirstName: dto.FirstName, 50 LastName: dto.LastName, 51 } 52 // Persist the new user 53 userAsBytes, err := json.Marshal(u) 54 if err != nil { 55 return shim.Error(err.Error()) 56 } 57 err = stub.PutState(userKey, userAsBytes) 58 if err != nil { 59 return shim.Error(err.Error()) 60 } 61 } else { 62 // Parse the existing user 63 err := json.Unmarshal(userAsBytes, &u) 64 if err != nil { 65 return shim.Error(err.Error()) 66 } 67 } 68 } else { 69 // Validate if the user with the provided username exists 70 userAsBytes, _ := stub.GetState(userKey) 71 if userAsBytes == nil { 72 return shim.Error("User with this username does not exist.") 73 } 74 } 75 76 contract := Contract{ 77 Username: dto.Username, 78 ContractTypeUUID: dto.ContractTypeUUID, 79 Item: dto.Item, 80 StartDate: dto.StartDate, 81 EndDate: dto.EndDate, 82 Void: false, 83 ClaimIndex: []string{}, 84 } 85 86 contractKey, err := stub.CreateCompositeKey(prefixContract, []string{dto.Username, dto.UUID}) 87 if err != nil { 88 return shim.Error(err.Error()) 89 } 90 contractAsBytes, err := json.Marshal(contract) 91 if err != nil { 92 return shim.Error(err.Error()) 93 } 94 err = stub.PutState(contractKey, contractAsBytes) 95 if err != nil { 96 return shim.Error(err.Error()) 97 } 98 99 // Return success, if the new user has been created 100 // (the user variable "u" should be blank) 101 if !requestUserCreate { 102 return shim.Success(nil) 103 } 104 105 response := struct { 106 Username string `json:"username"` 107 Password string `json:"password"` 108 }{ 109 Username: u.Username, 110 Password: u.Password, 111 } 112 responseAsBytes, err := json.Marshal(response) 113 if err != nil { 114 return shim.Error(err.Error()) 115 } 116 return shim.Success(responseAsBytes) 117 } 118 119 func createUser(stub shim.ChaincodeStubInterface, args []string) pb.Response { 120 if len(args) != 1 { 121 return shim.Error("Invalid argument count.") 122 } 123 124 user := User{} 125 err := json.Unmarshal([]byte(args[0]), &user) 126 if err != nil { 127 return shim.Error(err.Error()) 128 } 129 130 key, err := stub.CreateCompositeKey(prefixUser, []string{user.Username}) 131 if err != nil { 132 return shim.Error(err.Error()) 133 } 134 135 // Check if the user already exists 136 userAsBytes, _ := stub.GetState(key) 137 // User does not exist, attempting creation 138 if len(userAsBytes) == 0 { 139 userAsBytes, err = json.Marshal(user) 140 if err != nil { 141 return shim.Error(err.Error()) 142 } 143 144 err = stub.PutState(key, userAsBytes) 145 if err != nil { 146 return shim.Error(err.Error()) 147 } 148 149 // Return nil, if user is newly created 150 return shim.Success(nil) 151 } 152 153 err = json.Unmarshal(userAsBytes, &user) 154 if err != nil { 155 return shim.Error(err.Error()) 156 } 157 158 userResponse := struct { 159 Username string `json:"username"` 160 Password string `json:"password"` 161 }{ 162 Username: user.Username, 163 Password: user.Password, 164 } 165 166 userResponseAsBytes, err := json.Marshal(userResponse) 167 if err != nil { 168 return shim.Error(err.Error()) 169 } 170 // Return the username and the password of the already existing user 171 return shim.Success(userResponseAsBytes) 172 }