Skip to content

Commit e3b77ba

Browse files
committedAug 13, 2022
编辑个人资料
1 parent 05866a0 commit e3b77ba

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
lines changed
 

‎app/http/controllers/api/v1/users_controller.go

+19
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,22 @@ func (ctrl *UsersController) Index(c *gin.Context) {
3131
"pager": pager,
3232
})
3333
}
34+
35+
func (ctrl *UsersController) UpdateProfile(c *gin.Context) {
36+
37+
request := requests.UserUpdateProfileRequest{}
38+
if ok := requests.Validate(c, &request, requests.UserUpdateProfile); !ok {
39+
return
40+
}
41+
42+
currentUser := auth.CurrentUser(c)
43+
currentUser.Name = request.Name
44+
currentUser.City = request.City
45+
currentUser.Introduction = request.Introduction
46+
rowsAffected := currentUser.Save()
47+
if rowsAffected > 0 {
48+
response.Data(c, currentUser)
49+
} else {
50+
response.Abort500(c, "更新失败,请稍后尝试~")
51+
}
52+
}

‎app/models/user/user_model.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import (
1111
type User struct {
1212
models.BaseModel
1313

14-
Name string `json:"name,omitempty"`
14+
Name string `json:"name,omitempty"`
15+
16+
City string `json:"city,omitempty"`
17+
Introduction string `json:"introduction,omitempty"`
18+
Avatar string `json:"avatar,omitempty"`
19+
1520
Email string `json:"-"`
1621
Phone string `json:"-"`
1722
Password string `json:"-"`

‎app/requests/user_request.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package requests
2+
3+
import (
4+
"goapi/pkg/auth"
5+
6+
"github.com/gin-gonic/gin"
7+
"github.com/thedevsaddam/govalidator"
8+
)
9+
10+
type UserUpdateProfileRequest struct {
11+
Name string `valid:"name" json:"name"`
12+
City string `valid:"city" json:"city"`
13+
Introduction string `valid:"introduction" json:"introduction"`
14+
}
15+
16+
func UserUpdateProfile(data interface{}, c *gin.Context) map[string][]string {
17+
18+
// 查询用户名重复时,过滤掉当前用户 ID
19+
uid := auth.CurrentUID(c)
20+
rules := govalidator.MapData{
21+
"name": []string{"required", "alpha_num", "between:3,20", "not_exists:users,name," + uid},
22+
"introduction": []string{"min_cn:4", "max_cn:240"},
23+
"city": []string{"min_cn:2", "max_cn:20"},
24+
}
25+
messages := govalidator.MapData{
26+
"name": []string{
27+
"required:用户名为必填项",
28+
"alpha_num:用户名格式错误,只允许数字和英文",
29+
"between:用户名长度需在 3~20 之间",
30+
"not_exists:用户名已被占用",
31+
},
32+
"introduction": []string{
33+
"min_cn:描述长度需至少 4 个字",
34+
"max_cn:描述长度不能超过 240 个字",
35+
},
36+
"city": []string{
37+
"min_cn:城市需至少 2 个字",
38+
"max_cn:城市不能超过 20 个字",
39+
},
40+
}
41+
return validate(data, rules, messages)
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package migrations
2+
3+
import (
4+
"database/sql"
5+
"goapi/app/models"
6+
"goapi/pkg/migrate"
7+
8+
"gorm.io/gorm"
9+
)
10+
11+
func init() {
12+
13+
type User struct {
14+
models.BaseModel
15+
16+
City string `gorm:"type:varchar(10);"`
17+
Introduction string `gorm:"type:varchar(255);"`
18+
Avatar string `gorm:"type:varchar(255);default:null"`
19+
20+
models.CommonTimestampsField
21+
}
22+
23+
up := func(migrator gorm.Migrator, DB *sql.DB) {
24+
migrator.AutoMigrate(&User{})
25+
}
26+
27+
down := func(migrator gorm.Migrator, DB *sql.DB) {
28+
migrator.DropColumn(&User{}, "City")
29+
migrator.DropColumn(&User{}, "Introduction")
30+
migrator.DropColumn(&User{}, "Avatar")
31+
}
32+
33+
migrate.Add("2022_08_13_230458_add_fields_to_user", up, down)
34+
}

‎routes/api.go

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func RegisterAPIRoutes(r *gin.Engine) {
7474
usersGroup := v1.Group("/users")
7575
{
7676
usersGroup.GET("", uc.Index)
77+
usersGroup.PUT("", middlewares.AuthJWT(), uc.UpdateProfile)
7778
}
7879

7980
// 分类

0 commit comments

Comments
 (0)