48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package plant
|
|
|
|
import (
|
|
"sundynix-go/global"
|
|
"sundynix-go/model/plant"
|
|
plantReq "sundynix-go/model/plant/request"
|
|
"sundynix-go/model/system"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type UserProfileService struct{}
|
|
|
|
// UpdateProfile 修改用户信息
|
|
func (s *UserProfileService) UpdateProfile(req plantReq.UpdateProfile, userId string) error {
|
|
return global.DB.Transaction(func(tx *gorm.DB) error {
|
|
//1.更新profile
|
|
updateMap := map[string]interface{}{
|
|
"nick_name": req.Nickname,
|
|
"avatar_id": req.AvatarId,
|
|
}
|
|
err := tx.Model(&plant.UserProfile{}).Where("user_id = ?", userId).Updates(updateMap).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
//2.更新user表
|
|
updateMap = map[string]interface{}{
|
|
"name": req.Nickname,
|
|
"avatar_id": req.AvatarId,
|
|
}
|
|
err = tx.Model(&system.User{}).Where("id = ?", userId).Updates(updateMap).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// ProfileDetail 获取用户详情
|
|
func (s *UserProfileService) ProfileDetail(userId string) (plant.UserProfile, error) {
|
|
var res plant.UserProfile
|
|
err := global.DB.Where("user_id = ?", userId).Preload("Avatar").Preload("Level").First(&res).Error
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
return res, nil
|
|
}
|