|  | 
|  | 1 | +package controllers | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"iceinto/apiproject/models" | 
|  | 5 | +	"encoding/json" | 
|  | 6 | + | 
|  | 7 | +	"github.com/astaxie/beego" | 
|  | 8 | +) | 
|  | 9 | + | 
|  | 10 | +// Operations about Users | 
|  | 11 | +type UserController struct { | 
|  | 12 | +	beego.Controller | 
|  | 13 | +} | 
|  | 14 | + | 
|  | 15 | +// @Title CreateUser | 
|  | 16 | +// @Description create users | 
|  | 17 | +// @Param	body		body 	models.User	true		"body for user content" | 
|  | 18 | +// @Success 200 {int} models.User.Id | 
|  | 19 | +// @Failure 403 body is empty | 
|  | 20 | +// @router / [post] | 
|  | 21 | +func (u *UserController) Post() { | 
|  | 22 | +	var user models.User | 
|  | 23 | +	json.Unmarshal(u.Ctx.Input.RequestBody, &user) | 
|  | 24 | +	uid := models.AddUser(user) | 
|  | 25 | +	u.Data["json"] = map[string]string{"uid": uid} | 
|  | 26 | +	u.ServeJSON() | 
|  | 27 | +} | 
|  | 28 | + | 
|  | 29 | +// @Title GetAll | 
|  | 30 | +// @Description get all Users | 
|  | 31 | +// @Success 200 {object} models.User | 
|  | 32 | +// @router / [get] | 
|  | 33 | +func (u *UserController) GetAll() { | 
|  | 34 | +	users := models.GetAllUsers() | 
|  | 35 | +	u.Data["json"] = users | 
|  | 36 | +	u.ServeJSON() | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | +// @Title Get | 
|  | 40 | +// @Description get user by uid | 
|  | 41 | +// @Param	uid		path 	string	true		"The key for staticblock" | 
|  | 42 | +// @Success 200 {object} models.User | 
|  | 43 | +// @Failure 403 :uid is empty | 
|  | 44 | +// @router /:uid [get] | 
|  | 45 | +func (u *UserController) Get() { | 
|  | 46 | +	uid := u.GetString(":uid") | 
|  | 47 | +	if uid != "" { | 
|  | 48 | +		user, err := models.GetUser(uid) | 
|  | 49 | +		if err != nil { | 
|  | 50 | +			u.Data["json"] = err.Error() | 
|  | 51 | +		} else { | 
|  | 52 | +			u.Data["json"] = user | 
|  | 53 | +		} | 
|  | 54 | +	} | 
|  | 55 | +	u.ServeJSON() | 
|  | 56 | +} | 
|  | 57 | + | 
|  | 58 | +// @Title Update | 
|  | 59 | +// @Description update the user | 
|  | 60 | +// @Param	uid		path 	string	true		"The uid you want to update" | 
|  | 61 | +// @Param	body		body 	models.User	true		"body for user content" | 
|  | 62 | +// @Success 200 {object} models.User | 
|  | 63 | +// @Failure 403 :uid is not int | 
|  | 64 | +// @router /:uid [put] | 
|  | 65 | +func (u *UserController) Put() { | 
|  | 66 | +	uid := u.GetString(":uid") | 
|  | 67 | +	if uid != "" { | 
|  | 68 | +		var user models.User | 
|  | 69 | +		json.Unmarshal(u.Ctx.Input.RequestBody, &user) | 
|  | 70 | +		uu, err := models.UpdateUser(uid, &user) | 
|  | 71 | +		if err != nil { | 
|  | 72 | +			u.Data["json"] = err.Error() | 
|  | 73 | +		} else { | 
|  | 74 | +			u.Data["json"] = uu | 
|  | 75 | +		} | 
|  | 76 | +	} | 
|  | 77 | +	u.ServeJSON() | 
|  | 78 | +} | 
|  | 79 | + | 
|  | 80 | +// @Title Delete | 
|  | 81 | +// @Description delete the user | 
|  | 82 | +// @Param	uid		path 	string	true		"The uid you want to delete" | 
|  | 83 | +// @Success 200 {string} delete success! | 
|  | 84 | +// @Failure 403 uid is empty | 
|  | 85 | +// @router /:uid [delete] | 
|  | 86 | +func (u *UserController) Delete() { | 
|  | 87 | +	uid := u.GetString(":uid") | 
|  | 88 | +	models.DeleteUser(uid) | 
|  | 89 | +	u.Data["json"] = "delete success!" | 
|  | 90 | +	u.ServeJSON() | 
|  | 91 | +} | 
|  | 92 | + | 
|  | 93 | +// @Title Login | 
|  | 94 | +// @Description Logs user into the system | 
|  | 95 | +// @Param	username		query 	string	true		"The username for login" | 
|  | 96 | +// @Param	password		query 	string	true		"The password for login" | 
|  | 97 | +// @Success 200 {string} login success | 
|  | 98 | +// @Failure 403 user not exist | 
|  | 99 | +// @router /login [get] | 
|  | 100 | +func (u *UserController) Login() { | 
|  | 101 | +	username := u.GetString("username") | 
|  | 102 | +	password := u.GetString("password") | 
|  | 103 | +	if models.Login(username, password) { | 
|  | 104 | +		u.Data["json"] = "login success" | 
|  | 105 | +	} else { | 
|  | 106 | +		u.Data["json"] = "user not exist" | 
|  | 107 | +	} | 
|  | 108 | +	u.ServeJSON() | 
|  | 109 | +} | 
|  | 110 | + | 
|  | 111 | +// @Title logout | 
|  | 112 | +// @Description Logs out current logged in user session | 
|  | 113 | +// @Success 200 {string} logout success | 
|  | 114 | +// @router /logout [get] | 
|  | 115 | +func (u *UserController) Logout() { | 
|  | 116 | +	u.Data["json"] = "logout success" | 
|  | 117 | +	u.ServeJSON() | 
|  | 118 | +} | 
|  | 119 | + | 
0 commit comments