1
0
mirror of https://github.com/beego/bee.git synced 2024-11-26 06:21:34 +00:00

Merge pull request #142 from FarYoung/patch-2

add post and put json invalid fomart filter
This commit is contained in:
astaxie 2015-11-26 08:09:47 +08:00
commit 583f7f0969

View File

@ -1172,10 +1172,13 @@ func (c *{{ctrlName}}Controller) URLMapping() {
// @router / [post] // @router / [post]
func (c *{{ctrlName}}Controller) Post() { func (c *{{ctrlName}}Controller) Post() {
var v models.{{ctrlName}} var v models.{{ctrlName}}
json.Unmarshal(c.Ctx.Input.RequestBody, &v) if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
if _, err := models.Add{{ctrlName}}(&v); err == nil { if _, err := models.Add{{ctrlName}}(&v); err == nil {
c.Ctx.Output.SetStatus(201) c.Ctx.Output.SetStatus(201)
c.Data["json"] = v c.Data["json"] = v
} else {
c.Data["json"] = err.Error()
}
} else { } else {
c.Data["json"] = err.Error() c.Data["json"] = err.Error()
} }
@ -1273,9 +1276,12 @@ func (c *{{ctrlName}}Controller) Put() {
idStr := c.Ctx.Input.Params[":id"] idStr := c.Ctx.Input.Params[":id"]
id, _ := strconv.Atoi(idStr) id, _ := strconv.Atoi(idStr)
v := models.{{ctrlName}}{Id: id} v := models.{{ctrlName}}{Id: id}
json.Unmarshal(c.Ctx.Input.RequestBody, &v) if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
if err := models.Update{{ctrlName}}ById(&v); err == nil { if err := models.Update{{ctrlName}}ById(&v); err == nil {
c.Data["json"] = "OK" c.Data["json"] = "OK"
} else {
c.Data["json"] = err.Error()
}
} else { } else {
c.Data["json"] = err.Error() c.Data["json"] = err.Error()
} }