111 lines
3.6 KiB
Go
111 lines
3.6 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Post 博客文章模型
|
|
type Post struct {
|
|
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 博客文章响应模型
|
|
type PostResponse struct {
|
|
ID uint `json:"id"`
|
|
Title string `json:"title"`
|
|
CategoryID uint `json:"categoryId"`
|
|
CategoryName string `json:"categoryName"`
|
|
CategorySlug string `json:"categorySlug"`
|
|
Date string `json:"date"`
|
|
Excerpt string `json:"excerpt,omitempty"`
|
|
Content string `json:"content,omitempty"`
|
|
Tags []Tag `json:"tags,omitempty"`
|
|
}
|
|
|
|
// PostHistory 文章历史记录模型
|
|
type PostHistory struct {
|
|
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 文章历史记录响应模型
|
|
type PostHistoryResponse struct {
|
|
ID uint `json:"id"`
|
|
PostID uint `json:"postId"`
|
|
Version int `json:"version"`
|
|
Title string `json:"title"`
|
|
CategoryID uint `json:"categoryId"`
|
|
CategoryName string `json:"categoryName"`
|
|
Date string `json:"date"`
|
|
IsPublished int `json:"isPublished"`
|
|
ModifiedBy uint `json:"modifiedBy"`
|
|
ModifiedAt string `json:"modifiedAt"`
|
|
CreatedAt string `json:"createdAt"`
|
|
}
|