-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.go
243 lines (211 loc) · 5.92 KB
/
crud.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
// Copyright 2021 Kévin José. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package easyapi
import (
"fmt"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"gitlab.com/kjose/jgmc/api/internal/easyapi/db/dao"
"gitlab.com/kjose/jgmc/api/internal/easyapi/event"
"gitlab.com/kjose/jgmc/api/internal/easyapi/layer"
"gitlab.com/kjose/jgmc/api/internal/easyapi/utils"
)
// Gin handler for a POST request
func HandlePost(c *gin.Context, i interface{}) {
ic := utils.CloneInterface(i) // avoid duplicate variable use
if err := BindAndValidate(c, ic); err != nil {
return
}
err := event.DispatchEvent(c, event.EVENT_RESOURCE_PRE_CREATE, &event.ResourceActionEvent{
Resource: ic,
Action: event.EVENT_RESOURCE_PRE_CREATE,
})
if err != nil {
return
}
RemoveUUIDBindings(ic)
_, err = dao.GetResourceDAO(ic).Create(ic)
if err != nil {
HttpError(c, http.StatusBadRequest, "Creation error", nil)
return
}
err = event.DispatchEvent(c, event.EVENT_RESOURCE_POST_CREATE, &event.ResourceActionEvent{
Resource: ic,
Action: event.EVENT_RESOURCE_POST_CREATE,
})
if err != nil {
return
}
c.JSON(http.StatusCreated, NewItem(ic, &layer.SerializeGroups{
Values: []string{SERIALIZER_CONTEXT_KEY_ONE},
}))
}
// Gin handler for a GET request
func HandleGet(c *gin.Context, i interface{}, id string) {
ic := utils.CloneInterface(i) // avoid duplicate variable use
_, err := dao.GetResourceDAO(ic).FindById(ic, id)
if err != nil {
HttpError(c, http.StatusNotFound, "Not found", nil)
return
}
if err := AppendBindings(ic); err != nil {
return
}
err = event.DispatchEvent(c, event.EVENT_RESOURCE_POST_READ, &event.ResourceActionEvent{
Resource: ic,
Action: event.EVENT_RESOURCE_POST_READ,
})
if err != nil {
return
}
c.JSON(http.StatusOK, NewItem(ic, &layer.SerializeGroups{
Values: []string{SERIALIZER_CONTEXT_KEY_ONE},
}))
}
// Gin handler for a LIST request
func HandleList(c *gin.Context, i interface{}) {
ic := utils.CloneInterface(i) // avoid duplicate variable use
// Pagination
var pf *dao.PaginationFilter
var pQueryName string
var pc layer.PaginationConfig
if ipa, ok := i.(layer.PaginationAware); ok {
pc = ipa.GetPaginationConfig()
pQueryName = pc.QueryParamName
pf = pc.GetPaginationFilterFromContext(c)
}
// Check filters from
var ff []dao.FilterFunc
if iqfa, ok := ic.(layer.QueryFilterAware); ok {
for key, val := range c.Request.URL.Query() {
if key == pQueryName {
continue
}
qf := iqfa.GetQueryFilterSet().GetByParam(key)
if qf == nil {
HttpError(c, http.StatusNotFound, fmt.Sprintf("Param %s is not a filter", key), nil)
return
}
ff = append(ff, qf.Func(key, val[0], qf.Args))
}
// Apply defaults
for _, f := range iqfa.GetQueryFilterSet() {
if f.DefaultValue == "" || c.Request.URL.Query().Get(f.UrlParam) != "" {
continue
}
ff = append(ff, f.Func(f.UrlParam, f.DefaultValue, f.Args))
}
}
r, err := dao.GetResourceDAO(ic).FindByFilter(ic, ff, pf)
if err != nil {
HttpError(c, http.StatusNotFound, "Get collection request error", nil)
return
}
all := r.All()
for _, l := range all {
err = event.DispatchEvent(c, event.EVENT_RESOURCE_POST_READ, &event.ResourceActionEvent{
Resource: l,
Action: event.EVENT_RESOURCE_POST_READ,
})
if err != nil {
return
}
}
collectionItems := NewCollectionItem(all, &layer.SerializeGroups{
Values: []string{SERIALIZER_CONTEXT_KEY_LIST},
})
collectionItems.Count = len(all)
collectionItems.Total = r.CountTotal()
collectionItems.Links = pc.GetLinksFromContext(c, collectionItems.Total)
c.JSON(http.StatusOK, collectionItems)
}
// Gin handler for a PATCH request
func HandlePatch(c *gin.Context, i interface{}, id string) {
ic := utils.CloneInterface(i) // avoid duplicate variable use
_, err := dao.GetResourceDAO(ic).FindById(ic, id)
clone := utils.CloneInterface(ic)
if err != nil {
HttpError(c, http.StatusNotFound, "Not found", nil)
return
}
if err := BindAndValidate(c, ic); err != nil {
return
}
err = event.DispatchEvent(c, event.EVENT_RESOURCE_PRE_UPDATE, &event.ResourceActionEvent{
Resource: ic,
Action: event.EVENT_RESOURCE_PRE_UPDATE,
})
if err != nil {
return
}
RemoveUUIDBindings(ic)
_, err = dao.GetResourceDAO(ic).UpdateFromPrevious(clone, ic)
if err != nil {
HttpError(c, http.StatusBadRequest, "Update error", nil)
return
}
err = event.DispatchEvent(c, event.EVENT_RESOURCE_POST_UPDATE, &event.ResourceActionEvent{
Resource: ic,
Action: event.EVENT_RESOURCE_POST_UPDATE,
})
if err != nil {
return
}
}
// Gin handler for a DELETE request
func HandleDelete(c *gin.Context, i interface{}, id string) {
ic := utils.CloneInterface(i)
_, err := dao.GetResourceDAO(ic).FindById(ic, id)
if err != nil {
HttpError(c, http.StatusNotFound, "Not found", nil)
return
}
if err := AppendBindings(ic); err != nil {
return
}
err = event.DispatchEvent(c, event.EVENT_RESOURCE_PRE_DELETE, &event.ResourceActionEvent{
Resource: ic,
Action: event.EVENT_RESOURCE_PRE_DELETE,
})
if err != nil {
return
}
err = dao.GetResourceDAO(ic).DeleteById(ic, id)
if err != nil {
HttpError(c, http.StatusBadRequest, "Delete error", nil)
return
}
}
// Shortcut to handle multiple crud requests
func CRUDL(r gin.IRoutes, path string, i interface{}, methods string) {
if methods == "" {
methods = "CRUDL"
}
if strings.Contains(methods, "C") {
r.POST(path, func(c *gin.Context) {
HandlePost(c, i)
})
}
if strings.Contains(methods, "R") {
r.GET(path+"/:id", func(c *gin.Context) {
HandleGet(c, i, c.Param("id"))
})
}
if strings.Contains(methods, "U") {
r.PATCH(path+"/:id", func(c *gin.Context) {
HandlePatch(c, i, c.Param("id"))
})
}
if strings.Contains(methods, "D") {
r.DELETE(path+"/:id", func(c *gin.Context) {
HandleDelete(c, i, c.Param("id"))
})
}
if strings.Contains(methods, "L") {
r.GET(path, func(c *gin.Context) {
HandleList(c, i)
})
}
}