Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add update user data api #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ require (
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/copier v0.4.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jellydator/ttlcache/v2 v2.11.1 h1:AZGME43Eh2Vv3giG6GeqeLeFXxwxn1/qHItqWZl6U64=
github.com/jellydator/ttlcache/v2 v2.11.1/go.mod h1:RtE5Snf0/57e+2cLWFYWCCsLas2Hy3c5Z4n14XmSvTI=
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
Expand Down
10 changes: 10 additions & 0 deletions internal/common/dao/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ func SetUserPassword(user *model.User, newpass string) (err error) {
return err
}

// UpdateUserData 根据map更新用户信息
func UpdateUserData(user *model.User, maps map[string]interface{}) (err error) {
u := query.User
_, err = u.Where(u.UserID.Eq(user.UserID)).Updates(maps)
if err != nil {
return err
}
return err
}

// GetUserByEmail 根据 email 获取用户
func GetUserByEmail(email string) (user *model.User, err error) {
q := query.User
Expand Down
4 changes: 4 additions & 0 deletions internal/router/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func NewAPI() *gin.Engine {
jwt.RequireAuth(cache.Clients[cache.JWTBlacklist], true), // 把 token 拉黑
user_service.ResetPassword)
}
// 更新用户信息
user.POST("profile/update",
jwt.RequireAuth(cache.Clients[cache.JWTBlacklist], false),
user_service.ChangeUser)
}

return r
Expand Down
66 changes: 66 additions & 0 deletions internal/service/user/chagedata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package user

import (
"github.com/TensoRaws/NuxBT-Backend/internal/common/dao"
"github.com/TensoRaws/NuxBT-Backend/module/util"
"github.com/gin-gonic/gin"
)

type ChangeUserRequest struct {
Avatar string `json:"avatar" `
Background string `json:"background" `
Email string `json:"email" `
Private int `json:"private" `
Signature string `json:"signature" `
Username string `json:"username"`
}

// ChangeUser 用户信息更新 (POST /profile/update)
func ChangeUser(c *gin.Context) {
// 参数绑定
var req ChangeUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
util.AbortWithMsg(c, "invalid request")
return
}

userID, err := util.GetUserIDFromGinContext(c)
if err != nil {
util.AbortWithMsg(c, "Please login first")
return
}

user, err := dao.GetUserByID(int32(userID))

Check failure on line 33 in internal/service/user/chagedata.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
if err != nil {
util.AbortWithMsg(c, "User not found")
return
}
// 迁移数据
updates := make(map[string]interface{})
if req.Private == 1 {

Check failure on line 40 in internal/service/user/chagedata.go

View workflow job for this annotation

GitHub Actions / lint

ifElseChain: rewrite if-else to switch statement (gocritic)
updates["private"] = true
} else if req.Private == 2 {
updates["private"] = false
} else if req.Private != 0 {
util.AbortWithMsg(c, "invalid request")
return
}
if req.Username != "" {
updates["username"] = req.Username
}
if req.Email != "" {
updates["email"] = req.Email
}
if req.Avatar != "" {
updates["avatar"] = req.Avatar
}
if req.Signature != "" {
updates["signature"] = req.Signature
}
if req.Background != "" {
updates["background"] = req.Background
}
// 执行更新
dao.UpdateUserData(user, updates)

Check failure on line 64 in internal/service/user/chagedata.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `dao.UpdateUserData` is not checked (errcheck)
util.OKWithMsg(c, "update success")
}
Loading