Exemple #1
0
// @Title 添加一个新的用户
// @Description 添加一个新的用户
// @Param	body		body 	models.User	true		"用户信息"
// @Success 200 {int} models.User.Id
// @Failure 400	请求的参数不正确
// @router / [post]
func (this *UserController) Post() {
	user := models.User{}
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &user)
	if err != nil {
		this.ResponseErrorJSON(400, errorFormat(ErrorBadJson_400, err.Error()))
	} else {
		user.Password = tools.MD5(user.Password)
		if id, err := models.AddUser(user); err != nil {
			this.ResponseErrorJSON(400, errorFormat(ErrorBadParam_400, err.Error()))
		} else {
			this.Data["json"] = map[string]interface{}{
				"code": 0,
				"data": id,
			}
		}
	}
	this.ServeJson()
}
Exemple #2
0
// @Title 更新用户信息
// @Description 更新用户信息
// @Param	id		path 	int	true		用户唯一ID
// @Param	body		body 	models.User	true		用户信息
// @Success 200 {object} models.User
// @Failure 400	请求的参数不正确
// @router /:id [put]
func (this *UserController) Put() {
	id, _ := this.GetInt64(":id")
	var user models.User
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &user); err != nil {
		this.ResponseErrorJSON(400, errorFormat(ErrorBadJson_400, err.Error()))
	} else {
		if user.Password != "" {
			user.Password = tools.MD5(user.Password)
		}
		update, err := models.UpdateUser(id, &user)
		if err != nil {
			this.ResponseErrorJSON(400, errorFormat(ErrorBadParam_400, err.Error()))
		} else {
			this.Data["json"] = map[string]interface{}{
				"code": 0,
				"data": update,
			}
		}
	}
	this.ServeJson()
}