package chaincode import ( "encoding/json" "fmt" "github.com/hyperledger/fabric-contract-api-go/contractapi" ) type SmartContract struct { contractapi.Contract } type Application struct { Hash string `json:"Hash"` Name string `json:"Name"` SDK string `json:"SDK"` Size string `json:"Size"` Model string `json:"Model"` } func (s *SmartContract) Save(ctx contractapi.TransactionContextInterface, hash string, name string, sdk string, size string, model string) error { exists, err := s.Check(ctx, hash) if err != nil { return err } if exists { return fmt.Errorf("application %s is already registered!", hash) } application := Application{ Hash: hash, Name: name, SDK: sdk, Size: size, Model: model, } applicationJSON, err := json.Marshal(application) if err != nil { return err } return ctx.GetStub().PutState(hash, applicationJSON) } func (s *SmartContract) Query(ctx contractapi.TransactionContextInterface, hash string) (*Application, error) { applicationJSON, err := ctx.GetStub().GetState(hash) if err != nil { return nil, fmt.Errorf("failed to read from blockchain!: %v", err) } if applicationJSON == nil { return nil, fmt.Errorf("application %s not found", hash) } var application Application err = json.Unmarshal(applicationJSON, &application) if err != nil { return nil, err } return &application, nil } func (s *SmartContract) Update(ctx contractapi.TransactionContextInterface, hash string, name string, sdk string, size string, model string) error { exists, err := s.Check(ctx, hash) if err != nil { return err } if !exists { return fmt.Errorf("application %s not found", hash) } application := Application{ Hash: hash, Name: name, SDK: sdk, Size: size, Model: model, } applicationJSON, err := json.Marshal(application) if err != nil { return err } return ctx.GetStub().PutState(hash, applicationJSON) } func (s *SmartContract) Delete(ctx contractapi.TransactionContextInterface, hash string) error { exists, err := s.Check(ctx, hash) if err != nil { return err } if !exists { return fmt.Errorf("application %s not found", hash) } return ctx.GetStub().DelState(hash) } func (s *SmartContract) Check(ctx contractapi.TransactionContextInterface, hash string) (bool, error) { applicationJSON, err := ctx.GetStub().GetState(hash) if err != nil { return false, fmt.Errorf("failed to read from blockchain: %v", err) } return applicationJSON != nil, nil }