数据结构优化
This commit is contained in:
@@ -1,22 +1,67 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Column 专栏模型
|
||||
type Column struct {
|
||||
ID uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Cover string `json:"cover"`
|
||||
IsActive int `json:"isActive"`
|
||||
SortOrder uint `json:"sortOrder"`
|
||||
CreatedAt int64 `json:"createdAt"`
|
||||
UpdatedAt int64 `json:"updatedAt"`
|
||||
DeletedAt int64 `json:"deletedAt"`
|
||||
ID uint `json:"id" gorm:"primaryKey;column:id"`
|
||||
Name string `json:"name" gorm:"column:name"`
|
||||
Description string `json:"description" gorm:"column:description;type:text"`
|
||||
Cover string `json:"cover" gorm:"column:cover"`
|
||||
IsActive int `json:"isActive" gorm:"column:is_active;default:1"`
|
||||
SortOrder uint `json:"sortOrder" gorm:"column:sort_order;default:0"`
|
||||
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
|
||||
UpdatedAt int64 `json:"updatedAt" gorm:"column:updated_at"`
|
||||
DeletedAt int64 `json:"deletedAt" gorm:"column:deleted_at;default:0"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (Column) TableName() string {
|
||||
return "columns"
|
||||
}
|
||||
|
||||
// BeforeCreate 创建前钩子
|
||||
func (c *Column) BeforeCreate(tx *gorm.DB) error {
|
||||
now := time.Now().Unix()
|
||||
if c.CreatedAt == 0 {
|
||||
c.CreatedAt = now
|
||||
}
|
||||
if c.UpdatedAt == 0 {
|
||||
c.UpdatedAt = now
|
||||
}
|
||||
if c.DeletedAt == 0 {
|
||||
c.DeletedAt = 0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BeforeUpdate 更新前钩子
|
||||
func (c *Column) BeforeUpdate(tx *gorm.DB) error {
|
||||
c.UpdatedAt = time.Now().Unix()
|
||||
return nil
|
||||
}
|
||||
|
||||
// ColumnPost 专栏文章关联模型
|
||||
type ColumnPost struct {
|
||||
ColumnID uint `json:"columnId"`
|
||||
PostID uint `json:"postId"`
|
||||
SortOrder uint `json:"sortOrder"`
|
||||
CreatedAt int64 `json:"createdAt"`
|
||||
ColumnID uint `json:"columnId" gorm:"primaryKey;column:column_id"`
|
||||
PostID uint `json:"postId" gorm:"primaryKey;column:post_id"`
|
||||
SortOrder uint `json:"sortOrder" gorm:"column:sort_order;default:0"`
|
||||
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (ColumnPost) TableName() string {
|
||||
return "column_posts"
|
||||
}
|
||||
|
||||
// BeforeCreate 创建前钩子
|
||||
func (cp *ColumnPost) BeforeCreate(tx *gorm.DB) error {
|
||||
if cp.CreatedAt == 0 {
|
||||
cp.CreatedAt = time.Now().Unix()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user