github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/examples/creative/invoke/artist.go (about) 1 package invoke 2 3 import ( 4 . "github.com/inklabsfoundation/inkchain/examples/creative/conf" 5 . "github.com/inklabsfoundation/inkchain/examples/creative/util" 6 . "github.com/inklabsfoundation/inkchain/examples/creative/model" 7 "github.com/inklabsfoundation/inkchain/core/chaincode/shim" 8 pb "github.com/inklabsfoundation/inkchain/protos/peer" 9 "encoding/json" 10 "fmt" 11 "strings" 12 ) 13 14 type ArtistInvoke struct{} 15 16 func (*ArtistInvoke) AddArtist(stub shim.ChaincodeStubInterface, args []string) pb.Response { 17 fmt.Println("add artist start.") 18 username := args[0] 19 artist_name := args[1] 20 artist_desc := args[2] 21 // verify weather the user exists 22 user_key := GetUserKey(username) 23 userAsBytes, err := stub.GetState(user_key) 24 if err != nil { 25 return shim.Error("Fail to get user: " + err.Error()) 26 } 27 if userAsBytes == nil { 28 fmt.Println("This user doesn't exist: " + username) 29 return shim.Error("This user doesn't exist: " + username) 30 } 31 // verify weather the artist exists 32 artist_key := GetArtistKey(username) 33 artistAsBytes, err := stub.GetState(artist_key) 34 if err != nil { 35 return shim.Error("Fail to get artist: " + err.Error()) 36 } 37 if artistAsBytes != nil { 38 fmt.Println("This artist already exist: " + artist_name) 39 return shim.Error("This artist already exist: " + artist_name) 40 } 41 // add artist 42 artist := Artist{artist_name, artist_desc, username} 43 artistAsBtyes, err := json.Marshal(artist) 44 err = stub.PutState(artist_key, artistAsBtyes) 45 if err != nil { 46 return shim.Error(err.Error()) 47 } 48 return shim.Success([]byte("add artist success.")) 49 } 50 51 func (c *ArtistInvoke) ModifyArtist(stub shim.ChaincodeStubInterface, args []string) pb.Response { 52 fmt.Println("modify artist start.") 53 username := args[0] 54 user_key := GetUserKey(username) 55 // verify weather the user exists 56 userAsBytes, err := stub.GetState(user_key) 57 if err != nil { 58 return shim.Error("Fail to get user: " + err.Error()) 59 } 60 if userAsBytes == nil { 61 fmt.Println("This user doesn't exist: " + username) 62 return shim.Error("This user doesn't exist: " + username) 63 } 64 // get user's address 65 address, err := stub.GetSender() 66 if err != nil { 67 return shim.Error("Fail to reveal user's address.") 68 } 69 address = strings.ToLower(address) 70 var userJSON User 71 err = json.Unmarshal([]byte(userAsBytes), &userJSON) 72 if userJSON.Address != address { 73 return shim.Error("The sender's address doesn't correspond with the user's.") 74 } 75 artist_key := GetArtistKey(username) 76 // verify weather the artist exists 77 artistAsBytes, err := stub.GetState(artist_key) 78 if err != nil { 79 return shim.Error("Fail to get artist: " + err.Error()) 80 } 81 if artistAsBytes == nil { 82 fmt.Println("This artist doesn't exist: " + artist_key) 83 return shim.Error("This artist doesn't exist: " + artist_key) 84 } 85 var artistJSON Artist 86 err = json.Unmarshal([]byte(artistAsBytes), &artistJSON) 87 if err != nil { 88 return shim.Error(err.Error()) 89 } 90 if artistJSON.Username != userJSON.Username { 91 return shim.Error("The artist's username doesn't correspond with the user's.") 92 } 93 err = GetModifyArtist(&artistJSON, args[1:]) 94 if err != nil { 95 return shim.Error(err.Error()) 96 } 97 artistJSONasBytes, err := json.Marshal(artistJSON) 98 if err != nil { 99 return shim.Error(err.Error()) 100 } 101 err = stub.PutState(artist_key, artistJSONasBytes) 102 if err != nil { 103 return shim.Error(err.Error()) 104 } 105 return shim.Success([]byte("modify artist success.")) 106 } 107 108 func (c *ArtistInvoke) DeleteArtist(stub shim.ChaincodeStubInterface, args []string) pb.Response { 109 fmt.Println("delete artist start.") 110 username := args[0] 111 user_key := GetUserKey(username) 112 // verify weather the user exists 113 userAsBytes, err := stub.GetState(user_key) 114 if err != nil { 115 return shim.Error("Fail to get user: " + err.Error()) 116 } 117 if userAsBytes == nil { 118 fmt.Println("This user doesn't exist: " + username) 119 return shim.Error("This user doesn't exist: " + username) 120 } 121 // get user's address 122 address, err := stub.GetSender() 123 if err != nil { 124 return shim.Error("Fail to reveal user's address.") 125 } 126 address = strings.ToLower(address) 127 var userJSON User 128 err = json.Unmarshal([]byte(userAsBytes), &userJSON) 129 if userJSON.Address != address { 130 return shim.Error("The sender's address doesn't correspond with the user's.") 131 } 132 artist_key := GetArtistKey(username) 133 // verify weather the artist exists 134 artistAsBytes, err := stub.GetState(artist_key) 135 if err != nil { 136 return shim.Error("Fail to get artist: " + err.Error()) 137 } 138 if artistAsBytes == nil { 139 fmt.Println("This artist doesn't exist: " + artist_key) 140 return shim.Error("This artist doesn't exist: " + artist_key) 141 } 142 var artistJSON Artist 143 err = json.Unmarshal([]byte(artistAsBytes), &artistJSON) 144 if artistJSON.Username != userJSON.Username { 145 return shim.Error("The artist's username doesn't correspond with the user's.") 146 } 147 // delete artist's info 148 err = stub.DelState(artist_key) 149 if err != nil { 150 fmt.Println("Fail to delete: " + artist_key) 151 return shim.Error("Fail to delete" + artist_key) 152 } 153 return shim.Success([]byte("delete artist success.")) 154 } 155 156 func (c *ArtistInvoke) QueryArtist(stub shim.ChaincodeStubInterface, args []string) pb.Response { 157 fmt.Println("query artist start.") 158 username := args[0] 159 artist_key := GetArtistKey(username) 160 artistAsBytes, err := stub.GetState(artist_key) 161 if err != nil { 162 return shim.Error("Fail to get artist: " + err.Error()) 163 } 164 if artistAsBytes == nil { 165 fmt.Println("This artist doesn't exist: " + username) 166 return shim.Error("This artist doesn't exist: " + username) 167 } 168 return shim.Success(artistAsBytes) 169 } 170 171 func (c *ArtistInvoke) ListOfArtist(stub shim.ChaincodeStubInterface, args []string) pb.Response { 172 fmt.Println("list of artist start.") 173 resultsIterator, err := stub.GetStateByRange(ArtistPrefix+StateStartSymbol, ArtistPrefix+StateEndSymbol) 174 if err != nil { 175 return shim.Error(err.Error()) 176 } 177 list, err := GetListResult(resultsIterator) 178 if err != nil { 179 return shim.Error("getListResult failed") 180 } 181 return shim.Success(list) 182 }