Esempio n. 1
0
func EntityPost(post *entities.Put) server.Handler {
	return server.Handler(
		func(req *wcg.Request) response.Response {
			ent, err := post.Kind().CreateEntityFromForm(req.HTTPRequest().Form)
			if err != nil { // ParseError
				req.Logger.Warnf("You should use ParseForm middleware to avoid the error in EntityPost: %v", err)
				return response.BadRequest(req, ErrInvalidFormParameters)
			}
			_, ent_ := post.MustUpdate(req, ent)
			return response.NewJSONResponseWithStatus(ent_, 201)
		})
}
Esempio n. 2
0
func processEntityPut(req *wcg.Request, put *entities.Put, keyParams string, create bool) response.Response {
	key := req.Param(keyParams)
	_, ent := put.Kind().Get().Key(key).UseDefaultIfNil(create).MustOne(req)
	if ent == nil {
		return response.APINotFound
	}
	if req.HTTPRequest().Form == nil {
		req.Logger.Warnf("You should use ParseForm middleware to avoid the error in EntityPut|EntityPutOrCreate")
		return response.InternalServerError(req, ErrInvalidFormParameters).(response.Response)
	}
	err := put.Kind().UpdateEntityFromForm(ent, req.HTTPRequest().Form)
	if err != nil {
		return response.BadRequest(req, ErrInvalidFormParameters)
	}
	_, ent_ := put.Key(key).MustUpdate(req, ent)
	return response.NewJSONResponseWithStatus(ent_, 200)
}