-
Notifications
You must be signed in to change notification settings - Fork 17
/
user.go
151 lines (131 loc) · 3.56 KB
/
user.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package contract
import (
"encoding/json"
"fmt"
"github.com/hyperledger/fabric-contract-api-go/contractapi"
"github.com/wefantasy/FabricLearn/7_IntermediateContract/project_contract/model"
)
type UserContract struct {
contractapi.Contract
}
// 写入新用户
func (s *UserContract) CreateUser(ctx contractapi.TransactionContextInterface, username string, name string, email string, phone string) error {
exists, err := s.UserExists(ctx, username)
if err != nil {
return err
}
if exists {
return fmt.Errorf("the user %s already exists", username)
}
user := model.User{
Username: username,
Name: name,
Email: email,
Phone: phone,
}
userJSON, err := json.Marshal(user)
if err != nil {
return err
}
return ctx.GetStub().PutState(username, userJSON)
}
// 读取指定ID的用户信息
func (s *UserContract) ReadUser(ctx contractapi.TransactionContextInterface, username string) (*model.User, error) {
userJSON, err := ctx.GetStub().GetState(username)
if err != nil {
return nil, fmt.Errorf("failed to read from world state: %v", err)
}
if userJSON == nil {
return nil, fmt.Errorf("the user %s does not exist", username)
}
var user model.User
err = json.Unmarshal(userJSON, &user)
if err != nil {
return nil, err
}
return &user, nil
}
// 更新用户信息.
func (s *UserContract) UpdateUser(ctx contractapi.TransactionContextInterface, username string, name string, email string, phone string) error {
exists, err := s.UserExists(ctx, username)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("the user %s does not exist", username)
}
user := model.User{
Username: username,
Name: name,
Email: email,
Phone: phone,
}
userJSON, err := json.Marshal(user)
if err != nil {
return err
}
return ctx.GetStub().PutState(username, userJSON)
}
// 删除指定ID的用户信息
func (s *UserContract) DeleteUser(ctx contractapi.TransactionContextInterface, username string) error {
exists, err := s.UserExists(ctx, username)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("the user %s does not exist", username)
}
return ctx.GetStub().DelState(username)
}
// 判断某用户是否存在
func (s *UserContract) UserExists(ctx contractapi.TransactionContextInterface, username string) (bool, error) {
userJSON, err := ctx.GetStub().GetState(username)
if err != nil {
return false, fmt.Errorf("failed to read from world state: %v", err)
}
return userJSON != nil, nil
}
// 读取所有用户信息
func (s *UserContract) GetAllUsers(ctx contractapi.TransactionContextInterface) ([]*model.User, error) {
// GetStateByRange 查询参数为两个空字符串时即查询所有数据
resultsIterator, err := ctx.GetStub().GetStateByRange("", "")
if err != nil {
return nil, err
}
defer resultsIterator.Close()
var users []*model.User
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return nil, err
}
var user model.User
err = json.Unmarshal(queryResponse.Value, &user)
if err != nil {
return nil, err
}
users = append(users, &user)
}
return users, nil
}
func (o *UserContract) InitLedger(ctx contractapi.TransactionContextInterface) error {
txs := []model.User{
{
Username: "fantasy",
Name: "fantasy name",
Email: "root@ifantasy.net",
Phone: "15877778888",
},
}
for _, tx := range txs {
txJSON, err := json.Marshal(tx)
if err != nil {
return err
}
err = ctx.GetStub().PutState(tx.Username, txJSON)
if err != nil {
return fmt.Errorf("failed to put to world state. %v", err)
}
}
return nil
}