update
This commit is contained in:
@ -16,6 +16,8 @@ import (
|
||||
type UserRepository interface {
|
||||
// FindByID 按主键查找用户。不存在时返回 ErrNotFound。
|
||||
FindByID(ctx context.Context, id uint32) (*model.User, error)
|
||||
// FindByAccount 按用户名或邮箱查找(忽略已删除账号),供登录校验。
|
||||
FindByAccount(ctx context.Context, account string) (*model.User, error)
|
||||
}
|
||||
|
||||
type userRepository struct {
|
||||
@ -33,6 +35,15 @@ func (r *userRepository) FindByID(ctx context.Context, id uint32) (*model.User,
|
||||
return wrapUser(&u, err)
|
||||
}
|
||||
|
||||
func (r *userRepository) FindByAccount(ctx context.Context, account string) (*model.User, error) {
|
||||
var u model.User
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("username = ? OR email = ?", account, account).
|
||||
Where("is_deleted = ?", 0).
|
||||
First(&u).Error
|
||||
return wrapUser(&u, 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