框架慢慢修改
This commit is contained in:
21
models/contents.go
Normal file
21
models/contents.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
type Contents struct {
|
||||
Cid int32 `gorm:"column:cid;primaryKey;autoIncrement:true" json:"cid"`
|
||||
Title string `gorm:"column:title" json:"title"`
|
||||
Slug string `gorm:"column:slug" json:"slug"`
|
||||
Created int32 `gorm:"column:created" json:"created"`
|
||||
Modified int32 `gorm:"column:modified" json:"modified"`
|
||||
Text string `gorm:"column:text" json:"text"`
|
||||
Order_ int32 `gorm:"column:order" json:"order"`
|
||||
AuthorID int32 `gorm:"column:authorId" json:"authorId"`
|
||||
Template string `gorm:"column:template" json:"template"`
|
||||
Type string `gorm:"column:type;default:post" json:"type"`
|
||||
Status string `gorm:"column:status;default:publish" json:"status"`
|
||||
Password string `gorm:"column:password" json:"password"`
|
||||
CommentsNum int32 `gorm:"column:commentsNum" json:"commentsNum"`
|
||||
AllowComment string `gorm:"column:allowComment;default:0" json:"allowComment"`
|
||||
AllowPing string `gorm:"column:allowPing;default:0" json:"allowPing"`
|
||||
AllowFeed string `gorm:"column:allowFeed;default:0" json:"allowFeed"`
|
||||
Parent int32 `gorm:"column:parent" json:"parent"`
|
||||
}
|
||||
@@ -12,7 +12,9 @@ import (
|
||||
"go_blog/pkg/util"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
@@ -23,20 +25,22 @@ func SetUp(isOrmDebug bool) {
|
||||
conf.DataBase.Password,
|
||||
conf.DataBase.Host,
|
||||
conf.DataBase.Port,
|
||||
conf.DataBase.DB,
|
||||
conf.DataBase.DBName,
|
||||
conf.DataBase.Charset)
|
||||
db, err := gorm.Open(conf.DataBase.Type, conUri)
|
||||
|
||||
db, err := gorm.Open(mysql.Open(conUri), &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
TablePrefix: conf.DataBase.Prefix,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
DB = db
|
||||
DB.LogMode(isOrmDebug)
|
||||
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
|
||||
return conf.DataBase.Prefix + defaultTableName
|
||||
}
|
||||
|
||||
DB.AutoMigrate(&Account{})
|
||||
DB.AutoMigrate(&Content{})
|
||||
DB.AutoMigrate(&Contents{})
|
||||
|
||||
}
|
||||
|
||||
@@ -48,7 +52,7 @@ type BaseModel struct {
|
||||
}
|
||||
|
||||
// 生成全局唯一ID
|
||||
func (m *BaseModel) BeforeCreate(scope *gorm.Scope) error {
|
||||
func (m *BaseModel) BeforeCreate(scope *gorm.DB) error {
|
||||
if m.ID == 0 {
|
||||
m.ID = util.GenSonyFlakeId()
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@ type Account struct {
|
||||
BaseModel
|
||||
Username string `gorm:"column:username;not null;unique_index;comment:'用户名'" json:"username" form:"username"`
|
||||
Password string `gorm:"column:password;comment:'密码'" form:"password" json:"-"`
|
||||
Name string `form:"name" json:"name"`
|
||||
IsActive bool `json:"-"`
|
||||
Name string `form:"name" json:"name"`
|
||||
IsActive bool `json:"-"`
|
||||
}
|
||||
|
||||
func (a *Account) TableName() string {
|
||||
@@ -59,7 +59,7 @@ func (a *Account) IsPasswordEqual(password string) bool {
|
||||
|
||||
// 验证用户民重复
|
||||
func (a *Account) CheckDuplicateUsername() bool {
|
||||
var count int
|
||||
var count int64
|
||||
if DB.Model(&Account{}).Where("username=?", a.Username).Count(&count); count > 0 {
|
||||
return false
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user