-
Notifications
You must be signed in to change notification settings - Fork 8
/
samples.go
111 lines (80 loc) · 2.19 KB
/
samples.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
package main
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"multiorgledger/blockchain/invoke"
"multiorgledger/blockchain/org"
"multiorgledger/chaincode/model"
"sync"
)
type AllUsers struct {
Users []model.User `json:"users"`
}
func SampleUsers() {
var org string
fmt.Println("Enter the Org - ( org1, org2, org3, org4) ")
fmt.Scanln(&org)
byteValue, _ := ioutil.ReadFile("samples/" + org + ".json")
users := []model.User{}
v := []byte(byteValue)
err := json.Unmarshal(v, &users)
if err != nil {
fmt.Println("unable to convert the result to object: %v", err)
}
threads := len(users)
var wg sync.WaitGroup
respond := make(chan string, threads)
wg.Add(threads)
for i := 0; i < len(users); i++ {
email := users[i].Email
name := users[i].Name
mobile := users[i].Mobile
age := users[i].Age
salary := users[i].Salary
owner := users[i].Owner
role := users[i].Role
go createUser(respond, &wg, email, name, age, mobile, salary, owner, role)
}
wg.Wait()
close(respond)
for queryResp := range respond {
fmt.Println("Query Response: " + queryResp)
}
}
func hash(s string) string {
h := sha1.New()
h.Write([]byte(s))
sha1_hash := hex.EncodeToString(h.Sum(nil))
return sha1_hash
}
func createUser(respond chan<- string, wg *sync.WaitGroup, email, name, age, mobile, salary, owner, role string) {
fmt.Println(" Create User ---- " + email + " ---- Org = " + owner)
password := hash("Test@#123")
setup := &org.OrgSetup{}
_ = setup.Init(false)
Org, err := setup.InitializeOrg(owner)
if err != nil {
defer wg.Done()
respond <- fmt.Sprintf("Web ----- Unable to initialize org - %s", err.Error())
} else {
orgUser, err := Org.RegisterUserWithCA(owner, email, password, role)
orgInvoke := invoke.OrgInvoke{
User: orgUser,
}
if err != nil {
defer wg.Done()
respond <- fmt.Sprintf("Web Error ----->>> Unable to Register Error Msg - %s", err.Error())
} else {
err := orgInvoke.InvokeCreateUser(name, age, mobile, salary)
defer wg.Done()
if err != nil {
respond <- fmt.Sprintf("failed to invoke user - %s", err.Error())
} else {
respond <- fmt.Sprintf("User Created Successfully - %s", email)
}
}
}
}