-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.go
280 lines (252 loc) · 7.34 KB
/
api.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
package cosy
import (
"github.com/gin-gonic/gin"
"github.com/uozi-tech/cosy/model"
)
type ICurd[T any] interface {
Get() []gin.HandlerFunc
GetList() []gin.HandlerFunc
Create() []gin.HandlerFunc
Modify() []gin.HandlerFunc
Destroy() []gin.HandlerFunc
Recover() []gin.HandlerFunc
BeforeCreate(...gin.HandlerFunc) ICurd[T]
BeforeModify(...gin.HandlerFunc) ICurd[T]
BeforeGet(...gin.HandlerFunc) ICurd[T]
BeforeGetList(...gin.HandlerFunc) ICurd[T]
BeforeDestroy(...gin.HandlerFunc) ICurd[T]
BeforeRecover(...gin.HandlerFunc) ICurd[T]
GetHook(...func(*Ctx[T]))
GetListHook(...func(*Ctx[T]))
CreateHook(...func(*Ctx[T]))
ModifyHook(...func(*Ctx[T]))
DestroyHook(...func(*Ctx[T]))
RecoverHook(...func(*Ctx[T]))
InitRouter(*gin.RouterGroup, ...gin.HandlerFunc)
}
type Curd[T any] struct {
ICurd[T]
baseUrl string
getHook []func(*Ctx[T])
getListHook []func(*Ctx[T])
createHook []func(*Ctx[T])
modifyHook []func(*Ctx[T])
destroyHook []func(*Ctx[T])
recoverHook []func(*Ctx[T])
beforeCreate []gin.HandlerFunc
beforeModify []gin.HandlerFunc
beforeGet []gin.HandlerFunc
beforeGetList []gin.HandlerFunc
beforeDestroy []gin.HandlerFunc
beforeRecover []gin.HandlerFunc
}
// Api returns a new instance of Curd
func Api[T any](baseUrl string) ICurd[T] {
return &Curd[T]{
baseUrl: baseUrl,
}
}
// BeforeCreate registers a hook function to be called before the creating action
func (c *Curd[T]) BeforeCreate(hooks ...gin.HandlerFunc) ICurd[T] {
c.beforeCreate = append(c.beforeCreate, hooks...)
return c
}
// BeforeModify registers a hook function to be called before the modifying action
func (c *Curd[T]) BeforeModify(hooks ...gin.HandlerFunc) ICurd[T] {
c.beforeModify = append(c.beforeModify, hooks...)
return c
}
// BeforeGet registers a hook function to be called before the getting action
func (c *Curd[T]) BeforeGet(hooks ...gin.HandlerFunc) ICurd[T] {
c.beforeGet = append(c.beforeGet, hooks...)
return c
}
// BeforeGetList registers a hook function to be called before the getting list action
func (c *Curd[T]) BeforeGetList(hooks ...gin.HandlerFunc) ICurd[T] {
c.beforeGetList = append(c.beforeGetList, hooks...)
return c
}
// BeforeDestroy registers a hook function to be called before the deleting action
func (c *Curd[T]) BeforeDestroy(hooks ...gin.HandlerFunc) ICurd[T] {
c.beforeDestroy = append(c.beforeDestroy, hooks...)
return c
}
// BeforeRecover registers a hook function to be called before the recovering action
func (c *Curd[T]) BeforeRecover(hooks ...gin.HandlerFunc) ICurd[T] {
c.beforeRecover = append(c.beforeRecover, hooks...)
return c
}
// GetHook registers a hook function to the queen, and it will be called before the get action
func (c *Curd[T]) GetHook(hook ...func(*Ctx[T])) {
c.getHook = append(c.getHook, hook...)
}
// GetListHook registers a hook function to the queen, and it will be called before the get list action
func (c *Curd[T]) GetListHook(hook ...func(*Ctx[T])) {
c.getListHook = append(c.getListHook, hook...)
}
// CreateHook registers a hook function to the queen, and it will be called before the 'create' action
func (c *Curd[T]) CreateHook(hook ...func(*Ctx[T])) {
c.createHook = append(c.createHook, hook...)
}
// ModifyHook registers a hook function to the queen, and it will be called before the modify action
func (c *Curd[T]) ModifyHook(hook ...func(*Ctx[T])) {
c.modifyHook = append(c.modifyHook, hook...)
}
// DestroyHook registers a hook function to the queen, and it will be called before the delete action
func (c *Curd[T]) DestroyHook(hook ...func(*Ctx[T])) {
c.destroyHook = append(c.destroyHook, hook...)
}
// RecoverHook registers a hook function to the queen, and it will be called before the recover action
func (c *Curd[T]) RecoverHook(hook ...func(*Ctx[T])) {
c.recoverHook = append(c.recoverHook, hook...)
}
// InitRouter registers the CRUD routes to the gin router
func (c *Curd[T]) InitRouter(r *gin.RouterGroup, middleware ...gin.HandlerFunc) {
g := r.Group(c.baseUrl, middleware...)
{
g.GET("/:id", c.Get()...)
g.GET("", c.GetList()...)
g.POST("", c.Create()...)
g.POST("/:id", c.Modify()...)
g.DELETE("/:id", c.Destroy()...)
g.PATCH("/:id", c.Recover()...)
}
}
// Get returns a gin.HandlerFunc that handles get item requests
func (c *Curd[T]) Get() (h []gin.HandlerFunc) {
if len(c.beforeGet) > 0 {
h = append(h, c.beforeGet...)
}
var hook = getHook[T]()
h = append(h, func(ginCtx *gin.Context) {
core := Core[T](ginCtx)
hook(core)
if c.getHook != nil {
for _, v := range c.getHook {
v(core)
}
}
core.Get()
})
return
}
// GetList returns a gin.HandlerFunc that handles get items list requests
func (c *Curd[T]) GetList() (h []gin.HandlerFunc) {
if len(c.beforeGetList) > 0 {
h = append(h, c.beforeGetList...)
}
var hook = getListHook[T]()
h = append(h, func(ginCtx *gin.Context) {
core := Core[T](ginCtx)
hook(core)
if c.getListHook != nil {
for _, v := range c.getListHook {
v(core)
}
}
core.PagingList()
})
return
}
// Create returns a gin.HandlerFunc that handles create item requests
func (c *Curd[T]) Create() (h []gin.HandlerFunc) {
resolved := model.GetResolvedModel[T]()
h = append(h, c.beforeCreate...)
h = append(h, func(ginCtx *gin.Context) {
core := Core[T](ginCtx)
validMap := make(gin.H)
for _, field := range resolved.Fields {
dirs := field.CosyTag.GetAdd()
if dirs == "" {
continue
}
key := field.JsonTag
// like password field we don't need to response it to client,
// but we need to validate it
if key == "-" {
if field.CosyTag.GetJson() != "" {
key = field.CosyTag.GetJson()
} else {
continue
}
}
validMap[key] = dirs
if field.Unique {
core.SetUnique(key)
}
}
core.SetValidRules(validMap)
if c.createHook != nil {
for _, v := range c.createHook {
v(core)
}
}
core.Create()
})
return
}
// Modify returns a gin.HandlerFunc that handles modify item requests
func (c *Curd[T]) Modify() (h []gin.HandlerFunc) {
resolved := model.GetResolvedModel[T]()
h = append(h, c.beforeModify...)
h = append(h, func(ginCtx *gin.Context) {
core := Core[T](ginCtx)
validMap := make(gin.H)
for _, field := range resolved.Fields {
dirs := field.CosyTag.GetUpdate()
if dirs == "" {
continue
}
key := field.JsonTag
// like password field, we don't need to response it to the client,
// but we need to validate it
if key == "-" {
if field.CosyTag.GetJson() != "" {
key = field.CosyTag.GetJson()
} else {
continue
}
}
validMap[key] = dirs
if field.Unique {
core.SetUnique(key)
}
}
core.SetValidRules(validMap)
if c.modifyHook != nil {
for _, v := range c.modifyHook {
v(core)
}
}
core.Modify()
})
return
}
// Destroy returns a gin.HandlerFunc that handles delete item requests
func (c *Curd[T]) Destroy() (h []gin.HandlerFunc) {
h = append(h, c.beforeDestroy...)
h = append(h, func(ginCtx *gin.Context) {
core := Core[T](ginCtx)
if c.destroyHook != nil {
for _, v := range c.destroyHook {
v(core)
}
}
core.Destroy()
})
return
}
// Recover returns a gin.HandlerFunc that handles recover item requests
func (c *Curd[T]) Recover() (h []gin.HandlerFunc) {
h = append(h, c.beforeRecover...)
h = append(h, func(ginCtx *gin.Context) {
core := Core[T](ginCtx)
if c.recoverHook != nil {
for _, v := range c.recoverHook {
v(core)
}
}
core.Recover()
})
return
}