/
datapool.go
170 lines (150 loc) · 5.25 KB
/
datapool.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
package handler
import (
"fmt"
serverDomain "github.com/aaronchen2k/deeptest/cmd/server/v1/domain"
"github.com/aaronchen2k/deeptest/internal/server/modules/model"
"github.com/aaronchen2k/deeptest/internal/server/modules/service"
"github.com/aaronchen2k/deeptest/pkg/domain"
"github.com/kataras/iris/v12"
"github.com/snowlyg/multi"
)
type DatapoolCtrl struct {
DatapoolService *service.DatapoolService `inject:""`
BaseCtrl
}
// Index
// @Tags 数据池
// @summary 数据池列表
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param DatapoolReqPaginate body serverDomain.DatapoolReqPaginate true "获取数据池列表的请求体"
// @success 200 {object} _domain.Response{data=_domain.PageData{result=[]model.Datapool}}
// @Router /api/v1/datapools/index [post]
func (c *DatapoolCtrl) Index(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
var req serverDomain.DatapoolReqPaginate
err := ctx.ReadJSON(&req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
if req.ProjectId == 0 {
req.ProjectId, _ = ctx.URLParamInt64("currProjectId")
}
res, _ := c.DatapoolService.Paginate(tenantId, req)
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: res})
}
// Get
// @Tags 数据池
// @summary 数据池详情
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param id path int true "数据池ID"
// @success 200 {object} _domain.Response{data=model.Datapool}
// @Router /api/v1/datapools/{id} [get]
func (c *DatapoolCtrl) Get(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
id, err := ctx.Params().GetInt("id")
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: err.Error()})
return
}
datapool, err := c.DatapoolService.Get(tenantId, uint(id))
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: datapool, Msg: _domain.NoErr.Msg})
}
// Save
// @Tags 数据池
// @summary 保存数据池
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param Datapool body model.Datapool true "保存数据池的请求体"
// @success 200 {object} _domain.Response
// @Router /api/v1/datapools/save [post]
func (c *DatapoolCtrl) Save(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
userId := multi.GetUserId(ctx)
projectId, err := ctx.URLParamInt("currProjectId")
if projectId == 0 {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: "projectId"})
return
}
req := model.Datapool{}
err = ctx.ReadJSON(&req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: err.Error()})
return
}
req.ProjectId = uint(projectId)
// check name exist
po, err := c.DatapoolService.GetByName(tenantId, req.Name, req.ProjectId)
if po.ID > 0 && po.ID != req.ID {
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code,
MsgKey: fmt.Sprintf("%v", _domain.ErrNameExist.Code)})
return
}
err = c.DatapoolService.Save(tenantId, &req, userId)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Data: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code})
}
// Delete
// @Tags 数据池
// @summary 删除数据池
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param id path int true "数据池ID"
// @success 200 {object} _domain.Response
// @Router /api/v1/datapools/{id} [delete]
func (c *DatapoolCtrl) Delete(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
id, err := ctx.Params().GetInt("id")
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: err.Error()})
return
}
err = c.DatapoolService.Delete(tenantId, uint(id))
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Msg: _domain.NoErr.Msg})
}
// Disable
// @Tags 数据池
// @summary 禁用数据池
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param id path int true "数据池ID"
// @Param Datapool body model.Datapool true "保存数据池的请求体"
// @success 200 {object} _domain.Response
// @Router /api/v1/datapools/{id}/disable [put]
func (c *DatapoolCtrl) Disable(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
id, err := ctx.Params().GetInt("id")
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: err.Error()})
return
}
err = c.DatapoolService.Disable(tenantId, uint(id))
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Msg: _domain.NoErr.Msg})
}