-
Notifications
You must be signed in to change notification settings - Fork 22
/
request_type.go
131 lines (110 loc) · 4.1 KB
/
request_type.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
/**
* Copyright (c) 2018, 2019 National Digital ID COMPANY LIMITED
*
* This file is part of NDID software.
*
* NDID is the free software: you can redistribute it and/or modify it under
* the terms of the Affero GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or any later
* version.
*
* NDID is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Affero GNU General Public License for more details.
*
* You should have received a copy of the Affero GNU General Public License
* along with the NDID source code. If not, see https://www.gnu.org/licenses/agpl.txt.
*
* Please contact info@ndid.co.th for any further questions
*
*/
package app
import (
"encoding/json"
goleveldbutil "github.com/syndtr/goleveldb/leveldb/util"
"github.com/tendermint/tendermint/abci/types"
"github.com/ndidplatform/smart-contract/v7/abci/code"
"github.com/ndidplatform/smart-contract/v7/abci/utils"
data "github.com/ndidplatform/smart-contract/v7/protos/data"
)
type AddRequestTypeParam struct {
Name string `json:"name"`
}
// regulator only
func (app *ABCIApplication) addRequestType(param string, nodeID string) types.ResponseDeliverTx {
app.logger.Infof("AddRequestType, Parameter: %s", param)
var funcParam AddRequestTypeParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
key := requestTypeKeyPrefix + keySeparator + funcParam.Name
exists, err := app.state.Has([]byte(key), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
if exists {
return app.ReturnDeliverTxLog(code.RequestTypeAlreadyExists, "request type already exists", "")
}
var requestType data.RequestType
value, err := utils.ProtoDeterministicMarshal(&requestType)
if err != nil {
return app.ReturnDeliverTxLog(code.MarshalError, err.Error(), "")
}
app.state.Set([]byte(key), value)
return app.ReturnDeliverTxLog(code.OK, "success", "")
}
type RemoveRequestTypeParam struct {
Name string `json:"name"`
}
// regulator only
func (app *ABCIApplication) removeRequestType(param string, nodeID string) types.ResponseDeliverTx {
app.logger.Infof("RemoveRequestType, Parameter: %s", param)
var funcParam RemoveRequestTypeParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
key := requestTypeKeyPrefix + keySeparator + funcParam.Name
exists, err := app.state.Has([]byte(key), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
if !exists {
return app.ReturnDeliverTxLog(code.RequestTypeDoesNotExist, "request type does not exist", "")
}
app.state.Delete([]byte(key))
return app.ReturnDeliverTxLog(code.OK, "success", "")
}
type GetRequestTypeListParam struct {
Prefix string `json:"prefix"`
}
func (app *ABCIApplication) getRequestTypeList(param string, height int64) types.ResponseQuery {
app.logger.Infof("GetRequestTypeList, Parameter: %s", param)
var funcParam GetRequestTypeListParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
result := make([]string, 0)
requestTypeKeyIteratorBasePrefix := requestTypeKeyPrefix + keySeparator
requestTypeKeyIteratorPrefix := requestTypeKeyIteratorBasePrefix + funcParam.Prefix
r := goleveldbutil.BytesPrefix([]byte(requestTypeKeyIteratorPrefix))
iter, err := app.state.db.Iterator(r.Start, r.Limit)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
for ; iter.Valid(); iter.Next() {
key := iter.Key()
runes := []rune(string(key))
requestType := string(runes[len(requestTypeKeyIteratorBasePrefix):])
result = append(result, requestType)
}
iter.Close()
resultJSON, err := json.Marshal(result)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
return app.ReturnQuery(resultJSON, "success", app.state.Height)
}