-
Notifications
You must be signed in to change notification settings - Fork 66
/
endpoint-tag.go
304 lines (245 loc) · 8.76 KB
/
endpoint-tag.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package repo
import (
"github.com/aaronchen2k/deeptest/internal/pkg/consts"
"github.com/aaronchen2k/deeptest/internal/server/modules/model"
logUtils "github.com/aaronchen2k/deeptest/pkg/lib/log"
"go.uber.org/zap"
)
type EndpointTagRepo struct {
*BaseRepo `inject:""`
EndpointInterfaceRepo *EndpointInterfaceRepo `inject:""`
ServeRepo *ServeRepo `inject:""`
ProjectRepo *ProjectRepo `inject:""`
}
func NewEndpointTagRepo() *EndpointTagRepo {
return &EndpointTagRepo{}
}
func (r *EndpointTagRepo) ListByProject(tenantId consts.TenantId, projectId uint) (tags []model.EndpointTag, err error) {
err = r.GetDB(tenantId).Model(&model.EndpointTag{}).
Where("project_id = ? AND NOT deleted AND NOT disabled", projectId).
Find(&tags).Error
return
}
func (r *EndpointTagRepo) Create(tenantId consts.TenantId, name string, projectId uint) (id uint, err error) {
tag := model.EndpointTag{
Name: name,
ProjectId: projectId,
}
err = r.GetDB(tenantId).Model(&model.EndpointTag{}).Create(&tag).Error
if err != nil {
logUtils.Errorf("add endpoint tag error", zap.String("error:", err.Error()))
return
}
id = tag.ID
return
}
func (r *EndpointTagRepo) BatchCreate(tenantId consts.TenantId, names []string, projectId uint) (err error) {
tags := make([]model.EndpointTag, 0)
for _, v := range names {
tag := model.EndpointTag{
Name: v,
ProjectId: projectId,
}
tags = append(tags, tag)
}
err = r.GetDB(tenantId).Model(&model.EndpointTag{}).
Create(tags).Error
if err != nil {
logUtils.Errorf("batch add endpoint tag error", zap.String("error:", err.Error()))
return
}
return
}
func (r *EndpointTagRepo) DeleteById(tenantId consts.TenantId, id uint) (err error) {
err = r.GetDB(tenantId).
Where("id = ?", id).
Delete(&model.EndpointTag{}).Error
if err != nil {
logUtils.Errorf("delete endpoint tag by id error", zap.String("error:", err.Error()))
return
}
return
}
func (r *EndpointTagRepo) BatchDeleteByIds(tenantId consts.TenantId, ids []uint) (err error) {
err = r.GetDB(tenantId).
Where("id IN (?)", ids).
Delete(&model.EndpointTag{}).Error
if err != nil {
logUtils.Errorf("batch delete endpoint tag by ids error", zap.String("error:", err.Error()))
return
}
return
}
func (r *EndpointTagRepo) BatchGetByName(tenantId consts.TenantId, names []string, projectId uint) (tags []model.EndpointTag, err error) {
err = r.GetDB(tenantId).Model(&model.EndpointTag{}).
Where("name IN (?)", names).
Where("project_id = ? AND NOT deleted AND NOT disabled", projectId).
Find(&tags).Error
return
}
func (r *EndpointTagRepo) BatchGetIdsByName(tenantId consts.TenantId, names []string, projectId uint) (ids []uint, err error) {
tags, err := r.BatchGetByName(tenantId, names, projectId)
if err != nil {
return
}
for _, tag := range tags {
ids = append(ids, tag.ID)
}
return
}
func (r *EndpointTagRepo) BatchGetById(tenantId consts.TenantId, ids []string, projectId uint) (tags []model.EndpointTag, err error) {
err = r.GetDB(tenantId).Model(&model.EndpointTag{}).
Where("id IN (?)", ids).
Where("project_id = ? AND NOT deleted AND NOT disabled", projectId).
Find(&tags).Error
return
}
func (r *EndpointTagRepo) ListRelByTagId(tenantId consts.TenantId, tagId uint) (rel []model.EndpointTagRel, err error) {
err = r.GetDB(tenantId).Model(&model.EndpointTagRel{}).
Where("tag_id = ?", tagId).
Find(&rel).Error
if err != nil {
logUtils.Errorf("get endpoint tag relation by tag_id error", zap.String("error:", err.Error()))
return
}
return
}
func (r *EndpointTagRepo) ListRelByEndpointId(tenantId consts.TenantId, endpointId uint) (rel []model.EndpointTagRel, err error) {
err = r.GetDB(tenantId).Model(&model.EndpointTagRel{}).
Where("endpoint_id = ?", endpointId).
Find(&rel).Error
if err != nil {
logUtils.Errorf("get endpoint tag relation by endpoint_id error", zap.String("error:", err.Error()))
return
}
return
}
func (r *EndpointTagRepo) GetTagIdsByEndpointId(tenantId consts.TenantId, endpointId uint) (tagIds []uint, err error) {
err = r.GetDB(tenantId).Model(&model.EndpointTagRel{}).
Where("endpoint_id = ?", endpointId).
Select("tag_id").
Find(&tagIds).Error
if err != nil {
logUtils.Errorf("get tag ids by endpoint_id error", zap.String("error:", err.Error()))
return
}
return
}
func (r *EndpointTagRepo) DeleteRelByEndpointId(tenantId consts.TenantId, endpointId uint) (err error) {
err = r.GetDB(tenantId).
Where("endpoint_id = ?", endpointId).
Delete(&model.EndpointTagRel{}).Error
if err != nil {
logUtils.Errorf("delete endpoint tag relation by endpoint_id error", zap.String("error:", err.Error()))
return
}
return
}
func (r *EndpointTagRepo) AddRel(tenantId consts.TenantId, endpointId uint, tagIds []uint) (err error) {
relations := make([]model.EndpointTagRel, 0)
for _, v := range tagIds {
relation := model.EndpointTagRel{
EndpointId: endpointId,
TagId: v,
}
relations = append(relations, relation)
}
err = r.GetDB(tenantId).Model(&model.EndpointTagRel{}).
Create(relations).Error
if err != nil {
logUtils.Errorf("batch add endpoint and tag relation error", zap.String("error:", err.Error()))
return
}
return
}
func (r *EndpointTagRepo) GetEndpointIdsByTagNames(tenantId consts.TenantId, tagNames []string, projectId int64) (endpointIds []uint, err error) {
//err = r.GetDB(tenantId).Model(&model.EndpointTagRel{}).
// Joins("LEFT JOIN biz_endpoint_tag t ON biz_endpoint_tag_rel.tag_id=t.id").
// Where("t.project_id = ?", projectId).
// Where("t.name IN (?) AND NOT t.deleted AND NOT t.disabled", tagNames).
// Select("biz_endpoint_tag_rel.endpoint_id").
// Find(&endpointIds).Error
err = r.GetDB(tenantId).Model(&model.EndpointTagRel{}).
Where("project_id = ?", projectId).
Where("tag_name IN (?) AND NOT deleted AND NOT disabled", tagNames).
Select("endpoint_id").
Find(&endpointIds).Error
return
}
func (r *EndpointTagRepo) GetTagNamesByEndpointId(tenantId consts.TenantId, endpointId, projectId uint) (tagNames []string, err error) {
//err = r.GetDB(tenantId).Model(&model.EndpointTag{}).
// Joins("LEFT JOIN biz_endpoint_tag_rel l ON biz_endpoint_tag.id=l.tag_id").
// Where("l.endpoint_id = ?", endpointId).
// Where("biz_endpoint_tag.project_id = ? AND NOT biz_endpoint_tag.deleted AND NOT biz_endpoint_tag.disabled", projectId).
// Select("biz_endpoint_tag.name").
// Find(&tagNames).Error
err = r.GetDB(tenantId).Model(&model.EndpointTagRel{}).
Where("endpoint_id = ?", endpointId).
Where("project_id = ? AND NOT deleted AND NOT disabled", projectId).
Select("tag_name").
Find(&tagNames).Error
return
}
func (r *EndpointTagRepo) DeleteRelByEndpointAndProject(tenantId consts.TenantId, endpointId, projectId uint) (err error) {
err = r.GetDB(tenantId).
Where("endpoint_id = ?", endpointId).
Where("project_id = ?", projectId).
Delete(&model.EndpointTagRel{}).Error
if err != nil {
logUtils.Errorf("delete endpoint tag relation by endpoint and project error", zap.String("error:", err.Error()))
return
}
return
}
func (r *EndpointTagRepo) BatchAddRel(tenantId consts.TenantId, endpointId, projectId uint, tagNames []string) (err error) {
relations := make([]model.EndpointTagRel, 0)
for _, v := range tagNames {
relation := model.EndpointTagRel{
EndpointId: endpointId,
TagName: v,
ProjectId: projectId,
}
relations = append(relations, relation)
}
err = r.GetDB(tenantId).Model(&model.EndpointTagRel{}).
Create(relations).Error
if err != nil {
logUtils.Errorf("batch add endpoint and tag relation error", zap.String("error:", err.Error()))
return
}
return
}
func (r *EndpointTagRepo) ListRelByProject(tenantId consts.TenantId, projectId uint) (tags []model.EndpointTagRel, err error) {
err = r.GetDB(tenantId).Model(&model.EndpointTagRel{}).
Distinct("tag_name").
Where("project_id = ? AND NOT deleted AND NOT disabled", projectId).
Find(&tags).Error
return
}
func (r *EndpointTagRepo) BatchAddRelForTag(tagName string, endpointIds []uint, projectId uint) (err error) {
relations := make([]model.EndpointTagRel, 0)
for _, v := range endpointIds {
relation := model.EndpointTagRel{
EndpointId: v,
TagName: tagName,
ProjectId: projectId,
}
relations = append(relations, relation)
}
err = r.DB.Model(&model.EndpointTagRel{}).
Create(relations).Error
if err != nil {
logUtils.Errorf("batch add tag relation for endpoint error", zap.String("error:", err.Error()))
return
}
return
}
func (r *EndpointTagRepo) BatchGetEndpointIdsByTag(tagName string, endpointIds []uint, projectId uint) (res []uint, err error) {
err = r.DB.Model(&model.EndpointTagRel{}).
Where("tag_name = ?", tagName).
Where("endpoint_id IN (?)", endpointIds).
Where("project_id = ? AND NOT deleted AND NOT disabled", projectId).
Select("endpoint_id").
Find(&res).Error
return
}