github.com/s7techlab/cckit@v0.10.5/state/mapping/testdata/cc_slice_id.go (about)

     1  package testdata
     2  
     3  import (
     4  	"github.com/s7techlab/cckit/extensions/debug"
     5  	"github.com/s7techlab/cckit/extensions/owner"
     6  	"github.com/s7techlab/cckit/router"
     7  	"github.com/s7techlab/cckit/router/param"
     8  	"github.com/s7techlab/cckit/router/param/defparam"
     9  	"github.com/s7techlab/cckit/state"
    10  	m "github.com/s7techlab/cckit/state/mapping"
    11  	"github.com/s7techlab/cckit/state/mapping/testdata/schema"
    12  )
    13  
    14  func NewSliceIdCC() *router.Chaincode {
    15  	r := router.New(`complexId`)
    16  	debug.AddHandlers(r, `debug`, owner.Only)
    17  
    18  	// Mappings for chaincode state
    19  	r.Use(m.MapStates(m.StateMappings{}.
    20  		//key will be <`EntityWithSliceId`, {Id[0]}, {Id[1]},... {Id[len(Id)-1]} >
    21  		Add(&schema.EntityWithSliceId{}, m.PKeyId())))
    22  
    23  	r.Init(owner.InvokeSetFromCreator)
    24  
    25  	r.Group(`entity`).
    26  		Invoke(`List`, func(c router.Context) (interface{}, error) {
    27  			return c.State().List(&schema.EntityWithSliceId{})
    28  		}).
    29  		Invoke(`Get`, func(c router.Context) (interface{}, error) {
    30  			return c.State().Get(&schema.EntityWithSliceId{Id: state.StringsIdFromStr(c.ParamString(`Id`))})
    31  		}, param.String(`Id`)).
    32  		Invoke(`Insert`, func(c router.Context) (interface{}, error) {
    33  			return nil, c.State().Insert(c.Param())
    34  		}, defparam.Proto(&schema.EntityWithSliceId{}))
    35  
    36  	return router.NewChaincode(r)
    37  }