-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.go
216 lines (188 loc) · 5.62 KB
/
demo.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
/*
@Date: 2021/1/12 下午2:32
@Author: yvanz
@File : controller_demo
@Desc:
*/
package handler
import (
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/yvanz/gin-tmpl/internal/common"
"github.com/yvanz/gin-tmpl/internal/logic/srvdemo"
"github.com/yvanz/gin-tmpl/pkg/gormdb"
)
type checkController struct {
common.BaseController
}
func newCheckController(base common.BaseController) *checkController {
return &checkController{BaseController: base}
}
// @Summary 获取所有数据
// @Description 获取所有数据
// @Tags Demo
// @Accept json
// @Produce json
// @param q query string false "自定义查询语句, 使用 RSQL 语法"
// @Param pagelimit query int false "分页条数"
// @Param pageoffset query int false "分页偏移量"
// @Param keyword query string false "关键字模糊查询"
// @Param order query string false "排序, 支持desc和asc, 如 id desc"
// @Success 200 {object} common.Response "结果:{ret_code:code,data:数据,message:消息}"
// @Failure 500 {object} common.Response "结果:{ret_code:code,data:数据,message:消息}"
// @Router /demo/test [get]
func (pc *checkController) Get(c *gin.Context) {
var (
svc srvdemo.Svc
err error
)
pageArg := c.Query("pageoffset")
limitArg := c.Query("pagelimit")
keyword := c.Query("keyword")
keyword = strings.TrimSpace(keyword)
orderBy := c.Query("order")
in := c.Query("q")
var page, limit int
err = common.GetValidator().Var(pageArg, "number")
if err == nil {
page, _ = strconv.Atoi(pageArg)
} else {
page = 0
}
err = common.GetValidator().Var(limitArg, "number")
if err == nil {
limit, _ = strconv.Atoi(limitArg)
} else {
limit = 10
}
svc.Ctx = c
q := gormdb.BasicQuery{
Keyword: keyword,
Order: orderBy,
Limit: limit,
Offset: page,
Query: in,
}
data, err := svc.GetDemoList(q)
pc.Response(c, data, err)
}
// @Summary 获取指定ID详情
// @Description 获取详情
// @Tags Demo
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Success 200 {object} common.Response{data_set=models.Demo} "结果:{ret_code:code,data:数据,message:消息}"
// @Failure 500 {object} common.Response "结果:{ret_code:code,data:数据,message:消息}"
// @Router /demo/test/{id} [get]
func (pc *checkController) GetByID(c *gin.Context) {
var (
svc srvdemo.Svc
err error
)
id := c.Param("id")
err = common.GetValidator().Var(id, "number")
if err != nil {
pc.Response(c, nil, common.NewCodeWithErr(common.ErrInvalidParams, err))
return
}
idInt, _ := strconv.Atoi(id)
svc.ID = int64(idInt)
svc.Ctx = c
data, err := svc.GetByID()
pc.Response(c, data, err)
}
// @Summary 新增数据
// @Description 新增数据
// @Tags Demo
// @Accept json
// @Produce json
// @Param params body srvdemo.AddParams true "demo"
// @Success 200 {object} common.Response "结果:{ret_code:code,data:数据,message:消息}"
// @Failure 500 {object} common.Response "结果:{ret_code:code,data:数据,message:消息}"
// @Router /demo/test [post]
func (pc *checkController) Create(c *gin.Context) {
var (
svc srvdemo.Svc
err error
params srvdemo.AddParams
)
if !pc.CheckParams(c, ¶ms) {
return
}
svc.Ctx = c
err = svc.Add(params)
pc.Response(c, nil, err)
}
// @Summary 发送消息
// @Description 发送消息
// @Tags Demo
// @Accept json
// @Produce json
// @Param params body srvdemo.AddParams true "demo"
// @Success 200 {object} common.Response "结果:{ret_code:code,data:数据,message:消息}"
// @Failure 500 {object} common.Response "结果:{ret_code:code,data:数据,message:消息}"
// @Router /demo/test/message [post]
func (pc *checkController) CreateMessage(c *gin.Context) {
var (
svc srvdemo.Svc
err error
params srvdemo.AddParams
)
if !pc.CheckParams(c, ¶ms) {
return
}
svc.Ctx = c
err = svc.KafkaMessage(params)
pc.Response(c, nil, err)
}
// @Summary 更新数据
// @Description 更新数据
// @Tags Demo
// @Accept json
// @Produce json
// @Param id path string true "id"
// @Param param body srvdemo.AddParams true "IDC detail"
// @Success 200 {object} common.Response "结果:{ret_code:code,data:数据,message:消息}"
// @Failure 500 {object} common.Response "结果:{ret_code:code,data:数据,message:消息}"
// @Router /demo/test/{id} [put]
func (pc *checkController) Update(c *gin.Context) {
var (
params srvdemo.AddParams
svc srvdemo.Svc
err error
)
if !pc.CheckParams(c, ¶ms) {
return
}
id := c.Param("id")
err = common.GetValidator().Var(id, "number")
if err != nil {
pc.Response(c, nil, common.NewCodeWithErr(common.ErrInvalidParams, err))
return
}
svc.ID, _ = strconv.ParseInt(id, 10, 64)
svc.Ctx = c
err = svc.Mod(params)
pc.Response(c, nil, err)
}
// @Summary 删除数据
// @Description 删除数据
// @Tags Demo
// @Accept json
// @Produce json
// @Param ids path string true "ids"
// @Success 200 {object} common.Response "结果:{ret_code:code,data:数据,message:消息}"
// @Failure 500 {object} common.Response "结果:{ret_code:code,data:数据,message:消息}"
// @Router /demo/test/{ids} [delete]
func (pc *checkController) Delete(c *gin.Context) {
var (
err error
srv srvdemo.Svc
)
ids := c.Param("ids")
idList := strings.Split(ids, ",")
err = srv.Delete(idList)
pc.Response(c, nil, err)
}