Files

82 lines
2.2 KiB
Go
Raw Permalink Normal View History

2026-01-16 17:03:34 +08:00
package models
2026-01-19 13:53:32 +08:00
import (
"time"
"gorm.io/gorm"
)
2026-01-16 17:03:34 +08:00
// Column 专栏模型
type Column struct {
2026-01-19 13:53:32 +08:00
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
2026-01-16 17:03:34 +08:00
}
2026-06-26 17:00:02 +08:00
// ColumnResponse 专栏 API 响应
type ColumnResponse 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 string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
PostCount int64 `json:"postCount,omitempty"`
LastUpdated string `json:"lastUpdated,omitempty"`
}
2026-01-16 17:03:34 +08:00
// ColumnPost 专栏文章关联模型
type ColumnPost struct {
2026-01-19 13:53:32 +08:00
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
2026-01-16 17:03:34 +08:00
}