-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update user info && fix some errors (#15)
and refactor some code -- --------- Co-authored-by: ckappgit <[email protected]>
- Loading branch information
Showing
9 changed files
with
162 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package user | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/TensoRaws/NuxBT-Backend/internal/common/dao" | ||
"github.com/TensoRaws/NuxBT-Backend/module/log" | ||
"github.com/TensoRaws/NuxBT-Backend/module/util" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type ProfileUpdateRequest struct { | ||
Avatar *string `json:"avatar" binding:"omitempty"` | ||
Background *string `json:"background" binding:"omitempty"` | ||
Email *string `json:"email" binding:"omitempty,email"` | ||
Private *bool `json:"private" binding:"omitempty"` | ||
Signature *string `json:"signature" binding:"omitempty"` | ||
Username *string `json:"username" binding:"omitempty"` | ||
} | ||
|
||
// ProfileUpdate 用户信息更新 (POST /profile/update) | ||
func ProfileUpdate(c *gin.Context) { | ||
// 参数绑定 | ||
var req ProfileUpdateRequest | ||
if err := c.ShouldBindJSON(&req); err != nil { | ||
util.AbortWithMsg(c, "invalid request: "+err.Error()) | ||
return | ||
} | ||
|
||
userID, _ := util.GetUserIDFromGinContext(c) | ||
|
||
// 准备更新数据 | ||
updates := make(map[string]interface{}) | ||
|
||
if req.Private != nil { | ||
updates["private"] = req.Private | ||
} | ||
|
||
if req.Username != nil && *req.Username != "" { | ||
err := util.CheckUsername(*req.Username) | ||
if err != nil { | ||
util.AbortWithMsg(c, "invalid username: "+err.Error()) | ||
return | ||
} | ||
updates["username"] = *req.Username | ||
} | ||
|
||
if req.Email != nil { | ||
updates["email"] = *req.Email | ||
} | ||
|
||
if req.Avatar != nil { | ||
updates["avatar"] = *req.Avatar | ||
} | ||
|
||
if req.Signature != nil { | ||
updates["signature"] = *req.Signature | ||
} | ||
|
||
if req.Background != nil { | ||
updates["background"] = *req.Background | ||
} | ||
// 执行更新 | ||
err := dao.UpdateUserDataByUserID(userID, updates) | ||
if err != nil { | ||
util.AbortWithMsg(c, "update failed: "+err.Error()) | ||
return | ||
} | ||
|
||
util.OKWithMsg(c, "update success") | ||
|
||
log.Logger.Info("update user profile success: " + strconv.Itoa(int(userID))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package util | ||
|
||
import "fmt" | ||
|
||
func CheckUsername(username string) error { | ||
l := len([]rune(username)) | ||
if l > 20 { | ||
return fmt.Errorf("username too long") | ||
} | ||
if l < 2 { | ||
return fmt.Errorf("username too short") | ||
} | ||
if username == "" { | ||
return fmt.Errorf("username cannot be empty") | ||
} | ||
adminWords := []string{"admin", "root", "administrator", "管理员", "超级管理员", "版主", "站长", "moderator"} | ||
for _, word := range adminWords { | ||
if username == word { | ||
return fmt.Errorf("username cannot be %s", word) | ||
} | ||
} | ||
// TODO: more checks,如检查敏感词 | ||
|
||
return nil | ||
} |