forked from BonneVoyager/basicserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_post.go
58 lines (52 loc) · 1.42 KB
/
data_post.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
package basicserver
import (
"time"
"github.com/globalsign/mgo/bson"
"github.com/kataras/iris/v12"
)
// ServeDataPost serves
// Method: POST
// Resource: http://localhost/api/data
//
// This resource requires `Authorization` header, e.g.:
//
// Content-Type: application/json
// Authorization: Bearer {token}
//
// Sample request to be `POST`ed to the /api/data resource as `application/json`:
//
// {
// "foo": "bar",
// "bar": "foo"
// }
//
// If everything goes well, then this will return status code `200` and no response body.
//
// In case of error, this will return status code `400` or `500` and `text/plain` error
// message (e.g. "Incorrect Credentials") as a response.
//
// In case of invalid/expired token, this will return status code `401` and `text/plain`
// error message as a response.
//
func (app *BasicApp) ServeDataPost() iris.Handler {
return func(ctx iris.Context) {
var input bson.M
err := ctx.ReadJSON(&input)
if err != nil {
app.HandleError(err, ctx, iris.StatusBadRequest)
return
}
uid := ctx.Values().Get("uid").(string)
objectUID := bson.ObjectIdHex(uid)
parsedInput := make(bson.M)
for key, value := range input {
parsedInput["data."+key] = value
}
parsedInput["updated_at"] = time.Now()
_, err = app.Coll.States.UpsertId(objectUID, bson.M{"$set": parsedInput})
if err != nil {
app.HandleError(err, ctx, iris.StatusInternalServerError)
return
}
}
}