update environment mode handling, refactor user registration logic, and add username/email existence checks

This commit is contained in:
2025-07-22 10:04:42 +08:00
parent 08872ea015
commit f07200b0b9
7 changed files with 51 additions and 24 deletions

View File

@ -71,14 +71,12 @@ func InitDatabase() error {
return errors.New("unsupported database driver, only sqlite and postgres are supported")
}
return nil
// 迁移模型
if err = migrate(); err != nil {
logrus.Error("Failed to migrate models:", err)
return err
}
// TODO: impl
//// 迁移模型
//if err = models.Migrate(db); err != nil {
// logrus.Error("Failed to migrate models:", err)
// return err
//}
//// 执行初始化数据
//// 创建管理员账户
//hashedPassword, err := utils.Password.HashPassword(config.AdminPassword, config.JwtSecret)
@ -95,7 +93,7 @@ func InitDatabase() error {
// logrus.Error("Failed to update admin user:", err)
// return err
//}
//return nil
return nil
}
// initPostgres 初始化PostgreSQL连接
@ -128,6 +126,8 @@ func migrate() error {
return GetDB().AutoMigrate(
&model.Comment{},
&model.Label{},
&model.OidcConfig{},
&model.Post{},
&model.Session{},
&model.User{})
}

View File

@ -43,3 +43,19 @@ func (user *userRepo) Update(userModel *model.User) error {
}
return nil
}
func (user *userRepo) CheckUsernameExists(username string) (bool, error) {
var count int64
if err := GetDB().Model(&model.User{}).Where("username = ?", username).Count(&count).Error; err != nil {
return false, err
}
return count > 0, nil
}
func (user *userRepo) CheckEmailExists(email string) (bool, error) {
var count int64
if err := GetDB().Model(&model.User{}).Where("email = ?", email).Count(&count).Error; err != nil {
return false, err
}
return count > 0, nil
}