Created
May 20, 2018 19:52
-
-
Save vitiko/362ce12e3c2c76a2e0dd49f926b03ccc to your computer and use it in GitHub Desktop.
Cars chaincode methods implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Key for car entry in chaincode state | |
func Key(id string) []string { | |
return []string{CarKeyPrefix, id} | |
} | |
// car get info chaincode method handler | |
func car(c router.Context) (interface{}, error) { | |
return c.State().Get( // get state entry | |
Key(c.ArgString(`id`)), // by composite key using CarKeyPrefix and car.Id | |
&Car{}) // and unmarshal from []byte to Car struct | |
} | |
// cars car list chaincode method handler | |
func cars(c router.Context) (interface{}, error) { | |
return c.State().List( | |
CarKeyPrefix, // get list of state entries of type CarKeyPrefix | |
&Car{}) // unmarshal from []byte and append to []Car slice | |
} | |
// carRegister car register chaincode method handler | |
func carRegister(c router.Context) (interface{}, error) { | |
// arg name defined in router method definition | |
p := c.Arg(`car`).(CarPayload) | |
t, _ := c.Time() // tx time | |
car := &Car{ // data for chaincode state | |
Id: p.Id, | |
Title: p.Title, | |
Owner: p.Owner, | |
UpdatedAt: t, | |
} | |
return car, // peer.Response payload will be json serialized car data | |
c.State().Insert( //put json serialized data to state | |
Key(car.Id), // create composite key using CarKeyPrefix and car.Id | |
car) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment