fabric-samples/commercial-paper/organization/digibank/contract-go/commercial-paper/paperlist.go
Andrew Hurt 5b93dd0cc0 [FAB-17140] Add go commercial paper contract (#102)
Signed-off-by: Andrew Hurt <andrew.hurt1@ibm.com>
2020-01-24 14:12:30 +00:00

55 lines
1.4 KiB
Go

/*
* SPDX-License-Identifier: Apache-2.0
*/
package commercialpaper
import ledgerapi "github.com/hyperledger/fabric-samples/commercial-paper/organization/digibank/contract-go/ledger-api"
// ListInterface defines functionality needed
// to interact with the world state on behalf
// of a commercial paper
type ListInterface interface {
AddPaper(*CommercialPaper) error
GetPaper(string, string) (*CommercialPaper, error)
UpdatePaper(*CommercialPaper) error
}
type list struct {
stateList ledgerapi.StateListInterface
}
func (cpl *list) AddPaper(paper *CommercialPaper) error {
return cpl.stateList.AddState(paper)
}
func (cpl *list) GetPaper(issuer string, paperNumber string) (*CommercialPaper, error) {
cp := new(CommercialPaper)
err := cpl.stateList.GetState(CreateCommercialPaperKey(issuer, paperNumber), cp)
if err != nil {
return nil, err
}
return cp, nil
}
func (cpl *list) UpdatePaper(paper *CommercialPaper) error {
return cpl.stateList.UpdateState(paper)
}
// NewList create a new list from context
func newList(ctx TransactionContextInterface) *list {
stateList := new(ledgerapi.StateList)
stateList.Ctx = ctx
stateList.Name = "org.papernet.commercialpaperlist"
stateList.Deserialize = func(bytes []byte, state ledgerapi.StateInterface) error {
return Deserialize(bytes, state.(*CommercialPaper))
}
list := new(list)
list.stateList = stateList
return list
}