This commit is contained in:
toom1996
2026-08-31 19:59:56 +08:00
parent a325e438be
commit f4f6e02e3c
14 changed files with 594 additions and 20 deletions

View File

@ -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) {