Repository navigation
Expand file tree
/
Copy pathapi.go
More file actions
276 lines (246 loc) · 7.46 KB
/
Copy pathapi.go
File metadata and controls
276 lines (246 loc) · 7.46 KB
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
package cosy
import (
"github.com/gin-gonic/gin"
)
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]))
WithoutCreate() ICurd[T]
WithoutModify() ICurd[T]
WithoutGet() ICurd[T]
WithoutGetList() ICurd[T]
WithoutDestroy() ICurd[T]
WithoutRecover() ICurd[T]
WithReadonly() ICurd[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
getEnabled bool
getListEnabled bool
createEnabled bool
modifyEnabled bool
destroyEnabled bool
recoverEnabled bool
}
// Api returns a new instance of Curd
func Api[T any](baseUrl string) ICurd[T] {
return &Curd[T]{
baseUrl: baseUrl,
getEnabled: true,
getListEnabled: true,
createEnabled: true,
modifyEnabled: true,
destroyEnabled: true,
recoverEnabled: true,
}
}
// 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...)
{
if c.getEnabled {
g.GET("/:id", c.Get()...)
}
if c.getListEnabled {
g.GET("", c.GetList()...)
}
if c.createEnabled {
g.POST("", c.Create()...)
}
if c.modifyEnabled {
g.POST("/:id", c.Modify()...)
}
if c.destroyEnabled {
g.DELETE("/:id", c.Destroy()...)
}
if c.recoverEnabled {
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...)
}
h = append(h, func(ginCtx *gin.Context) {
core := Core[T](ginCtx)
core.PrepareHook(c.getHook...)
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...)
}
h = append(h, func(ginCtx *gin.Context) {
core := Core[T](ginCtx)
core.PrepareHook(c.getListHook...)
core.PagingList()
})
return
}
// Create returns a gin.HandlerFunc that handles create item requests
func (c *Curd[T]) Create() (h []gin.HandlerFunc) {
h = append(h, c.beforeCreate...)
h = append(h, func(ginCtx *gin.Context) {
core := Core[T](ginCtx)
core.PrepareHook(c.createHook...)
core.Create()
})
return
}
// Modify returns a gin.HandlerFunc that handles modify item requests
func (c *Curd[T]) Modify() (h []gin.HandlerFunc) {
h = append(h, c.beforeModify...)
h = append(h, func(ginCtx *gin.Context) {
core := Core[T](ginCtx)
core.PrepareHook(c.modifyHook...)
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)
core.PrepareHook(c.destroyHook...)
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)
core.PrepareHook(c.recoverHook...)
core.Recover()
})
return
}
// WithoutGet disable get item route
func (c *Curd[T]) WithoutGet() ICurd[T] {
c.getEnabled = false
return c
}
// WithoutGetList disable get items list route
func (c *Curd[T]) WithoutGetList() ICurd[T] {
c.getListEnabled = false
return c
}
// WithoutCreate disable create item route
func (c *Curd[T]) WithoutCreate() ICurd[T] {
c.createEnabled = false
return c
}
// WithoutModify disable modify item route
func (c *Curd[T]) WithoutModify() ICurd[T] {
c.modifyEnabled = false
return c
}
// WithoutDestroy disable delete item route
func (c *Curd[T]) WithoutDestroy() ICurd[T] {
c.destroyEnabled = false
return c
}
// WithoutRecover disable recover item route
func (c *Curd[T]) WithoutRecover() ICurd[T] {
c.recoverEnabled = false
return c
}
// WithReadonly disable create, modify, destroy, recover item route
func (c *Curd[T]) WithReadonly() ICurd[T] {
c.createEnabled = false
c.modifyEnabled = false
c.destroyEnabled = false
c.recoverEnabled = false
return c
}