添加用户修改setting接口
This commit is contained in:
100
controllers/user/setting/userSetting.go
Normal file
100
controllers/user/setting/userSetting.go
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
package setting
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"toutoukan/init/databaseInit"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserSettingUpdateReq struct {
|
||||||
|
UID string `json:"uid" binding:"required"` // 用户ID,必填
|
||||||
|
Password string `json:"password"` // 密码,可选
|
||||||
|
AvatarURL string `json:"avatar_url"` // 头像URL,可选
|
||||||
|
Gender int `json:"gender"` // 性别,可选
|
||||||
|
BirthdateDate string `json:"birthdate_date"` // 生日,可选
|
||||||
|
Bio string `json:"bio"` // 个人简介,可选
|
||||||
|
Username string `json:"username"` // 用户名,可选
|
||||||
|
}
|
||||||
|
|
||||||
|
// 自定义验证函数,确保除uid外至少有一个字段有值
|
||||||
|
func (u *UserSettingUpdateReq) Validate() error {
|
||||||
|
if u.Password == "" && u.AvatarURL == "" && u.Gender == 0 && u.BirthdateDate == "" && u.Bio == "" && u.Username == "" {
|
||||||
|
return ErrAtLeastOneFieldRequired
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定义错误变量
|
||||||
|
var ErrAtLeastOneFieldRequired = errors.New("at least one field (password, avatar_url, gender, birthdate_date, bio, username) is required")
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
UID string `gorm:"column:uid;type:varchar(40);primaryKey"`
|
||||||
|
Telephone string `gorm:"column:telephone;type:varchar(255)"`
|
||||||
|
Password string `gorm:"column:password;type:varchar(30)"`
|
||||||
|
AvatarURL string `gorm:"column:avatar_url;type:varchar(255);default:'https://默认头像地址'"`
|
||||||
|
Gender int `gorm:"column:gender;type:int"`
|
||||||
|
BirthdateDate string `gorm:"column:birthdate_date;type:datetime"`
|
||||||
|
CreateTime string `gorm:"column:createtime;type:datetime"`
|
||||||
|
UpdateTime string `gorm:"column:updatetime;type:datetime"`
|
||||||
|
Bio string `gorm:"column:bio;type:varchar(255)"`
|
||||||
|
Username string `gorm:"column:username;type:varchar(30)"`
|
||||||
|
TotalPoints int `gorm:"column:total_points;type:int;default:0"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// TableName 指定表名
|
||||||
|
func (User) TableName() string {
|
||||||
|
return "user_info"
|
||||||
|
}
|
||||||
|
|
||||||
|
// ChangeUserSetting
|
||||||
|
func ChangeUserSetting(c *gin.Context) {
|
||||||
|
// 1. 绑定请求体到结构体
|
||||||
|
var req UserSettingUpdateReq
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 自定义验证:确保至少有一个字段有值
|
||||||
|
if err := req.Validate(); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 准备要更新的数据
|
||||||
|
var updateData User
|
||||||
|
if req.Password != "" {
|
||||||
|
updateData.Password = req.Password
|
||||||
|
}
|
||||||
|
if req.AvatarURL != "" {
|
||||||
|
updateData.AvatarURL = req.AvatarURL
|
||||||
|
}
|
||||||
|
if req.Gender != 0 {
|
||||||
|
updateData.Gender = req.Gender
|
||||||
|
}
|
||||||
|
if req.BirthdateDate != "" {
|
||||||
|
updateData.BirthdateDate = req.BirthdateDate
|
||||||
|
}
|
||||||
|
if req.Bio != "" {
|
||||||
|
updateData.Bio = req.Bio
|
||||||
|
}
|
||||||
|
if req.Username != "" {
|
||||||
|
updateData.Username = req.Username
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 执行数据库更新
|
||||||
|
result := databaseInit.UserDB.Model(&User{}).Where("uid = ?", req.UID).Updates(updateData)
|
||||||
|
if result.Error != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if result.RowsAffected == 0 {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. 返回成功响应
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "user setting updated successfully", "success": true})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user