github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/examples/creative/util/util.go (about)

     1  package util
     2  
     3  import (
     4  	. "github.com/inklabsfoundation/inkchain/examples/creative/conf"
     5  	. "github.com/inklabsfoundation/inkchain/examples/creative/model"
     6  	"github.com/inklabsfoundation/inkchain/core/chaincode/shim"
     7  	"bytes"
     8  	"fmt"
     9  	"strconv"
    10  	"encoding/json"
    11  	"strings"
    12  	"errors"
    13  )
    14  
    15  func GetUserKey(username string) string {
    16  	return GetStateKey(UserPrefix, username)
    17  }
    18  
    19  func GetArtistKey(username string) string {
    20  	return GetStateKey(ArtistPrefix, username)
    21  }
    22  
    23  func GetProductionKey(username, production_type, serial string) string {
    24  	return GetStateKey(ProductionPrefix, username, production_type, serial)
    25  }
    26  
    27  func GetStateKey(prefix string, args ...string) string {
    28  	for _, value := range args {
    29  		if value == "" {
    30  			break
    31  		} else {
    32  			prefix = prefix + value + StateSplitSymbol
    33  		}
    34  	}
    35  	return prefix
    36  }
    37  
    38  func Add(num_str_1, num_str_2 string) string {
    39  	num1, _ := strconv.Atoi(num_str_1)
    40  	num2, _ := strconv.Atoi(num_str_2)
    41  	return strconv.Itoa(num1 + num2)
    42  }
    43  
    44  func Mul(num_str_1, num_str_2 string) string {
    45  	num1, _ := strconv.Atoi(num_str_1)
    46  	num2, _ := strconv.Atoi(num_str_2)
    47  	return strconv.Itoa(num1 * num2)
    48  }
    49  
    50  func GetListResult(resultsIterator shim.StateQueryIteratorInterface) ([]byte, error) {
    51  	defer resultsIterator.Close()
    52  	// buffer is a JSON array containing QueryRecords
    53  	var buffer bytes.Buffer
    54  	buffer.WriteString("[")
    55  
    56  	bArrayMemberAlreadyWritten := false
    57  	for resultsIterator.HasNext() {
    58  		queryResponse, err := resultsIterator.Next()
    59  		if err != nil {
    60  			return nil, err
    61  		}
    62  		// Add a comma before array members, suppress it for the first array member
    63  		if bArrayMemberAlreadyWritten == true {
    64  			buffer.WriteString(",")
    65  		}
    66  		buffer.WriteString("{\"Key\":")
    67  		buffer.WriteString("\"")
    68  		buffer.WriteString(queryResponse.Key)
    69  		buffer.WriteString("\"")
    70  
    71  		buffer.WriteString(", \"Record\":")
    72  		// Record is a JSON object, so we write as-is
    73  		buffer.WriteString(string(queryResponse.Value))
    74  		buffer.WriteString("}")
    75  		bArrayMemberAlreadyWritten = true
    76  	}
    77  	buffer.WriteString("]")
    78  	fmt.Printf("queryResult:\n%s\n", buffer.String())
    79  	return buffer.Bytes(), nil
    80  }
    81  
    82  func GetListProduction(resultsIterator shim.StateQueryIteratorInterface) ([]Production, error) {
    83  	defer resultsIterator.Close()
    84  	var products []Production
    85  	for resultsIterator.HasNext() {
    86  		queryResponse, err := resultsIterator.Next()
    87  		if err != nil {
    88  			return nil, err
    89  		}
    90  		var pro Production
    91  		json.Unmarshal(queryResponse.Value, &pro)
    92  		products = append(products, pro)
    93  	}
    94  	return products, nil
    95  }
    96  
    97  func GetModifyProduction(productionJSON *Production, params []string) error {
    98  	fmt.Println("params:", params)
    99  	for _, param := range params {
   100  		index := strings.Index(param, ",")
   101  		if index == -1 {
   102  			return errors.New("Wrong parameter format:" + param)
   103  		}
   104  		name := param[:index]
   105  		value := param[index+1:]
   106  		switch name {
   107  		case "Name":
   108  			productionJSON.Name = value
   109  		case "Desc":
   110  			productionJSON.Desc = value
   111  		case "CopyrightPriceType":
   112  			productionJSON.CopyrightPriceType = value
   113  		case "CopyrightPrice":
   114  			productionJSON.CopyrightPrice = value
   115  		case "CopyrightNum":
   116  			productionJSON.CopyrightNum = value
   117  		case "CopyrightTransferPart":
   118  			productionJSON.CopyrightTransferPart = value
   119  		default:
   120  			return errors.New("This field_name doesn't exist:" + value)
   121  		}
   122  	}
   123  	return nil
   124  }
   125  
   126  func GetModifyArtist(artistJSON *Artist, params []string) error {
   127  	fmt.Println("params:", params)
   128  	for _, param := range params {
   129  		index := strings.Index(param, ",")
   130  		if index == -1 {
   131  			return errors.New("Wrong parameter format:" + param)
   132  		}
   133  		name := param[:index]
   134  		value := param[index+1:]
   135  		switch name {
   136  		case "Name":
   137  			artistJSON.Name = value
   138  		case "Desc":
   139  			artistJSON.Desc = value
   140  		default:
   141  			return errors.New("This field_name doesn't exist:" + value)
   142  		}
   143  	}
   144  	return nil
   145  }
   146  
   147  func GetModifyUser(userJSON *User, params []string) error {
   148  	fmt.Println("params:", params)
   149  	for _, param := range params {
   150  		if param != "" {
   151  			index := strings.Index(param, ",")
   152  			if index == -1 {
   153  				return errors.New("Wrong parameter format:" + param)
   154  			}
   155  			name := param[:index]
   156  			value := param[index+1:]
   157  			switch name {
   158  			case "Email":
   159  				userJSON.Email = value
   160  			default:
   161  				return errors.New("This field_name doesn't exist:" + value)
   162  			}
   163  		}
   164  	}
   165  	return nil
   166  }