1
0
mirror of https://github.com/beego/bee.git synced 2024-11-23 17:00:56 +00:00

bee api to modify half

bee api conn not been modified
This commit is contained in:
MingZong 2016-03-26 23:35:25 +08:00
parent bb75e5773d
commit 6793c7d60d

View File

@ -148,41 +148,36 @@ func init() {
var apiModels = `package models var apiModels = `package models
import ( import (
"{{.Appname}}/structures"
"errors" "errors"
"strconv" "strconv"
"time" "time"
) )
var ( var (
Objects map[string]*Object Objects map[string]*structures.Object
) )
type Object struct {
ObjectId string
Score int64
PlayerName string
}
func init() { func init() {
Objects = make(map[string]*Object) Objects = make(map[string]*structures.Object)
Objects["hjkhsbnmn123"] = &Object{"hjkhsbnmn123", 100, "astaxie"} Objects["hjkhsbnmn123"] = &structures.Object{"hjkhsbnmn123", 100, "astaxie"}
Objects["mjjkxsxsaa23"] = &Object{"mjjkxsxsaa23", 101, "someone"} Objects["mjjkxsxsaa23"] = &structures.Object{"mjjkxsxsaa23", 101, "someone"}
} }
func AddOne(object Object) (ObjectId string) { func AddOne(object structures.Object) (ObjectId string) {
object.ObjectId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10) object.ObjectId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10)
Objects[object.ObjectId] = &object Objects[object.ObjectId] = &object
return object.ObjectId return object.ObjectId
} }
func GetOne(ObjectId string) (object *Object, err error) { func GetOne(ObjectId string) (object *structures.Object, err error) {
if v, ok := Objects[ObjectId]; ok { if v, ok := Objects[ObjectId]; ok {
return v, nil return v, nil
} }
return nil, errors.New("ObjectId Not Exist") return nil, errors.New("ObjectId Not Exist")
} }
func GetAll() map[string]*Object { func GetAll() map[string]*structures.Object {
return Objects return Objects
} }
@ -279,6 +274,7 @@ var apiControllers = `package controllers
import ( import (
"{{.Appname}}/models" "{{.Appname}}/models"
"{{.Appname}}/structures"
"encoding/json" "encoding/json"
"github.com/astaxie/beego" "github.com/astaxie/beego"
@ -296,7 +292,7 @@ type ObjectController struct {
// @Failure 403 body is empty // @Failure 403 body is empty
// @router / [post] // @router / [post]
func (o *ObjectController) Post() { func (o *ObjectController) Post() {
var ob models.Object var ob structures.Object
json.Unmarshal(o.Ctx.Input.RequestBody, &ob) json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
objectid := models.AddOne(ob) objectid := models.AddOne(ob)
o.Data["json"] = map[string]string{"ObjectId": objectid} o.Data["json"] = map[string]string{"ObjectId": objectid}
@ -342,7 +338,7 @@ func (o *ObjectController) GetAll() {
// @router /:objectId [put] // @router /:objectId [put]
func (o *ObjectController) Put() { func (o *ObjectController) Put() {
objectId := o.Ctx.Input.Param(":objectId") objectId := o.Ctx.Input.Param(":objectId")
var ob models.Object var ob structures.Object
json.Unmarshal(o.Ctx.Input.RequestBody, &ob) json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
err := models.Update(objectId, ob.Score) err := models.Update(objectId, ob.Score)
@ -368,10 +364,12 @@ func (o *ObjectController) Delete() {
} }
` `
var apiControllers2 = `package controllers var apiControllers2 = `package controllers
import ( import (
"{{.Appname}}/models" "{{.Appname}}/models"
"{{.Appname}}/structures"
"encoding/json" "encoding/json"
"github.com/astaxie/beego" "github.com/astaxie/beego"
@ -389,7 +387,7 @@ type UserController struct {
// @Failure 403 body is empty // @Failure 403 body is empty
// @router / [post] // @router / [post]
func (u *UserController) Post() { func (u *UserController) Post() {
var user models.User var user structures.User
json.Unmarshal(u.Ctx.Input.RequestBody, &user) json.Unmarshal(u.Ctx.Input.RequestBody, &user)
uid := models.AddUser(user) uid := models.AddUser(user)
u.Data["json"] = map[string]string{"uid": uid} u.Data["json"] = map[string]string{"uid": uid}
@ -435,7 +433,7 @@ func (u *UserController) Get() {
func (u *UserController) Put() { func (u *UserController) Put() {
uid := u.GetString(":uid") uid := u.GetString(":uid")
if uid != "" { if uid != "" {
var user models.User var user structures.User
json.Unmarshal(u.Ctx.Input.RequestBody, &user) json.Unmarshal(u.Ctx.Input.RequestBody, &user)
uu, err := models.UpdateUser(uid, &user) uu, err := models.UpdateUser(uid, &user)
if err != nil { if err != nil {
@ -545,6 +543,16 @@ type Profile struct {
Email string Email string
} }
`
var apistructures2 = `package structures
type Object struct {
ObjectId string
Score int64
PlayerName string
}
` `
func init() { func init() {
cmdApiapp.Run = createapi cmdApiapp.Run = createapi
@ -635,7 +643,8 @@ func createapi(cmd *Command, args []string) int {
strings.Replace(apirouter, "{{.Appname}}", packpath, -1)) strings.Replace(apirouter, "{{.Appname}}", packpath, -1))
fmt.Println("create models object.go:", path.Join(apppath, "models", "object.go")) fmt.Println("create models object.go:", path.Join(apppath, "models", "object.go"))
writetofile(path.Join(apppath, "models", "object.go"), apiModels) writetofile(path.Join(apppath, "models", "object.go"),
strings.Replace(apiModels, "{{.Appname}}", packpath, -1))
fmt.Println("create models user.go:", path.Join(apppath, "models", "user.go")) fmt.Println("create models user.go:", path.Join(apppath, "models", "user.go"))
writetofile(path.Join(apppath, "models", "user.go"), writetofile(path.Join(apppath, "models", "user.go"),
@ -644,6 +653,15 @@ func createapi(cmd *Command, args []string) int {
fmt.Println("create structures user_structure.go:", path.Join(apppath, "structures", "user_structure.go")) fmt.Println("create structures user_structure.go:", path.Join(apppath, "structures", "user_structure.go"))
writetofile(path.Join(apppath, "structures", "user_structure.go"), apistructures) writetofile(path.Join(apppath, "structures", "user_structure.go"), apistructures)
fmt.Println("create structures object_structure.go:", path.Join(apppath, "structures", "object_structure.go"))
writetofile(path.Join(apppath, "structures", "object_structure.go"), apistructures2)
fmt.Println("create helpers user_helper.go:", path.Join(apppath, "helpers", "user_helper.go"))
writetofile(path.Join(apppath, "helpers", "user_helper.go"), "package helpers")
fmt.Println("create helpers object_helper.go:", path.Join(apppath, "helpers", "object_helper.go"))
writetofile(path.Join(apppath, "helpers", "object_helper.go"), "package helpers")
fmt.Println("create docs doc.go:", path.Join(apppath, "docs", "doc.go")) fmt.Println("create docs doc.go:", path.Join(apppath, "docs", "doc.go"))
writetofile(path.Join(apppath, "docs", "doc.go"), "package docs") writetofile(path.Join(apppath, "docs", "doc.go"), "package docs")