forked from s7techlab/cckit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
96 lines (76 loc) · 2.61 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cpaper
import (
"fmt"
"github.com/pkg/errors"
"github.com/s7techlab/cckit/examples/cpaper/schema"
"github.com/s7techlab/cckit/router"
)
func cpaperList(c router.Context) (interface{}, error) {
// commercial paper key is composite key <`CommercialPaper`>, {Issuer}, {PaperNumber} >
// where `CommercialPaper` - namespace of this type
// list method retrieves entries from chaincode state
// using GetStateByPartialCompositeKey method, then unmarshal received from state bytes via proto.Ummarshal method
// and creates slice of *schema.CommercialPaper
return c.State().List(&schema.CommercialPaper{})
}
func cpaperIssue(c router.Context) (interface{}, error) {
var (
issue = c.Param().(*schema.IssueCommercialPaper) //default parameter
cpaper = &schema.CommercialPaper{
Issuer: issue.Issuer,
PaperNumber: issue.PaperNumber,
Owner: issue.Issuer,
IssueDate: issue.IssueDate,
MaturityDate: issue.MaturityDate,
FaceValue: issue.FaceValue,
State: schema.CommercialPaper_ISSUED, // initial state
}
err error
)
if err = c.Event().Set(issue); err != nil {
return nil, err
}
return cpaper, c.State().Insert(cpaper)
}
func cpaperBuy(c router.Context) (interface{}, error) {
var (
cpaper *schema.CommercialPaper
// but tx payload
buy = c.Param().(*schema.BuyCommercialPaper)
// current commercial paper state
cp, err = c.State().Get(
&schema.CommercialPaperId{Issuer: buy.Issuer, PaperNumber: buy.PaperNumber},
&schema.CommercialPaper{})
)
if err != nil {
return nil, errors.Wrap(err, `not found`)
}
cpaper = cp.(*schema.CommercialPaper)
// Validate current owner
if cpaper.Owner != buy.CurrentOwner {
return nil, fmt.Errorf(`paper %s %s is not owned by %s`, cpaper.Issuer, cpaper.PaperNumber, buy.CurrentOwner)
}
// First buy moves state from ISSUED to TRADING
if cpaper.State == schema.CommercialPaper_ISSUED {
cpaper.State = schema.CommercialPaper_TRADING
}
// Check paper is not already REDEEMED
if cpaper.State == schema.CommercialPaper_TRADING {
cpaper.Owner = buy.NewOwner
} else {
return nil, fmt.Errorf(`paper %s %s is not trading.current state = %s`, cpaper.Issuer, cpaper.PaperNumber, cpaper.State)
}
if err = c.Event().Set(buy); err != nil {
return nil, err
}
return cpaper, c.State().Put(cpaper)
}
func cpaperRedeem(c router.Context) (interface{}, error) {
return nil, nil
}
func cpaperGet(c router.Context) (interface{}, error) {
return c.State().Get(c.Param().(*schema.CommercialPaperId))
}
func cpaperDelete(c router.Context) (interface{}, error) {
return nil, c.State().Delete(c.Param().(*schema.CommercialPaperId))
}