41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package organize
|
|
|
|
import (
|
|
"context"
|
|
"git.nobla.cn/golang/moto/common/db"
|
|
"git.nobla.cn/golang/moto/internal/organize/types"
|
|
"git.nobla.cn/golang/moto/models"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|