longyu 3 年 前
コミット
b10daac4b3
7 ファイル変更396 行追加0 行削除
  1. 1 0
      .gitignore
  2. 92 0
      controllers/object.go
  3. 119 0
      controllers/user.go
  4. 15 0
      main.go
  5. 53 0
      models/object.go
  6. 86 0
      models/user.go
  7. 30 0
      routers/router.go

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+/conf

+ 92 - 0
controllers/object.go

@@ -0,0 +1,92 @@
+package controllers
+
+import (
+	"hongze_edb_lib/models"
+	"encoding/json"
+
+	beego "github.com/beego/beego/v2/server/web"
+)
+
+// Operations about object
+type ObjectController struct {
+	beego.Controller
+}
+
+// @Title Create
+// @Description create object
+// @Param	body		body 	models.Object	true		"The object content"
+// @Success 200 {string} models.Object.Id
+// @Failure 403 body is empty
+// @router / [post]
+func (o *ObjectController) Post() {
+	var ob models.Object
+	json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
+	objectid := models.AddOne(ob)
+	o.Data["json"] = map[string]string{"ObjectId": objectid}
+	o.ServeJSON()
+}
+
+// @Title Get
+// @Description find object by objectid
+// @Param	objectId		path 	string	true		"the objectid you want to get"
+// @Success 200 {object} models.Object
+// @Failure 403 :objectId is empty
+// @router /:objectId [get]
+func (o *ObjectController) Get() {
+	objectId := o.Ctx.Input.Param(":objectId")
+	if objectId != "" {
+		ob, err := models.GetOne(objectId)
+		if err != nil {
+			o.Data["json"] = err.Error()
+		} else {
+			o.Data["json"] = ob
+		}
+	}
+	o.ServeJSON()
+}
+
+// @Title GetAll
+// @Description get all objects
+// @Success 200 {object} models.Object
+// @Failure 403 :objectId is empty
+// @router / [get]
+func (o *ObjectController) GetAll() {
+	obs := models.GetAll()
+	o.Data["json"] = obs
+	o.ServeJSON()
+}
+
+// @Title Update
+// @Description update the object
+// @Param	objectId		path 	string	true		"The objectid you want to update"
+// @Param	body		body 	models.Object	true		"The body"
+// @Success 200 {object} models.Object
+// @Failure 403 :objectId is empty
+// @router /:objectId [put]
+func (o *ObjectController) Put() {
+	objectId := o.Ctx.Input.Param(":objectId")
+	var ob models.Object
+	json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
+
+	err := models.Update(objectId, ob.Score)
+	if err != nil {
+		o.Data["json"] = err.Error()
+	} else {
+		o.Data["json"] = "update success!"
+	}
+	o.ServeJSON()
+}
+
+// @Title Delete
+// @Description delete the object
+// @Param	objectId		path 	string	true		"The objectId you want to delete"
+// @Success 200 {string} delete success!
+// @Failure 403 objectId is empty
+// @router /:objectId [delete]
+func (o *ObjectController) Delete() {
+	objectId := o.Ctx.Input.Param(":objectId")
+	models.Delete(objectId)
+	o.Data["json"] = "delete success!"
+	o.ServeJSON()
+}
+

+ 119 - 0
controllers/user.go

@@ -0,0 +1,119 @@
+package controllers
+
+import (
+	"hongze_edb_lib/models"
+	"encoding/json"
+
+	beego "github.com/beego/beego/v2/server/web"
+)
+
+// Operations about Users
+type UserController struct {
+	beego.Controller
+}
+
+// @Title CreateUser
+// @Description create users
+// @Param	body		body 	models.User	true		"body for user content"
+// @Success 200 {int} models.User.Id
+// @Failure 403 body is empty
+// @router / [post]
+func (u *UserController) Post() {
+	var user models.User
+	json.Unmarshal(u.Ctx.Input.RequestBody, &user)
+	uid := models.AddUser(user)
+	u.Data["json"] = map[string]string{"uid": uid}
+	u.ServeJSON()
+}
+
+// @Title GetAll
+// @Description get all Users
+// @Success 200 {object} models.User
+// @router / [get]
+func (u *UserController) GetAll() {
+	users := models.GetAllUsers()
+	u.Data["json"] = users
+	u.ServeJSON()
+}
+
+// @Title Get
+// @Description get user by uid
+// @Param	uid		path 	string	true		"The key for staticblock"
+// @Success 200 {object} models.User
+// @Failure 403 :uid is empty
+// @router /:uid [get]
+func (u *UserController) Get() {
+	uid := u.GetString(":uid")
+	if uid != "" {
+		user, err := models.GetUser(uid)
+		if err != nil {
+			u.Data["json"] = err.Error()
+		} else {
+			u.Data["json"] = user
+		}
+	}
+	u.ServeJSON()
+}
+
+// @Title Update
+// @Description update the user
+// @Param	uid		path 	string	true		"The uid you want to update"
+// @Param	body		body 	models.User	true		"body for user content"
+// @Success 200 {object} models.User
+// @Failure 403 :uid is not int
+// @router /:uid [put]
+func (u *UserController) Put() {
+	uid := u.GetString(":uid")
+	if uid != "" {
+		var user models.User
+		json.Unmarshal(u.Ctx.Input.RequestBody, &user)
+		uu, err := models.UpdateUser(uid, &user)
+		if err != nil {
+			u.Data["json"] = err.Error()
+		} else {
+			u.Data["json"] = uu
+		}
+	}
+	u.ServeJSON()
+}
+
+// @Title Delete
+// @Description delete the user
+// @Param	uid		path 	string	true		"The uid you want to delete"
+// @Success 200 {string} delete success!
+// @Failure 403 uid is empty
+// @router /:uid [delete]
+func (u *UserController) Delete() {
+	uid := u.GetString(":uid")
+	models.DeleteUser(uid)
+	u.Data["json"] = "delete success!"
+	u.ServeJSON()
+}
+
+// @Title Login
+// @Description Logs user into the system
+// @Param	username		query 	string	true		"The username for login"
+// @Param	password		query 	string	true		"The password for login"
+// @Success 200 {string} login success
+// @Failure 403 user not exist
+// @router /login [get]
+func (u *UserController) Login() {
+	username := u.GetString("username")
+	password := u.GetString("password")
+	if models.Login(username, password) {
+		u.Data["json"] = "login success"
+	} else {
+		u.Data["json"] = "user not exist"
+	}
+	u.ServeJSON()
+}
+
+// @Title logout
+// @Description Logs out current logged in user session
+// @Success 200 {string} logout success
+// @router /logout [get]
+func (u *UserController) Logout() {
+	u.Data["json"] = "logout success"
+	u.ServeJSON()
+}
+

+ 15 - 0
main.go

@@ -0,0 +1,15 @@
+package main
+
+import (
+	_ "hongze_edb_lib/routers"
+
+	beego "github.com/beego/beego/v2/server/web"
+)
+
+func main() {
+	if beego.BConfig.RunMode == "dev" {
+		beego.BConfig.WebConfig.DirectoryIndex = true
+		beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
+	}
+	beego.Run()
+}

+ 53 - 0
models/object.go

@@ -0,0 +1,53 @@
+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)
+}
+

+ 86 - 0
models/user.go

@@ -0,0 +1,86 @@
+package models
+
+import (
+	"errors"
+	"strconv"
+	"time"
+)
+
+var (
+	UserList map[string]*User
+)
+
+func init() {
+	UserList = make(map[string]*User)
+	u := User{"user_11111", "astaxie", "11111", Profile{"male", 20, "Singapore", "astaxie@gmail.com"}}
+	UserList["user_11111"] = &u
+}
+
+type User struct {
+	Id       string
+	Username string
+	Password string
+	Profile  Profile
+}
+
+type Profile struct {
+	Gender  string
+	Age     int
+	Address string
+	Email   string
+}
+
+func AddUser(u User) string {
+	u.Id = "user_" + strconv.FormatInt(time.Now().UnixNano(), 10)
+	UserList[u.Id] = &u
+	return u.Id
+}
+
+func GetUser(uid string) (u *User, err error) {
+	if u, ok := UserList[uid]; ok {
+		return u, nil
+	}
+	return nil, errors.New("User not exists")
+}
+
+func GetAllUsers() map[string]*User {
+	return UserList
+}
+
+func UpdateUser(uid string, uu *User) (a *User, err error) {
+	if u, ok := UserList[uid]; ok {
+		if uu.Username != "" {
+			u.Username = uu.Username
+		}
+		if uu.Password != "" {
+			u.Password = uu.Password
+		}
+		if uu.Profile.Age != 0 {
+			u.Profile.Age = uu.Profile.Age
+		}
+		if uu.Profile.Address != "" {
+			u.Profile.Address = uu.Profile.Address
+		}
+		if uu.Profile.Gender != "" {
+			u.Profile.Gender = uu.Profile.Gender
+		}
+		if uu.Profile.Email != "" {
+			u.Profile.Email = uu.Profile.Email
+		}
+		return u, nil
+	}
+	return nil, errors.New("User Not Exist")
+}
+
+func Login(username, password string) bool {
+	for _, u := range UserList {
+		if u.Username == username && u.Password == password {
+			return true
+		}
+	}
+	return false
+}
+
+func DeleteUser(uid string) {
+	delete(UserList, uid)
+}

+ 30 - 0
routers/router.go

@@ -0,0 +1,30 @@
+// @APIVersion 1.0.0
+// @Title beego Test API
+// @Description beego has a very cool tools to autogenerate documents for your API
+// @Contact astaxie@gmail.com
+// @TermsOfServiceUrl http://beego.me/
+// @License Apache 2.0
+// @LicenseUrl http://www.apache.org/licenses/LICENSE-2.0.html
+package routers
+
+import (
+	"hongze_edb_lib/controllers"
+
+	beego "github.com/beego/beego/v2/server/web"
+)
+
+func init() {
+	ns := beego.NewNamespace("/v1",
+		beego.NSNamespace("/object",
+			beego.NSInclude(
+				&controllers.ObjectController{},
+			),
+		),
+		beego.NSNamespace("/user",
+			beego.NSInclude(
+				&controllers.UserController{},
+			),
+		),
+	)
+	beego.AddNamespace(ns)
+}