Skip to content

Commit

Permalink
refactor: user_id int32 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tohrusky authored Jul 14, 2024
1 parent b023e97 commit cad38b7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
1 change: 1 addition & 0 deletions internal/router/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func NewAPI() *gin.Engine {
// 用户查询他人信息
user.GET("profile/",
jwt.RequireAuth(cache.Clients[cache.JWTBlacklist], false),
middleware_cache.Response(cache.Clients[cache.RespCache], 1*time.Minute),
user_service.ProfileOthers,
)

Expand Down
17 changes: 8 additions & 9 deletions internal/service/user/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type ProfileResponse struct {
Username string `json:"username"`
}

type UserProfileRequest struct {
type ProfileOthersRequest struct {
UserId int `form:"user_id" binding:"required"`
}

Expand All @@ -36,13 +36,13 @@ func ProfileMe(c *gin.Context) {
return
}

user, err := dao.GetUserByID(int32(userID))
user, err := dao.GetUserByID(userID)
if err != nil {
util.AbortWithMsg(c, "User not found")
return
}

roles, err := dao.GetUserRolesByID(int32(userID))
roles, err := dao.GetUserRolesByID(userID)
if err != nil {
log.Logger.Info("Failed to get user roles: " + err.Error())
roles = []string{}
Expand All @@ -59,7 +59,7 @@ func ProfileMe(c *gin.Context) {
Private: user.Private,
Roles: roles,
Signature: user.Signature,
UserID: int32(user.UserID),
UserID: user.UserID,
Username: user.Username,
})

Expand All @@ -69,7 +69,7 @@ func ProfileMe(c *gin.Context) {
// ProfileOthers 用户查询他人信息 (GET /profile)
func ProfileOthers(c *gin.Context) {
// 绑定参数
var req UserProfileRequest
var req ProfileOthersRequest
if err := c.ShouldBindQuery(&req); err != nil {
util.AbortWithMsg(c, "invalid request")
return
Expand All @@ -81,7 +81,7 @@ func ProfileOthers(c *gin.Context) {
return
}
// 仅用于鉴权不使用
_, err = dao.GetUserByID(int32(userID))
_, err = dao.GetUserByID(userID)
if err != nil {
util.AbortWithMsg(c, "User not found")
return
Expand All @@ -93,7 +93,7 @@ func ProfileOthers(c *gin.Context) {
return
}

roles, err := dao.GetUserRolesByID(int32(userID))
roles, err := dao.GetUserRolesByID(userID)
if err != nil {
log.Logger.Info("Failed to get user roles: " + err.Error())
roles = []string{}
Expand All @@ -110,14 +110,13 @@ func ProfileOthers(c *gin.Context) {
Inviter: "",
LastActive: "",
Private: user.Private,
Roles: []string{},
Roles: nil,
Signature: "",
UserID: user.UserID,
Username: user.Username,
})
} else {
// 显示全部信息

util.OKWithData(c, ProfileResponse{
Avatar: user.Avatar,
Background: user.Background,
Expand Down
2 changes: 1 addition & 1 deletion internal/service/user/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func ResetPassword(c *gin.Context) {
return
}

user, err := dao.GetUserByID(int32(userID))
user, err := dao.GetUserByID(userID)
if err != nil {
util.AbortWithMsg(c, "User not found")
return
Expand Down
4 changes: 2 additions & 2 deletions module/util/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// GetUserIDFromGinContext 从 RequireAuth 处读取 user_id
func GetUserIDFromGinContext(c *gin.Context) (int64, error) {
func GetUserIDFromGinContext(c *gin.Context) (int32, error) {
userIDstr := c.GetString("user_id")
// 未登录
if len(userIDstr) == 0 {
Expand All @@ -18,7 +18,7 @@ func GetUserIDFromGinContext(c *gin.Context) (int64, error) {
// 已登录
userID, err := strconv.ParseInt(userIDstr, 10, 64)

return userID, err
return int32(userID), err
}

// OKWithMsg 返回成功信息
Expand Down

0 comments on commit cad38b7

Please sign in to comment.