/
demo.go
176 lines (162 loc) · 5.71 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
package ctl
import (
"github.com/gin-gonic/gin"
"github.com/souxls/dvs_api/internal/app/bll"
"github.com/souxls/dvs_api/internal/app/ginplus"
"github.com/souxls/dvs_api/internal/app/schema"
"github.com/souxls/dvs_api/pkg/util"
)
// NewDemo 创建demo控制器
func NewDemo(bDemo bll.IDemo) *Demo {
return &Demo{
DemoBll: bDemo,
}
}
// Demo 示例程序
type Demo struct {
DemoBll bll.IDemo
}
// Query 查询数据
// @Tags Demo
// @Summary 查询数据
// @Param Authorization header string false "Bearer 用户令牌"
// @Param current query int true "分页索引" default(1)
// @Param pageSize query int true "分页大小" default(10)
// @Param code query string false "编号"
// @Param name query string false "名称"
// @Param status query int false "状态(1:启用 2:停用)"
// @Success 200 {array} schema.Demo "查询结果:{list:列表数据,pagination:{current:页索引,pageSize:页大小,total:总数量}}"
// @Failure 401 {object} schema.HTTPError "{error:{code:0,message:未授权}}"
// @Failure 500 {object} schema.HTTPError "{error:{code:0,message:服务器错误}}"
// @Router /api/v1/demos [get]
func (a *Demo) Query(c *gin.Context) {
var params schema.DemoQueryParam
params.LikeCode = c.Query("code")
params.LikeName = c.Query("name")
params.Status = util.S(c.Query("status")).DefaultInt(0)
result, err := a.DemoBll.Query(ginplus.NewContext(c), params, schema.DemoQueryOptions{
PageParam: ginplus.GetPaginationParam(c),
})
if err != nil {
ginplus.ResError(c, err)
return
}
ginplus.ResPage(c, result.Data, result.PageResult)
}
// Get 查询指定数据
// @Tags Demo
// @Summary 查询指定数据
// @Param Authorization header string false "Bearer 用户令牌"
// @Param id path string true "记录ID"
// @Success 200 {object} schema.Demo
// @Failure 401 {object} schema.HTTPError "{error:{code:0,message:未授权}}"
// @Failure 404 {object} schema.HTTPError "{error:{code:0,message:资源不存在}}"
// @Failure 500 {object} schema.HTTPError "{error:{code:0,message:服务器错误}}"
// @Router /api/v1/demos/{id} [get]
func (a *Demo) Get(c *gin.Context) {
item, err := a.DemoBll.Get(ginplus.NewContext(c), c.Param("id"))
if err != nil {
ginplus.ResError(c, err)
return
}
ginplus.ResSuccess(c, item)
}
// Create 创建数据
// @Tags Demo
// @Summary 创建数据
// @Param Authorization header string false "Bearer 用户令牌"
// @Param body body schema.Demo true "创建数据"
// @Success 200 {object} schema.Demo
// @Failure 400 {object} schema.HTTPError "{error:{code:0,message:无效的请求参数}}"
// @Failure 401 {object} schema.HTTPError "{error:{code:0,message:未授权}}"
// @Failure 500 {object} schema.HTTPError "{error:{code:0,message:服务器错误}}"
// @Router /api/v1/demos [post]
func (a *Demo) Create(c *gin.Context) {
var item schema.Demo
if err := ginplus.ParseJSON(c, &item); err != nil {
ginplus.ResError(c, err)
return
}
item.Creator = ginplus.GetUserID(c)
nitem, err := a.DemoBll.Create(ginplus.NewContext(c), item)
if err != nil {
ginplus.ResError(c, err)
return
}
ginplus.ResSuccess(c, nitem)
}
// Update 更新数据
// @Tags Demo
// @Summary 更新数据
// @Param Authorization header string false "Bearer 用户令牌"
// @Param id path string true "记录ID"
// @Param body body schema.Demo true "更新数据"
// @Success 200 {object} schema.Demo
// @Failure 400 {object} schema.HTTPError "{error:{code:0,message:无效的请求参数}}"
// @Failure 401 {object} schema.HTTPError "{error:{code:0,message:未授权}}"
// @Failure 500 {object} schema.HTTPError "{error:{code:0,message:服务器错误}}"
// @Router /api/v1/demos/{id} [put]
func (a *Demo) Update(c *gin.Context) {
var item schema.Demo
if err := ginplus.ParseJSON(c, &item); err != nil {
ginplus.ResError(c, err)
return
}
nitem, err := a.DemoBll.Update(ginplus.NewContext(c), c.Param("id"), item)
if err != nil {
ginplus.ResError(c, err)
return
}
ginplus.ResSuccess(c, nitem)
}
// Delete 删除数据
// @Tags Demo
// @Summary 删除数据
// @Param Authorization header string false "Bearer 用户令牌"
// @Param id path string true "记录ID"
// @Success 200 {object} schema.HTTPStatus "{status:OK}"
// @Failure 401 {object} schema.HTTPError "{error:{code:0,message:未授权}}"
// @Failure 500 {object} schema.HTTPError "{error:{code:0,message:服务器错误}}"
// @Router /api/v1/demos/{id} [delete]
func (a *Demo) Delete(c *gin.Context) {
err := a.DemoBll.Delete(ginplus.NewContext(c), c.Param("id"))
if err != nil {
ginplus.ResError(c, err)
return
}
ginplus.ResOK(c)
}
// Enable 启用数据
// @Tags Demo
// @Summary 启用数据
// @Param Authorization header string false "Bearer 用户令牌"
// @Param id path string true "记录ID"
// @Success 200 {object} schema.HTTPStatus "{status:OK}"
// @Failure 401 {object} schema.HTTPError "{error:{code:0,message:未授权}}"
// @Failure 500 {object} schema.HTTPError "{error:{code:0,message:服务器错误}}"
// @Router /api/v1/demos/{id}/enable [patch]
func (a *Demo) Enable(c *gin.Context) {
err := a.DemoBll.UpdateStatus(ginplus.NewContext(c), c.Param("id"), 1)
if err != nil {
ginplus.ResError(c, err)
return
}
ginplus.ResOK(c)
}
// Disable 禁用数据
// @Tags Demo
// @Summary 禁用数据
// @Param Authorization header string false "Bearer 用户令牌"
// @Param id path string true "记录ID"
// @Success 200 {object} schema.HTTPStatus "{status:OK}"
// @Failure 401 {object} schema.HTTPError "{error:{code:0,message:未授权}}"
// @Failure 500 {object} schema.HTTPError "{error:{code:0,message:服务器错误}}"
// @Router /api/v1/demos/{id}/disable [patch]
func (a *Demo) Disable(c *gin.Context) {
err := a.DemoBll.UpdateStatus(ginplus.NewContext(c), c.Param("id"), 2)
if err != nil {
ginplus.ResError(c, err)
return
}
ginplus.ResOK(c)
}