update
This commit is contained in:
@ -18,6 +18,10 @@ type UserRepository interface {
|
||||
FindByID(ctx context.Context, id uint32) (*model.User, error)
|
||||
// FindByAccount 按用户名或邮箱查找(忽略已删除账号),供登录校验。
|
||||
FindByAccount(ctx context.Context, account string) (*model.User, error)
|
||||
// SetTier 设置用户等级(free/vip),供后台管理提级 VIP。
|
||||
SetTier(ctx context.Context, id uint32, tier string) error
|
||||
// List 列出全量用户(后台管理用),按 id 升序。
|
||||
List(ctx context.Context) ([]model.User, error)
|
||||
}
|
||||
|
||||
type userRepository struct {
|
||||
@ -44,6 +48,22 @@ func (r *userRepository) FindByAccount(ctx context.Context, account string) (*mo
|
||||
return wrapUser(&u, err)
|
||||
}
|
||||
|
||||
func (r *userRepository) SetTier(ctx context.Context, id uint32, tier string) error {
|
||||
return r.db.WithContext(ctx).
|
||||
Model(&model.User{}).
|
||||
Where("id = ? AND is_deleted = ?", id, 0).
|
||||
Update("tier", tier).Error
|
||||
}
|
||||
|
||||
func (r *userRepository) List(ctx context.Context) ([]model.User, error) {
|
||||
var users []model.User
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("is_deleted = ?", 0).
|
||||
Order("id ASC").
|
||||
Find(&users).Error
|
||||
return users, err
|
||||
}
|
||||
|
||||
func wrapUser(u *model.User, err error) (*model.User, error) {
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
|
||||
Reference in New Issue
Block a user