feat: add auto migrate and init account

This commit is contained in:
engigu
2024-01-07 21:30:59 +08:00
parent 312c591063
commit 63f6e93894
9 changed files with 116 additions and 36 deletions
+19 -5
View File
@@ -1,18 +1,21 @@
package models
import "github.com/jinzhu/gorm"
import (
"errors"
"github.com/jinzhu/gorm"
)
type Auth struct {
ID int `gorm:"primary_key" json:"id"`
Username string `json:"username"`
Password string `json:"password"`
ID int `json:"id" gorm:"type:int(11) AUTO_INCREMENT comment 'id';primary_key" json:"id"`
Username string `json:"username" gorm:"type:varchar(100) comment '用户名';default:'';"`
Password string `json:"password" gorm:"type:varchar(100) comment '密码';default:'';"`
}
// CheckAuth 检查用户信息
func CheckAuth(username, password string) (bool, error) {
var auth Auth
err := db.Select("id").Where(Auth{Username: username, Password: password}).First(&auth).Error
if err != nil && err != gorm.ErrRecordNotFound {
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return false, err
}
@@ -30,3 +33,14 @@ func EditUser(username string, data interface{}) error {
}
return nil
}
func AddUser(account string, password string) error {
auth := Auth{
Username: account,
Password: password,
}
if err := db.Create(&auth).Error; err != nil {
return err
}
return nil
}