1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-28 10:35:46 +00:00
Beego/example/beeapi/controllers/default.go

57 lines
1.2 KiB
Go
Raw Normal View History

2013-07-09 05:59:47 +00:00
package controllers
import (
"encoding/json"
"github.com/astaxie/beego"
"github.com/astaxie/beego/example/beeapi/models"
)
type ObjectController struct {
2013-07-09 05:59:47 +00:00
beego.Controller
}
func (this *ObjectController) Post() {
2013-07-09 05:59:47 +00:00
var ob models.Object
2013-09-10 07:08:32 +00:00
json.Unmarshal(this.Ctx.Input.RequestBody, &ob)
2013-07-09 05:59:47 +00:00
objectid := models.AddOne(ob)
2013-08-19 02:26:10 +00:00
this.Data["json"] = map[string]string{"ObjectId": objectid}
2013-07-09 05:59:47 +00:00
this.ServeJson()
}
func (this *ObjectController) Get() {
2013-12-05 02:29:04 +00:00
objectId := this.Ctx.Input.Params[":objectId"]
2013-07-09 05:59:47 +00:00
if objectId != "" {
ob, err := models.GetOne(objectId)
if err != nil {
this.Data["json"] = err
} else {
this.Data["json"] = ob
}
} else {
obs := models.GetAll()
this.Data["json"] = obs
}
this.ServeJson()
}
func (this *ObjectController) Put() {
2013-12-05 02:29:04 +00:00
objectId := this.Ctx.Input.Params[":objectId"]
2013-07-09 05:59:47 +00:00
var ob models.Object
2013-09-10 07:08:32 +00:00
json.Unmarshal(this.Ctx.Input.RequestBody, &ob)
2013-07-09 05:59:47 +00:00
err := models.Update(objectId, ob.Score)
if err != nil {
this.Data["json"] = err
} else {
this.Data["json"] = "update success!"
}
this.ServeJson()
}
func (this *ObjectController) Delete() {
2013-12-05 02:29:04 +00:00
objectId := this.Ctx.Input.Params[":objectId"]
2013-07-09 05:59:47 +00:00
models.Delete(objectId)
this.Data["json"] = "delete success!"
this.ServeJson()
}