This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
invoke_shop.go
172 lines (152 loc) · 4.24 KB
/
invoke_shop.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package app
import (
"encoding/json"
"time"
"github.com/hyperledger/fabric-chaincode-go/shim"
pb "github.com/hyperledger/fabric-protos-go/peer"
)
func createContract(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Invalid argument count.")
}
dto := struct {
UUID string `json:"uuid"`
ContractTypeUUID string `json:"contract_type_uuid"`
Username string `json:"username"`
Password string `json:"password"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Item Item `json:"item"`
StartDate time.Time `json:"start_date"`
EndDate time.Time `json:"end_date"`
}{}
err := json.Unmarshal([]byte(args[0]), &dto)
if err != nil {
return shim.Error(err.Error())
}
// Create new user if necessary
var u User
requestUserCreate := len(dto.Username) > 0 && len(dto.Password) > 0
userKey, err := stub.CreateCompositeKey(prefixUser, []string{dto.Username})
if requestUserCreate {
// Check if a user with the same username exists
if err != nil {
return shim.Error(err.Error())
}
userAsBytes, _ := stub.GetState(userKey)
if userAsBytes == nil {
// Create new user
u = User{
Username: dto.Username,
Password: dto.Password,
FirstName: dto.FirstName,
LastName: dto.LastName,
}
// Persist the new user
userAsBytes, err := json.Marshal(u)
if err != nil {
return shim.Error(err.Error())
}
err = stub.PutState(userKey, userAsBytes)
if err != nil {
return shim.Error(err.Error())
}
} else {
// Parse the existing user
err := json.Unmarshal(userAsBytes, &u)
if err != nil {
return shim.Error(err.Error())
}
}
} else {
// Validate if the user with the provided username exists
userAsBytes, _ := stub.GetState(userKey)
if userAsBytes == nil {
return shim.Error("User with this username does not exist.")
}
}
contract := Contract{
Username: dto.Username,
ContractTypeUUID: dto.ContractTypeUUID,
Item: dto.Item,
StartDate: dto.StartDate,
EndDate: dto.EndDate,
Void: false,
ClaimIndex: []string{},
}
contractKey, err := stub.CreateCompositeKey(prefixContract, []string{dto.Username, dto.UUID})
if err != nil {
return shim.Error(err.Error())
}
contractAsBytes, err := json.Marshal(contract)
if err != nil {
return shim.Error(err.Error())
}
err = stub.PutState(contractKey, contractAsBytes)
if err != nil {
return shim.Error(err.Error())
}
// Return success, if the new user has been created
// (the user variable "u" should be blank)
if !requestUserCreate {
return shim.Success(nil)
}
response := struct {
Username string `json:"username"`
Password string `json:"password"`
}{
Username: u.Username,
Password: u.Password,
}
responseAsBytes, err := json.Marshal(response)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(responseAsBytes)
}
func createUser(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Invalid argument count.")
}
user := User{}
err := json.Unmarshal([]byte(args[0]), &user)
if err != nil {
return shim.Error(err.Error())
}
key, err := stub.CreateCompositeKey(prefixUser, []string{user.Username})
if err != nil {
return shim.Error(err.Error())
}
// Check if the user already exists
userAsBytes, _ := stub.GetState(key)
// User does not exist, attempting creation
if len(userAsBytes) == 0 {
userAsBytes, err = json.Marshal(user)
if err != nil {
return shim.Error(err.Error())
}
err = stub.PutState(key, userAsBytes)
if err != nil {
return shim.Error(err.Error())
}
// Return nil, if user is newly created
return shim.Success(nil)
}
err = json.Unmarshal(userAsBytes, &user)
if err != nil {
return shim.Error(err.Error())
}
userResponse := struct {
Username string `json:"username"`
Password string `json:"password"`
}{
Username: user.Username,
Password: user.Password,
}
userResponseAsBytes, err := json.Marshal(userResponse)
if err != nil {
return shim.Error(err.Error())
}
// Return the username and the password of the already existing user
return shim.Success(userResponseAsBytes)
}