diff --git a/controller/user.go b/controller/user.go index 9fe9814..85af316 100644 --- a/controller/user.go +++ b/controller/user.go @@ -3,6 +3,7 @@ package controller import ( "electricity_bill_calc/cache" "electricity_bill_calc/exceptions" + "electricity_bill_calc/global" "electricity_bill_calc/model" "electricity_bill_calc/repository" "electricity_bill_calc/response" @@ -35,6 +36,7 @@ func InitializeUserController(router *gin.Engine) { UserController.Router.POST("/account", security.OPSAuthorize, UserController.CreateOPSAndManagementAccount) UserController.Router.GET("/account/:uid", security.MustAuthenticated, UserController.GetUserDetail) UserController.Router.POST("/enterprise", security.OPSAuthorize, UserController.CreateEnterpriseAccount) + UserController.Router.PUT("/account/:uid", security.OPSAuthorize, UserController.ModifyAccountDetail) } type LoginFormData struct { @@ -302,3 +304,45 @@ func (_UserController) CreateEnterpriseAccount(c *gin.Context) { } result.Json(http.StatusCreated, "用户已经成功创建。", gin.H{"verify": verifyCode}) } + +type AccountModificationFormData struct { + Name string `json:"name" form:"name"` + Region *string `json:"region" form:"region"` + Address *string `json:"address" form:"address"` + Contact *string `json:"contact" form:"contact"` + Phone *string `json:"phone" form:"phone"` + UnitServiceFee *string `json:"unitServiceFee" form:"unitServiceFee"` +} + +func (_UserController) ModifyAccountDetail(c *gin.Context) { + result := response.NewResult(c) + targetUserId := c.Param("uid") + modForm := new(AccountModificationFormData) + c.BindJSON(modForm) + exists, err := service.UserService.IsUserExists(targetUserId) + if !exists { + result.NotFound("指定的用户不存在。") + return + } + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + newUserInfo := new(model.UserDetail) + newUserInfo.Name = &modForm.Name + newUserInfo.Region = modForm.Region + newUserInfo.Address = modForm.Address + newUserInfo.Contact = modForm.Contact + newUserInfo.Phone = modForm.Phone + newUserInfo.UnitServiceFee, err = decimal.NewFromString(*modForm.UnitServiceFee) + if err != nil { + result.BadRequest("用户月服务费无法解析。") + return + } + _, err = global.DBConn.ID(targetUserId).Update(newUserInfo) + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + result.Success("指定用户的信息已经更新。") +}