-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.go
190 lines (168 loc) · 5.08 KB
/
resource.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
package lightweight_api
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/ssst0n3/awesome_libs/awesome_reflect"
"github.com/ssst0n3/lightweight_api/db"
"github.com/ssst0n3/lightweight_api/response"
"net/http"
"reflect"
"strconv"
)
type Resource struct {
Name string
TableName string
BaseRelativePath string
Model interface{} // model cannot be reused
GuidFieldJsonTag string
}
func NewResource(resourceName string, tableName string, model interface{}, guidFiledJsonTag string) Resource {
awesome_reflect.MustNotPointer(model)
return Resource{
Name: resourceName,
TableName: tableName,
BaseRelativePath: BaseRelativePathV1(resourceName),
Model: model,
GuidFieldJsonTag: guidFiledJsonTag,
}
}
func BaseRelativePath(apiVersion, resourceName string) string {
return fmt.Sprintf("/api/%s/%s", apiVersion, resourceName)
}
func BaseRelativePathV1(resourceName string) string {
return BaseRelativePath("v1", resourceName)
}
func (r *Resource) CountResource(c *gin.Context) {
var count int64
model := awesome_reflect.EmptyPointerOfModel(r.Model)
DB.Model(model).Count(&count)
c.String(http.StatusOK, strconv.Itoa(int(count)))
}
func (r *Resource) ListResource(c *gin.Context) {
model := awesome_reflect.EmptyPointerOfModel(r.Model)
sliceModel := awesome_reflect.EmptyPointerOfSliceModel(r.Model)
DB.Table(r.TableName).Model(model).Find(sliceModel)
c.JSON(http.StatusOK, sliceModel)
}
func (r *Resource) MapResourceById(c *gin.Context) {
var list []map[string]interface{}
model := awesome_reflect.EmptyPointerOfModel(r.Model)
DB.Model(model).Find(&list)
objects := db.MapObjectById(list)
c.JSON(http.StatusOK, objects)
}
func (r *Resource) CreateResourceTemplate(c *gin.Context, taskBeforeCreateObject func(modelPtr interface{}) error, taskAfterCreateObject func(id uint) error) {
awesome_reflect.MustNotPointer(r.Model)
modelPtr := awesome_reflect.EmptyPointerOfModel(r.Model)
if err := c.ShouldBindJSON(modelPtr); err != nil {
HandleStatusBadRequestError(c, err)
return
}
if len(r.GuidFieldJsonTag) > 0 {
if err := r.MustResourceNotExistsByModelPtrWithGuid(c, modelPtr, r.GuidFieldJsonTag); err != nil {
return
}
}
if taskBeforeCreateObject != nil {
if err := taskBeforeCreateObject(modelPtr); err != nil {
HandleInternalServerError(c, err)
return
}
}
DB.Create(modelPtr)
id := awesome_reflect.ValueByPtr(modelPtr).FieldByName("ID").Interface().(uint)
if taskAfterCreateObject != nil {
if err := taskAfterCreateObject(id); err != nil {
HandleInternalServerError(c, err)
return
}
}
var msg string
if len(r.GuidFieldJsonTag) > 0 {
guidValue, _ := awesome_reflect.FieldByJsonTag(reflect.ValueOf(modelPtr).Elem(), r.GuidFieldJsonTag)
msg = fmt.Sprintf(MsgResourceCreateSuccess, r.Name, guidValue)
} else {
msg = fmt.Sprintf(MsgResourceCreateSuccess, r.Name, id)
}
if !c.IsAborted() {
response.CreateSuccess200(c, id, msg)
}
}
func (r *Resource) CreateResource(c *gin.Context) {
r.CreateResourceTemplate(c, nil, nil)
}
func (r *Resource) DeleteResource(c *gin.Context) {
r.DeleteResourceTemplate(c, false)
}
func (r *Resource) DeleteResourceTemplate(c *gin.Context, softDelete bool) {
id, err := r.MustResourceExistsByIdAutoParseParam(c)
if err != nil {
return
}
model := awesome_reflect.EmptyPointerOfModel(r.Model)
if softDelete {
DB.Delete(model, id) // soft delete
} else {
DB.Unscoped().Delete(model, id)
}
response.DeleteSuccess200(c)
}
func (r *Resource) UpdateResourceTemplate(c *gin.Context, model interface{}, taskBeforeCreateObject func(modelPtr interface{}) error) {
if model == nil {
model = r.Model
}
awesome_reflect.MustNotPointer(model)
modelPtr := awesome_reflect.EmptyPointerOfModel(model)
id, err := r.MustResourceExistsByIdAutoParseParam(c)
if err != nil {
return
}
if err := c.ShouldBindJSON(modelPtr); err != nil {
HandleStatusBadRequestError(c, err)
return
}
if len(r.GuidFieldJsonTag) > 0 {
if err := r.MustResourceNotExistsExceptSelfByModelPtrWithGuid(c, modelPtr, r.GuidFieldJsonTag, uint(id)); err != nil {
HandleInternalServerError(c, err)
return
}
}
if taskBeforeCreateObject != nil {
if err := taskBeforeCreateObject(modelPtr); err != nil {
HandleInternalServerError(c, err)
return
}
}
awesome_reflect.ValueByPtr(modelPtr).FieldByName("ID").SetUint(uint64(id))
result := DB.Updates(modelPtr)
if result.Error != nil {
HandleInternalServerError(c, err)
return
} else {
response.UpdateSuccess200(c)
return
}
//if err := Conn.UpdateObject(id, r.TableName, modelPtr); err != nil {
// HandleInternalServerError(c, err)
// return
//} else {
// response.UpdateSuccess200(c)
// return
//}
}
func (r *Resource) UpdateResource(c *gin.Context) {
r.UpdateResourceTemplate(c, nil, nil)
}
func (r *Resource) ShowResource(c *gin.Context) {
id, err := r.MustResourceExistsByIdAutoParseParam(c)
if err != nil {
return
}
model := awesome_reflect.EmptyPointerOfModel(r.Model)
result := DB.Table(r.TableName).First(model, id)
if result.Error != nil {
HandleInternalServerError(c, err)
return
}
c.JSON(http.StatusOK, model)
}