diff --git a/example/beeapi/conf/app.conf b/example/beeapi/conf/app.conf new file mode 100644 index 00000000..fd1681a2 --- /dev/null +++ b/example/beeapi/conf/app.conf @@ -0,0 +1,5 @@ +appname = beeapi +httpport = 8080 +runmode = dev +autorender = false +copyrequestbody = true diff --git a/example/beeapi/controllers/default.go b/example/beeapi/controllers/default.go new file mode 100644 index 00000000..cf3075e5 --- /dev/null +++ b/example/beeapi/controllers/default.go @@ -0,0 +1,59 @@ +package controllers + +import ( + "encoding/json" + "github.com/astaxie/beego" + "github.com/astaxie/beego/example/beeapi/models" +) + +type ResponseInfo struct { +} + +type ObejctController struct { + beego.Controller +} + +func (this *ObejctController) Post() { + var ob models.Object + json.Unmarshal(this.Ctx.RequestBody, &ob) + objectid := models.AddOne(ob) + this.Data["json"] = "{\"ObjectId\":\"" + objectid + "\"}" + this.ServeJson() +} + +func (this *ObejctController) Get() { + objectId := this.Ctx.Params[":objectId"] + 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 *ObejctController) Put() { + objectId := this.Ctx.Params[":objectId"] + var ob models.Object + json.Unmarshal(this.Ctx.RequestBody, &ob) + + err := models.Update(objectId, ob.Score) + if err != nil { + this.Data["json"] = err + } else { + this.Data["json"] = "update success!" + } + this.ServeJson() +} + +func (this *ObejctController) Delete() { + objectId := this.Ctx.Params[":objectId"] + models.Delete(objectId) + this.Data["json"] = "delete success!" + this.ServeJson() +} diff --git a/example/beeapi/main.go b/example/beeapi/main.go new file mode 100644 index 00000000..a65dfadc --- /dev/null +++ b/example/beeapi/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "github.com/astaxie/beego" + "github.com/astaxie/beego/example/beeapi/controllers" +) + +// Objects + +// URL HTTP Verb Functionality +// /object POST Creating Objects +// /object/ GET Retrieving Objects +// /object/ PUT Updating Objects +// /object GET Queries +// /object/ DELETE Deleting Objects + +func main() { + beego.RESTRouter("/object", &controllers.ObejctController{}) + beego.Run() +} diff --git a/example/beeapi/models/object.go b/example/beeapi/models/object.go new file mode 100644 index 00000000..2c72e6b6 --- /dev/null +++ b/example/beeapi/models/object.go @@ -0,0 +1,52 @@ +package models + +import ( + "errors" + "strconv" + "time" +) + +var ( + Objects map[string]*Object +) + +type Object struct { + ObjectId string + Score int64 + PlayerName string +} + +func init() { + Objects = make(map[string]*Object) + Objects["hjkhsbnmn123"] = &Object{"hjkhsbnmn123", 100, "astaxie"} + Objects["mjjkxsxsaa23"] = &Object{"mjjkxsxsaa23", 101, "someone"} +} + +func AddOne(object Object) (ObjectId string) { + object.ObjectId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10) + Objects[object.ObjectId] = &object + return object.ObjectId +} + +func GetOne(ObjectId string) (object *Object, err error) { + if v, ok := Objects[ObjectId]; ok { + return v, nil + } + return nil, errors.New("ObjectId Not Exist") +} + +func GetAll() map[string]*Object { + return Objects +} + +func Update(ObjectId string, Score int64) (err error) { + if v, ok := Objects[ObjectId]; ok { + v.Score = Score + return nil + } + return errors.New("ObjectId Not Exist") +} + +func Delete(ObjectId string) { + delete(Objects, ObjectId) +}