github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/examples/creative/invoke/user.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  	"fmt"
    10  	"encoding/json"
    11  	"strings"
    12  )
    13  
    14  type UserInvoke struct{}
    15  
    16  func (*UserInvoke) AddUser(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    17  	fmt.Println("add user start.")
    18  	username := args[0]
    19  	email := args[1]
    20  	//email = "mm@mm.com"
    21  	// The user parameters will be extended. TODO
    22  	user_key := GetUserKey(username)
    23  	// get user's address
    24  	address, err := stub.GetSender()
    25  	if err != nil {
    26  		return shim.Error("Fail to reveal user's address.")
    27  	}
    28  	address = strings.ToLower(address)
    29  	// check if user exists
    30  	userAsBytes, err := stub.GetState(user_key)
    31  	if err != nil {
    32  		return shim.Error("Fail to get user: " + err.Error())
    33  	} else if userAsBytes != nil {
    34  		fmt.Println("This user already exists: " + username)
    35  		return shim.Error("This user already exists: " + username)
    36  	}
    37  	// add user
    38  	user := &User{Username: username, Email: email, Address: address}
    39  	userJSONasBytes, err := json.Marshal(user)
    40  	if err != nil {
    41  		return shim.Error(err.Error())
    42  	}
    43  	err = stub.PutState(user_key, userJSONasBytes)
    44  	if err != nil {
    45  		return shim.Error(err.Error())
    46  	}
    47  	return shim.Success([]byte("add user success."))
    48  }
    49  
    50  func (*UserInvoke) DeleteUser(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    51  	fmt.Println("delete user start.")
    52  	username := args[0]
    53  	user_key := GetUserKey(username)
    54  	// step 1: get the user info
    55  	userAsBytes, err := stub.GetState(user_key)
    56  	if err != nil {
    57  		return shim.Error("Fail to get user: " + err.Error())
    58  	}
    59  	if userAsBytes == nil {
    60  		fmt.Println("This user doesn't exist: " + username)
    61  		return shim.Error("This user doesn't exist: " + username)
    62  	}
    63  	var userJSON User
    64  	err = json.Unmarshal([]byte(userAsBytes), &userJSON)
    65  	if err != nil {
    66  		jsonResp := "{\"Error\":\"Failed to decode JSON of: " + username + "\"}"
    67  		return shim.Error(jsonResp)
    68  	}
    69  	// step 2: get user's address
    70  	address, err := stub.GetSender()
    71  	if err != nil {
    72  		return shim.Error("Fail to reveal user's address.")
    73  	}
    74  	address = strings.ToLower(address)
    75  	if userJSON.Address != address {
    76  		return shim.Error("The sender's address doesn't correspond with the user's.")
    77  	}
    78  	// step 3: delete user's info
    79  	err = stub.DelState(user_key)
    80  	if err != nil {
    81  		fmt.Println("Fail to delete: " + user_key)
    82  		return shim.Error("Fail to delete" + user_key)
    83  	}
    84  	return shim.Success([]byte("delete user success."))
    85  }
    86  
    87  func (*UserInvoke) ModifyUser(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    88  	fmt.Println("modify user start.")
    89  	username := args[0]
    90  	user_key := GetUserKey(username)
    91  	userAsBytes, err := stub.GetState(user_key)
    92  	if err != nil {
    93  		return shim.Error("Fail to get user: " + err.Error())
    94  	}
    95  	if userAsBytes == nil {
    96  		fmt.Println("This user doesn't exist: " + username)
    97  		return shim.Error("This user doesn't exist: " + username)
    98  	}
    99  	var userJSON User
   100  	err = json.Unmarshal([]byte(userAsBytes), &userJSON)
   101  	if err != nil {
   102  		return shim.Error(err.Error())
   103  	}
   104  	err = GetModifyUser(&userJSON, args[1:])
   105  	if err != nil {
   106  		return shim.Error(err.Error())
   107  	}
   108  	userJSONasBytes, err := json.Marshal(userJSON)
   109  	if err != nil {
   110  		return shim.Error(err.Error())
   111  	}
   112  	err = stub.PutState(user_key, userJSONasBytes)
   113  	if err != nil {
   114  		return shim.Error(err.Error())
   115  	}
   116  	return shim.Success(userJSONasBytes)
   117  }
   118  
   119  func (*UserInvoke) QueryUser(stub shim.ChaincodeStubInterface, args []string) pb.Response {
   120  	fmt.Println("query user start.")
   121  	username := args[0]
   122  	user_key := GetUserKey(username)
   123  	userAsBytes, err := stub.GetState(user_key)
   124  	if err != nil {
   125  		return shim.Error("Fail to get user: " + err.Error())
   126  	}
   127  	if userAsBytes == nil {
   128  		fmt.Println("This user doesn't exist: " + username)
   129  		return shim.Error("This user doesn't exist: " + username)
   130  	}
   131  	return shim.Success(userAsBytes)
   132  }
   133  
   134  func (*UserInvoke) ListOfUser(stub shim.ChaincodeStubInterface, args []string) pb.Response {
   135  	fmt.Println("list of user start.")
   136  	resultsIterator, err := stub.GetStateByRange(UserPrefix+StateStartSymbol, UserPrefix+StateEndSymbol)
   137  	if err != nil {
   138  		return shim.Error(err.Error())
   139  	}
   140  	list, err := GetListResult(resultsIterator)
   141  	if err != nil {
   142  		return shim.Error("getListResult failed")
   143  	}
   144  	return shim.Success(list)
   145  }