数据结构优化
This commit is contained in:
@@ -1,20 +1,52 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Post 博客文章模型
|
||||
type Post struct {
|
||||
ID uint `json:"id"`
|
||||
OriginalID string `json:"originalId,omitempty"` // For backward compatibility
|
||||
Title string `json:"title"`
|
||||
CategoryID uint `json:"categoryId"`
|
||||
Category *Category `json:"category,omitempty"` // For join query result
|
||||
Excerpt string `json:"excerpt"`
|
||||
Content string `json:"content"`
|
||||
ReadCount uint `json:"readCount"`
|
||||
IsPublished int `json:"isPublished"` // 0: draft, 1: published
|
||||
Tags []Tag `json:"tags"`
|
||||
CreatedAt int64 `json:"createdAt"`
|
||||
UpdatedAt int64 `json:"updatedAt"`
|
||||
DeletedAt int64 `json:"deletedAt"`
|
||||
ID uint `json:"id" gorm:"primaryKey;column:id"`
|
||||
OriginalID string `json:"originalId,omitempty" gorm:"-"` // For backward compatibility
|
||||
Title string `json:"title" gorm:"column:title"`
|
||||
CategoryID uint `json:"categoryId" gorm:"column:category_id"`
|
||||
Category *Category `json:"category,omitempty" gorm:"foreignKey:CategoryID"` // For join query result
|
||||
Excerpt string `json:"excerpt" gorm:"column:excerpt"`
|
||||
Content string `json:"content" gorm:"column:content;type:text"`
|
||||
ReadCount uint `json:"readCount" gorm:"column:read_count;default:0"`
|
||||
IsPublished int `json:"isPublished" gorm:"column:is_published;default:0"` // 0: draft, 1: published
|
||||
Tags []Tag `json:"tags" gorm:"many2many:post_tags;joinForeignKey:post_id;joinReferences:tag_id"`
|
||||
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 (Post) TableName() string {
|
||||
return "posts"
|
||||
}
|
||||
|
||||
// BeforeCreate 创建前钩子
|
||||
func (p *Post) BeforeCreate(tx *gorm.DB) error {
|
||||
now := time.Now().Unix()
|
||||
if p.CreatedAt == 0 {
|
||||
p.CreatedAt = now
|
||||
}
|
||||
if p.UpdatedAt == 0 {
|
||||
p.UpdatedAt = now
|
||||
}
|
||||
if p.DeletedAt == 0 {
|
||||
p.DeletedAt = 0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BeforeUpdate 更新前钩子
|
||||
func (p *Post) BeforeUpdate(tx *gorm.DB) error {
|
||||
p.UpdatedAt = time.Now().Unix()
|
||||
return nil
|
||||
}
|
||||
|
||||
// PostResponse 博客文章响应模型
|
||||
@@ -32,17 +64,34 @@ type PostResponse struct {
|
||||
|
||||
// PostHistory 文章历史记录模型
|
||||
type PostHistory struct {
|
||||
ID uint `json:"id"`
|
||||
PostID uint `json:"postId"`
|
||||
Version int `json:"version"`
|
||||
Title string `json:"title"`
|
||||
CategoryID uint `json:"categoryId"`
|
||||
Excerpt string `json:"excerpt"`
|
||||
Content string `json:"content"`
|
||||
IsPublished int `json:"isPublished"`
|
||||
ModifiedBy uint `json:"modifiedBy"`
|
||||
ModifiedAt int64 `json:"modifiedAt"`
|
||||
CreatedAt int64 `json:"createdAt"`
|
||||
ID uint `json:"id" gorm:"primaryKey;column:id"`
|
||||
PostID uint `json:"postId" gorm:"column:post_id;index"`
|
||||
Version int `json:"version" gorm:"column:version"`
|
||||
Title string `json:"title" gorm:"column:title"`
|
||||
CategoryID uint `json:"categoryId" gorm:"column:category_id"`
|
||||
Excerpt string `json:"excerpt" gorm:"column:excerpt"`
|
||||
Content string `json:"content" gorm:"column:content;type:text"`
|
||||
IsPublished int `json:"isPublished" gorm:"column:is_published"`
|
||||
ModifiedBy uint `json:"modifiedBy" gorm:"column:modified_by"`
|
||||
ModifiedAt int64 `json:"modifiedAt" gorm:"column:modified_at"`
|
||||
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
func (PostHistory) TableName() string {
|
||||
return "post_history"
|
||||
}
|
||||
|
||||
// BeforeCreate 创建前钩子
|
||||
func (ph *PostHistory) BeforeCreate(tx *gorm.DB) error {
|
||||
now := time.Now().Unix()
|
||||
if ph.CreatedAt == 0 {
|
||||
ph.CreatedAt = now
|
||||
}
|
||||
if ph.ModifiedAt == 0 {
|
||||
ph.ModifiedAt = now
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PostHistoryResponse 文章历史记录响应模型
|
||||
|
||||
Reference in New Issue
Block a user