/
classes.go
304 lines (266 loc) · 7.45 KB
/
classes.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package controllers
import (
"encoding/json"
"errors"
"strconv"
"github.com/lfq7413/tomato/errs"
"github.com/lfq7413/tomato/rest"
"github.com/lfq7413/tomato/types"
"github.com/lfq7413/tomato/utils"
)
// ClassesController 对象操作 API 的基础结构
// 处理 /classes 接口的所有请求,处理内部类的部分请求
// ClassName 要操作的类名
// ObjectID 要操作的对象 id
type ClassesController struct {
BaseController
ClassName string
ObjectID string
}
// HandleCreate 处理对象创建请求,返回对象 id 与对象位置
// @router /:className [post]
func (c *ClassesController) HandleCreate() {
if c.ClassName == "" {
c.ClassName = c.Ctx.Input.Param(":className")
}
if c.JSONBody == nil {
c.HandleError(errs.E(errs.InvalidJSON, "request body is empty"), 0)
return
}
result, err := rest.Create(c.Auth, c.ClassName, c.JSONBody, c.Info.ClientSDK)
if err != nil {
c.HandleError(err, 0)
return
}
status := 201
if i, ok := result["status"].(int); ok {
status = i
}
c.Data["json"] = result["response"]
c.Ctx.Output.SetStatus(status)
if status == 201 {
c.Ctx.Output.Header("Location", utils.S(result["location"]))
}
c.ServeJSON()
}
// HandleGet 处理查询指定对象请求,返回查询到的对象
// @router /:className/:objectId [get]
func (c *ClassesController) HandleGet() {
if c.ClassName == "" {
c.ClassName = c.Ctx.Input.Param(":className")
}
if c.ObjectID == "" {
c.ObjectID = c.Ctx.Input.Param(":objectId")
}
allowedGetQueryKeys := map[string]bool{
"keys": true,
"include": true,
}
for k := range c.Query {
if allowedGetQueryKeys[k] == false {
c.HandleError(errs.E(errs.InvalidQuery, "Invalid parameter for query: "+k), 0)
return
}
}
for k := range c.JSONBody {
if allowedGetQueryKeys[k] == false {
c.HandleError(errs.E(errs.InvalidQuery, "Invalid parameter for query: "+k), 0)
return
}
}
options := types.M{}
if c.Query["keys"] != "" {
options["keys"] = c.Query["keys"]
} else if c.JSONBody["keys"] != "" {
options["keys"] = c.JSONBody["keys"]
}
if c.Query["include"] != "" {
options["include"] = c.Query["include"]
} else if c.JSONBody["include"] != "" {
options["include"] = c.JSONBody["include"]
}
response, err := rest.Get(c.Auth, c.ClassName, c.ObjectID, options, c.Info.ClientSDK)
if err != nil {
c.HandleError(err, 0)
return
}
results := utils.A(response["results"])
if results == nil || len(results) == 0 {
c.HandleError(errs.E(errs.ObjectNotFound, "Object not found."), 0)
return
}
result := utils.M(results[0])
if c.ClassName == "_User" {
delete(result, "sessionToken")
if c.Auth.User != nil && utils.S(result["objectId"]) == utils.S(c.Auth.User["objectId"]) {
// 重新设置 session token
result["sessionToken"] = c.Info.SessionToken
}
}
c.Data["json"] = result
c.ServeJSON()
}
// HandleUpdate 处理更新指定对象请求
// @router /:className/:objectId [put]
func (c *ClassesController) HandleUpdate() {
if c.ClassName == "" {
c.ClassName = c.Ctx.Input.Param(":className")
}
if c.ObjectID == "" {
c.ObjectID = c.Ctx.Input.Param(":objectId")
}
if c.JSONBody == nil {
c.HandleError(errs.E(errs.InvalidJSON, "request body is empty"), 0)
return
}
result, err := rest.Update(c.Auth, c.ClassName, c.ObjectID, c.JSONBody, c.Info.ClientSDK)
if err != nil {
c.HandleError(err, 0)
return
}
c.Data["json"] = result["response"]
c.ServeJSON()
}
// HandleFind 处理查找对象请求
// @router /:className [get]
func (c *ClassesController) HandleFind() {
if c.ClassName == "" {
c.ClassName = c.Ctx.Input.Param(":className")
}
allowConstraints := map[string]bool{
"skip": true,
"limit": true,
"order": true,
"count": true,
"keys": true,
"include": true,
"redirectClassNameForKey": true,
"where": true,
}
for k := range c.Query {
if allowConstraints[k] == false {
c.HandleError(errs.E(errs.InvalidQuery, "Invalid parameter for query: "+k), 0)
return
}
}
for k := range c.JSONBody {
if allowConstraints[k] == false {
c.HandleError(errs.E(errs.InvalidQuery, "Invalid parameter for query: "+k), 0)
return
}
}
// 获取查询参数,并组装
options := types.M{}
if c.Query["skip"] != "" {
if i, err := strconv.Atoi(c.Query["skip"]); err == nil {
options["skip"] = i
}
} else if c.JSONBody != nil && c.JSONBody["skip"] != nil {
if i, ok := c.JSONBody["skip"].(float64); ok {
options["skip"] = int(i)
}
}
if c.Query["limit"] != "" {
if i, err := strconv.Atoi(c.Query["limit"]); err == nil {
options["limit"] = i
} else {
options["limit"] = 100
}
} else if c.JSONBody != nil && c.JSONBody["limit"] != nil {
if i, ok := c.JSONBody["limit"].(float64); ok {
options["limit"] = int(i)
} else {
options["limit"] = 100
}
} else {
options["limit"] = 100
}
if c.Query["order"] != "" {
options["order"] = c.Query["order"]
} else if c.JSONBody != nil && c.JSONBody["order"] != nil {
options["order"] = c.JSONBody["order"]
}
if c.Query["count"] != "" {
options["count"] = true
} else if c.JSONBody != nil && c.JSONBody["count"] != nil {
options["count"] = true
}
if c.Query["keys"] != "" {
options["keys"] = c.Query["keys"]
} else if c.JSONBody != nil && c.JSONBody["keys"] != nil {
options["keys"] = c.JSONBody["keys"]
}
if c.Query["include"] != "" {
options["include"] = c.Query["include"]
} else if c.JSONBody != nil && c.JSONBody["include"] != nil {
options["include"] = c.JSONBody["include"]
}
if c.Query["redirectClassNameForKey"] != "" {
options["redirectClassNameForKey"] = c.Query["redirectClassNameForKey"]
} else if c.JSONBody != nil && c.JSONBody["redirectClassNameForKey"] != nil {
options["redirectClassNameForKey"] = c.JSONBody["redirectClassNameForKey"]
}
where := types.M{}
if c.Query["where"] != "" {
err := json.Unmarshal([]byte(c.Query["where"]), &where)
if err != nil {
c.HandleError(errs.E(errs.InvalidJSON, "where should be valid json"), 0)
return
}
} else if c.JSONBody != nil && c.JSONBody["where"] != nil {
where = utils.M(c.JSONBody["where"])
}
response, err := rest.Find(c.Auth, c.ClassName, where, options, c.Info.ClientSDK)
if err != nil {
c.HandleError(err, 0)
return
}
if utils.HasResults(response) {
results := utils.A(response["results"])
for _, v := range results {
result := utils.M(v)
if result["sessionToken"] != nil && c.Info.SessionToken != "" {
result["sessionToken"] = c.Info.SessionToken
}
}
}
c.Data["json"] = response
c.ServeJSON()
}
// HandleDelete 处理删除指定对象请求
// @router /:className/:objectId [delete]
func (c *ClassesController) HandleDelete() {
if c.ClassName == "" {
c.ClassName = c.Ctx.Input.Param(":className")
}
if c.ObjectID == "" {
c.ObjectID = c.Ctx.Input.Param(":objectId")
}
err := rest.Delete(c.Auth, c.ClassName, c.ObjectID)
if err != nil {
c.HandleError(err, 0)
return
}
c.Data["json"] = types.M{}
c.ServeJSON()
}
// Get ...
// @router / [get]
func (c *ClassesController) Get() {
c.HandleError(errors.New("Method Not Allowed"), 405)
}
// Post ...
// @router / [post]
func (c *ClassesController) Post() {
c.HandleError(errors.New("Method Not Allowed"), 405)
}
// Delete ...
// @router / [delete]
func (c *ClassesController) Delete() {
c.HandleError(errors.New("Method Not Allowed"), 405)
}
// Put ...
// @router / [put]
func (c *ClassesController) Put() {
c.HandleError(errors.New("Method Not Allowed"), 405)
}