github.com/s7techlab/cckit@v0.10.5/state/mapping/testdata/cc_composite_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/defparam"
     8  	"github.com/s7techlab/cckit/state"
     9  	"github.com/s7techlab/cckit/state/mapping"
    10  	"github.com/s7techlab/cckit/state/mapping/testdata/schema"
    11  )
    12  
    13  var (
    14  	EntityCompositeIdNamespace        = state.Key{`entity-composite-id`}
    15  	EntityWithCompositeIdStateMapping = mapping.StateMappings{}.Add(&schema.EntityWithCompositeId{},
    16  		// explicit set namespace for primary key, otherwise namespace will be based of schema type string representation
    17  		mapping.WithNamespace(EntityCompositeIdNamespace),
    18  		//  schema for Primary Key
    19  		mapping.PKeySchema(&schema.EntityCompositeId{}),
    20  		mapping.List(&schema.EntityWithCompositeIdList{}))
    21  )
    22  
    23  func NewCompositeIdCC() *router.Chaincode {
    24  	r := router.New("composite_id")
    25  
    26  	r.Use(mapping.MapStates(EntityWithCompositeIdStateMapping))
    27  
    28  	r.Use(mapping.MapEvents(mapping.EventMappings{}.
    29  		Add(&schema.CreateEntityWithCompositeId{}).
    30  		Add(&schema.UpdateEntityWithCompositeId{})))
    31  
    32  	r.Init(owner.InvokeSetFromCreator)
    33  	debug.AddHandlers(r, "debug", owner.Only)
    34  
    35  	r.
    36  		Query("list", queryListComposite).
    37  		Query("get", queryByIdComposite, defparam.Proto(&schema.EntityCompositeId{})).
    38  		Invoke("create", invokeCreateComposite, defparam.Proto(&schema.CreateEntityWithCompositeId{})).
    39  		Invoke("update", invokeUpdateComposite, defparam.Proto(&schema.UpdateEntityWithCompositeId{})).
    40  		Invoke("delete", invokeDeleteComposite, defparam.Proto(&schema.EntityCompositeId{}))
    41  
    42  	return router.NewChaincode(r)
    43  }
    44  
    45  func queryByIdComposite(c router.Context) (interface{}, error) {
    46  	return c.State().Get(c.Param().(*schema.EntityCompositeId))
    47  }
    48  
    49  func queryListComposite(c router.Context) (interface{}, error) {
    50  	return c.State().List(&schema.EntityWithCompositeId{})
    51  }
    52  
    53  func invokeCreateComposite(c router.Context) (interface{}, error) {
    54  	create := c.Param().(*schema.CreateEntityWithCompositeId)
    55  	entity := &schema.EntityWithCompositeId{
    56  		IdFirstPart:  create.IdFirstPart,
    57  		IdSecondPart: create.IdSecondPart,
    58  		IdThirdPart:  create.IdThirdPart,
    59  		Name:         create.Name,
    60  		Value:        create.Value,
    61  	}
    62  
    63  	if err := c.Event().Set(create); err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	return entity, c.State().Insert(entity)
    68  }
    69  
    70  func invokeUpdateComposite(c router.Context) (interface{}, error) {
    71  	update := c.Param().(*schema.UpdateEntityWithCompositeId)
    72  	entity, _ := c.State().Get(
    73  		&schema.EntityCompositeId{
    74  			IdFirstPart:  update.IdFirstPart,
    75  			IdSecondPart: update.IdSecondPart,
    76  			IdThirdPart:  update.IdThirdPart,
    77  		},
    78  		&schema.EntityWithCompositeId{})
    79  
    80  	e := entity.(*schema.EntityWithCompositeId)
    81  
    82  	e.Name = update.Name
    83  	e.Value = update.Value
    84  
    85  	if err := c.Event().Set(update); err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	return e, c.State().Put(e)
    90  }
    91  
    92  func invokeDeleteComposite(c router.Context) (interface{}, error) {
    93  	return nil, c.State().Delete(c.Param().(*schema.EntityCompositeId))
    94  }