-
Notifications
You must be signed in to change notification settings - Fork 6
/
relation.go
169 lines (136 loc) · 4.27 KB
/
relation.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
package manager
import (
"encoding/json"
"fmt"
"net/http"
"dearcode.net/crab/log"
"github.com/juju/errors"
"dearcode.net/doodle/pkg/meta"
"dearcode.net/doodle/pkg/util"
)
type relation struct {
}
func (ra *relation) GET(w http.ResponseWriter, r *http.Request) {
vars := struct {
AppID int64 `json:"applicationID"`
InterfaceID int64 `json:"interfaceID"`
Sort string `json:"sort"`
Order string `json:"order"`
Page int `json:"offset"`
Size int `json:"limit"`
}{}
if err := util.DecodeRequestValue(r, &vars); err != nil {
fmt.Fprintf(w, "%v", err)
return
}
var where string
switch vars.Sort {
case "InterfaceName":
vars.Sort = "interface.name"
case "ServiceName":
vars.Sort = "service.name"
}
if vars.AppID != 0 {
where = fmt.Sprintf("relation.application_id=%d", vars.AppID)
}
if vars.InterfaceID != 0 {
if where != "" {
where += " and "
}
where += fmt.Sprintf("relation.interface_id=%d", vars.InterfaceID)
}
if where != "" {
where += " and "
}
where += " relation.interface_id=interface.id and relation.application_id = application.id and interface.service_id = service.id"
var rs []meta.Relation
total, err := query("relation, application, interface, service", where, vars.Sort, vars.Order, vars.Page, vars.Size, &rs)
if err != nil {
fmt.Fprintf(w, "%v", err)
return
}
if len(rs) == 0 {
w.Header().Set("Content-Type", "Relationlication/json")
w.Write([]byte(`{"total":0,"rows":[]}`))
log.Debugf("service not found")
return
}
result := struct {
Total int `json:"total"`
Rows []meta.Relation `json:"rows"`
}{total, rs}
buf, err := json.Marshal(result)
if err != nil {
fmt.Fprintf(w, "%v", err)
return
}
w.Header().Set("Content-Type", "Relationlication/json")
w.Write(buf)
}
func (ra *relation) PUT(w http.ResponseWriter, r *http.Request) {
vars := struct {
ID int64 `json:"id"`
AppID int64 `json:"appID"`
InterfaceID int64 `json:"interfaceID"`
}{}
if err := util.DecodeRequestValue(r, &vars); err != nil {
log.Errorf("DecodeRequestValue req:%+v, error:%s", r, errors.ErrorStack(err))
util.SendResponse(w, http.StatusInternalServerError, err.Error())
return
}
if err := updateRelation(vars.ID, vars.InterfaceID, vars.AppID); err != nil {
log.Errorf("updateRelation req:%+v, error:%s", r, errors.ErrorStack(err))
util.SendResponse(w, http.StatusInternalServerError, err.Error())
return
}
util.SendResponse(w, 0, "")
log.Debugf("update relation success, vars:%+v", vars)
}
// TODO 删除应用,删除接口时要调用这个接口把关系也删除了
func (ra *relation) DELETE(w http.ResponseWriter, r *http.Request) {
vars := struct {
ID int64 `json:"id"`
}{}
if err := util.DecodeRequestValue(r, &vars); err != nil {
util.SendResponse(w, http.StatusInternalServerError, err.Error())
return
}
if err := del("relation", vars.ID); err != nil {
util.SendResponse(w, http.StatusInternalServerError, err.Error())
return
}
util.SendResponse(w, 0, "")
log.Debugf("delete service:%v, success", vars.ID)
}
func (ra *relation) POST(w http.ResponseWriter, r *http.Request) {
vars := struct {
AppID int64 `db:"application_id" json:"appID"`
InterfaceID int64 `db:"interface_id" json:"interfaceID"`
CTime string `db_default:"now()"`
Mtime string `db_default:"now()"`
}{}
if err := util.DecodeRequestValue(r, &vars); err != nil {
log.Errorf("DecodeRequestValue req:%+v, error:%s", r, errors.ErrorStack(err))
util.SendResponse(w, http.StatusInternalServerError, err.Error())
return
}
state, err := getInterfaceState(vars.InterfaceID)
if err != nil {
log.Errorf("getInterfaceState req:%+v, error:%s", r, errors.ErrorStack(err))
util.SendResponse(w, http.StatusInternalServerError, err.Error())
return
}
if state != 1 {
log.Errorf("interface:%d state:%d", vars.InterfaceID, state)
util.SendResponse(w, http.StatusInternalServerError, "接口未发布,不可以授权应用")
return
}
id, err := add("relation", vars)
if err != nil {
log.Errorf("addRelation req:%+v, error:%s", r, errors.ErrorStack(err))
util.SendResponse(w, http.StatusInternalServerError, err.Error())
return
}
util.SendResponse(w, 0, fmt.Sprintf(`{"id":%d}`, id))
log.Debugf("add relation success, id:%v, %+v", id, vars)
}