-
Notifications
You must be signed in to change notification settings - Fork 66
/
role.go
198 lines (181 loc) · 6.83 KB
/
role.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
package handler
import (
"github.com/aaronchen2k/deeptest/cmd/server/v1/domain"
"github.com/aaronchen2k/deeptest/internal/server/core/web/validate"
"github.com/aaronchen2k/deeptest/internal/server/modules/service"
"github.com/aaronchen2k/deeptest/pkg/domain"
logUtils "github.com/aaronchen2k/deeptest/pkg/lib/log"
"github.com/snowlyg/multi"
"strings"
"github.com/kataras/iris/v12"
"go.uber.org/zap"
)
type RoleCtrl struct {
BaseCtrl
RoleService *service.RoleService `inject:""`
}
// GetAllRoles 分页列表
// @Tags 角色模块
// @summary 角色列表
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param RoleReqPaginate query serverDomain.RoleReqPaginate true "获取角色列表的请求参数"
// @success 200 {object} _domain.Response{data=_domain.PageData{result=[]serverDomain.RoleResp}}
// @Router /api/v1/roles [get]
func (c *RoleCtrl) GetAllRoles(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
var req serverDomain.RoleReqPaginate
if err := ctx.ReadQuery(&req); err != nil {
errs := validate.ValidRequest(err)
if len(errs) > 0 {
logUtils.Errorf("参数验证失败", zap.String("错误", strings.Join(errs, ";")))
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: strings.Join(errs, ";")})
return
}
}
data, err := c.RoleService.Paginate(tenantId, req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: data, Msg: _domain.NoErr.Msg})
}
// GetRole 详情
// @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=serverDomain.RoleResp}
// @Router /api/v1/roles/{id} [get]
func (c *RoleCtrl) GetRole(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
var req _domain.ReqId
if err := ctx.ReadParams(&req); err != nil {
logUtils.Errorf("参数解析失败", zap.String("错误:", err.Error()))
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: _domain.ParamErr.Msg})
return
}
role, err := c.RoleService.FindById(tenantId, req.Id)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: _domain.SystemErr.Msg})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: role, Msg: _domain.NoErr.Msg})
}
// CreateRole 添加
// @Tags 角色模块
// @summary 新建角色
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param RoleReq body serverDomain.RoleReq true "新建角色的请求参数"
// @success 200 {object} _domain.Response{data=object{id=int}}
// @Router /api/v1/roles [post]
func (c *RoleCtrl) CreateRole(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
req := serverDomain.RoleReq{}
if err := ctx.ReadJSON(&req); err != nil {
errs := validate.ValidRequest(err)
if len(errs) > 0 {
logUtils.Errorf("参数验证失败", zap.String("错误", strings.Join(errs, ";")))
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: strings.Join(errs, ";")})
return
}
}
id, err := c.RoleService.Create(tenantId, req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: iris.Map{"id": id}, Msg: _domain.NoErr.Msg})
}
// UpdateRole 更新
// @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 RoleReq body serverDomain.RoleReq true "编辑角色的请求参数"
// @success 200 {object} _domain.Response
// @Router /api/v1/roles/{id} [post]
func (c *RoleCtrl) UpdateRole(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
id, _ := ctx.Params().GetInt("id")
var req serverDomain.RoleReq
if err := ctx.ReadJSON(&req); err != nil {
errs := validate.ValidRequest(err)
if len(errs) > 0 {
logUtils.Errorf("参数验证失败", zap.String("错误", strings.Join(errs, ";")))
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: strings.Join(errs, ";")})
return
}
}
err := c.RoleService.Update(tenantId, uint(id), req)
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})
}
// DeleteRole 删除
// @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=serverDomain.RoleResp}
// @Router /api/v1/roles/{id} [delete]
func (c *RoleCtrl) DeleteRole(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
var req _domain.ReqId
if err := ctx.ReadParams(&req); err != nil {
logUtils.Errorf("参数解析失败", zap.String("错误:", err.Error()))
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: _domain.ParamErr.Msg})
return
}
err := c.RoleService.DeleteById(tenantId, req.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})
}
// AllRoleList
// @Tags 角色模块
// @summary 无分页的角色列表
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @success 200 {object} _domain.Response{data=object{result=[]serverDomain.RoleResp}}
// @Router /api/v1/roles/all [get]
func (c *RoleCtrl) AllRoleList(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
roles, err := c.RoleService.AllRoleList(tenantId)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
data := iris.Map{"result": roles}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: data, Msg: _domain.NoErr.Msg})
}
func (c *RoleCtrl) GetAuthByEnv(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
userId := multi.GetUserId(ctx)
res, err := c.RoleService.GetAuthByEnv(tenantId, userId)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: res, Msg: _domain.NoErr.Msg})
}