-
Notifications
You must be signed in to change notification settings - Fork 1
/
superAdminHandler.go
142 lines (116 loc) · 4.76 KB
/
superAdminHandler.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
package handler
import (
"encoding/json"
"fmt"
"reflect"
"github.com/Akachain/akc-go-sdk/common"
"github.com/Akachain/akc-go-sdk/util"
"github.com/Akachain/hstx-go-sdk/model"
hUtil "github.com/Akachain/hstx-go-sdk/utils"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/mitchellh/mapstructure"
)
// SuperAdminHandler ...
type SuperAdminHandler struct{}
// CreateSuperAdmin ...
func (sah *SuperAdminHandler) CreateSuperAdmin(stub shim.ChaincodeStubInterface, superAdminStr string) (result *string, err error) {
common.Logger.Debugf("Input-data sent to CreateSuperAdmin func: %+v\n", superAdminStr)
err = hUtil.IsSuperAdmin(stub)
if err != nil {
return nil, fmt.Errorf("%s %s", err.Error(), common.GetLine())
}
superAdmin := new(model.SuperAdmin)
err = json.Unmarshal([]byte(superAdminStr), superAdmin)
if err != nil { // Return error: Can't unmarshal json
return nil, fmt.Errorf("%s %s %s", common.ResCodeDict[common.ERR3], err.Error(), common.GetLine())
}
if superAdmin.Status == "" {
superAdmin.Status = "A"
}
common.Logger.Infof("Create SuperAdmin: %+v\n", superAdmin)
err = util.Createdata(stub, model.SuperAdminTable, []string{superAdmin.SuperAdminID}, &superAdmin)
if err != nil { // Return error: Fail to insert data
return nil, fmt.Errorf("%s %s %s", common.ResCodeDict[common.ERR5], err.Error(), common.GetLine())
}
bytes, err := json.Marshal(superAdmin)
if err != nil { // Return error: Can't marshal json
return nil, fmt.Errorf("%s %s %s", common.ResCodeDict[common.ERR3], err.Error(), common.GetLine())
}
temp := ""
result = &temp
*result = string(bytes)
return result, nil
}
// GetAllSuperAdmin ...
func (sah *SuperAdminHandler) GetAllSuperAdmin(stub shim.ChaincodeStubInterface) (result *string, err error) {
res := util.GetAllData(stub, new(model.SuperAdmin), model.SuperAdminTable)
if res.Status == 200 {
return &res.Message, nil
}
return nil, fmt.Errorf("%s %s %s", common.ResCodeDict[common.ERR4], res.Message, common.GetLine())
}
// GetSuperAdminByID ...
func (sah *SuperAdminHandler) GetSuperAdminByID(stub shim.ChaincodeStubInterface, superAdminID string) (result *string, err error) {
common.Logger.Debugf("Input-data sent to GetSuperAdminByID func: %+v\n", superAdminID)
rawSuperAdmin, err := util.Getdatabyid(stub, superAdminID, model.SuperAdminTable)
if err != nil {
return nil, fmt.Errorf("%s %s %s", common.ResCodeDict[common.ERR4], err.Error(), common.GetLine())
}
superAdmin := new(model.SuperAdmin)
mapstructure.Decode(rawSuperAdmin, superAdmin)
bytes, err := json.Marshal(superAdmin)
if err != nil { // Return error: Can't marshal json
return nil, fmt.Errorf("%s %s %s", common.ResCodeDict[common.ERR3], err.Error(), common.GetLine())
}
temp := ""
result = &temp
*result = string(bytes)
return result, nil
}
//UpdateSuperAdmin ...
func (sah *SuperAdminHandler) UpdateSuperAdmin(stub shim.ChaincodeStubInterface, superAdminStr string) (result *string, err error) {
common.Logger.Debugf("Input-data sent to UpdateSuperAdmin func: %+v\n", superAdminStr)
err = hUtil.IsSuperAdmin(stub)
if err != nil {
return nil, fmt.Errorf("%s %s", err.Error(), common.GetLine())
}
newSuperAdmin := new(model.SuperAdmin)
err = json.Unmarshal([]byte(superAdminStr), newSuperAdmin)
if err != nil { // Return error: Can't unmarshal json
return nil, fmt.Errorf("%s %s %s", common.ResCodeDict[common.ERR3], err.Error(), common.GetLine())
}
if len(newSuperAdmin.SuperAdminID) == 0 {
return nil, fmt.Errorf("%s %s", "This ApprovalID can't be empty", common.GetLine())
}
// Get superAdmin information
rawSuperAdmin, err := util.Getdatabyid(stub, newSuperAdmin.SuperAdminID, model.SuperAdminTable)
if err != nil {
return nil, fmt.Errorf("%s %s %s", common.ResCodeDict[common.ERR4], err.Error(), common.GetLine())
}
superAdmin := new(model.SuperAdmin)
mapstructure.Decode(rawSuperAdmin, superAdmin)
// Filter fields needed to update
newSuperAdminValue := reflect.ValueOf(newSuperAdmin).Elem()
superAdminValue := reflect.ValueOf(superAdmin).Elem()
for i := 0; i < newSuperAdminValue.NumField(); i++ {
fieldName := newSuperAdminValue.Type().Field(i).Name
if len(newSuperAdminValue.Field(i).String()) > 0 {
field := superAdminValue.FieldByName(fieldName)
if field.CanSet() {
field.SetString(newSuperAdminValue.Field(i).String())
}
}
}
err = util.Changeinfo(stub, model.SuperAdminTable, []string{superAdmin.SuperAdminID}, superAdmin)
if err != nil {
return nil, fmt.Errorf("%s %s %s", common.ResCodeDict[common.ERR5], err.Error(), common.GetLine())
}
bytes, err := json.Marshal(superAdmin)
if err != nil { // Return error: Can't marshal json
return nil, fmt.Errorf("%s %s %s", common.ResCodeDict[common.ERR3], err.Error(), common.GetLine())
}
temp := ""
result = &temp
*result = string(bytes)
return result, nil
}