2024-12-12 17:39:04 +08:00
|
|
|
package organize
|
2024-12-12 16:24:50 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"git.nobla.cn/golang/moto/common/db"
|
2024-12-12 17:39:04 +08:00
|
|
|
"git.nobla.cn/golang/moto/internal/organize/types"
|
2024-12-13 10:37:09 +08:00
|
|
|
"git.nobla.cn/golang/moto/models"
|
2024-12-12 16:24:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Profile(ctx context.Context, uid string) (profile *types.Profile, err error) {
|
|
|
|
profile = &types.Profile{}
|
|
|
|
tx := db.WithContext(ctx)
|
|
|
|
err = tx.Table("users AS u").
|
|
|
|
Select("u.uid as id", "u.username", "u.avatar", "u.email", "u.description", "u.role", "r.permissions").
|
|
|
|
Joins("left join roles AS r on r.id = u.role").Where("u.uid=? ", uid).
|
|
|
|
First(profile).Error
|
|
|
|
return
|
|
|
|
}
|
2024-12-13 10:37:09 +08:00
|
|
|
|
|
|
|
func UpdateProfile(ctx context.Context, uid string, model *types.Profile) (err error) {
|
|
|
|
tx := db.DB().WithContext(ctx)
|
|
|
|
userModel := &models.User{}
|
|
|
|
if err = tx.Where("uid=?", uid).First(userModel).Error; err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if model.Avatar != "" {
|
|
|
|
userModel.Avatar = model.Avatar
|
|
|
|
}
|
|
|
|
if model.Email != "" {
|
|
|
|
userModel.Email = model.Email
|
|
|
|
}
|
|
|
|
if model.Username != "" {
|
|
|
|
userModel.Username = model.Username
|
|
|
|
}
|
|
|
|
if model.Description != "" {
|
|
|
|
userModel.Description = model.Description
|
|
|
|
}
|
|
|
|
err = tx.Save(userModel).Error
|
|
|
|
return
|
|
|
|
}
|