1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-18 00:40:53 +00:00
Beego/example/beeapi/controllers/default.go

64 lines
1.4 KiB
Go
Raw Normal View History

2014-04-12 05:18:18 +00:00
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors astaxie
2013-07-09 05:59:47 +00:00
package controllers
import (
"encoding/json"
2014-04-12 05:18:18 +00:00
2013-07-09 05:59:47 +00:00
"github.com/astaxie/beego"
"github.com/astaxie/beego/example/beeapi/models"
)
type ObjectController struct {
2013-07-09 05:59:47 +00:00
beego.Controller
}
2014-11-05 14:40:31 +00:00
func (o *ObjectController) Post() {
2013-07-09 05:59:47 +00:00
var ob models.Object
2014-11-05 14:40:31 +00:00
json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
2013-07-09 05:59:47 +00:00
objectid := models.AddOne(ob)
2014-11-05 14:40:31 +00:00
o.Data["json"] = map[string]string{"ObjectId": objectid}
o.ServeJson()
2013-07-09 05:59:47 +00:00
}
2014-11-05 14:40:31 +00:00
func (o *ObjectController) Get() {
objectId := o.Ctx.Input.Params[":objectId"]
2013-07-09 05:59:47 +00:00
if objectId != "" {
ob, err := models.GetOne(objectId)
if err != nil {
2014-11-05 14:40:31 +00:00
o.Data["json"] = err
2013-07-09 05:59:47 +00:00
} else {
2014-11-05 14:40:31 +00:00
o.Data["json"] = ob
2013-07-09 05:59:47 +00:00
}
} else {
obs := models.GetAll()
2014-11-05 14:40:31 +00:00
o.Data["json"] = obs
2013-07-09 05:59:47 +00:00
}
2014-11-05 14:40:31 +00:00
o.ServeJson()
2013-07-09 05:59:47 +00:00
}
2014-11-05 14:40:31 +00:00
func (o *ObjectController) Put() {
objectId := o.Ctx.Input.Params[":objectId"]
2013-07-09 05:59:47 +00:00
var ob models.Object
2014-11-05 14:40:31 +00:00
json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
2013-07-09 05:59:47 +00:00
err := models.Update(objectId, ob.Score)
if err != nil {
2014-11-05 14:40:31 +00:00
o.Data["json"] = err
2013-07-09 05:59:47 +00:00
} else {
2014-11-05 14:40:31 +00:00
o.Data["json"] = "update success!"
2013-07-09 05:59:47 +00:00
}
2014-11-05 14:40:31 +00:00
o.ServeJson()
2013-07-09 05:59:47 +00:00
}
2014-11-05 14:40:31 +00:00
func (o *ObjectController) Delete() {
objectId := o.Ctx.Input.Params[":objectId"]
2013-07-09 05:59:47 +00:00
models.Delete(objectId)
2014-11-05 14:40:31 +00:00
o.Data["json"] = "delete success!"
o.ServeJson()
2013-07-09 05:59:47 +00:00
}