Files
nl-blogs/server/models/tag.go

73 lines
1.6 KiB
Go
Raw 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
// Tag 标签模型
type Tag struct {
2026-01-19 13:53:32 +08:00
ID uint `json:"id" gorm:"primaryKey;column:id"`
Name string `json:"name" gorm:"column:name"`
Slug string `json:"slug" gorm:"column:slug;uniqueIndex"`
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 (Tag) TableName() string {
return "tags"
}
// BeforeCreate 创建前钩子
func (t *Tag) BeforeCreate(tx *gorm.DB) error {
now := time.Now().Unix()
if t.CreatedAt == 0 {
t.CreatedAt = now
}
if t.UpdatedAt == 0 {
t.UpdatedAt = now
}
if t.DeletedAt == 0 {
t.DeletedAt = 0
}
return nil
}
// BeforeUpdate 更新前钩子
func (t *Tag) BeforeUpdate(tx *gorm.DB) error {
t.UpdatedAt = time.Now().Unix()
return nil
2026-01-16 17:03:34 +08:00
}
2026-06-26 17:00:02 +08:00
// TagResponse 标签 API 响应
type TagResponse struct {
ID uint `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
}
2026-01-16 17:03:34 +08:00
// PostTag 文章标签关联模型
type PostTag struct {
2026-01-19 13:53:32 +08:00
PostID uint `json:"postId" gorm:"primaryKey;column:post_id"`
TagID uint `json:"tagId" gorm:"primaryKey;column:tag_id"`
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"`
}
// TableName 指定表名
func (PostTag) TableName() string {
return "post_tags"
}
// BeforeCreate 创建前钩子
func (pt *PostTag) BeforeCreate(tx *gorm.DB) error {
if pt.CreatedAt == 0 {
pt.CreatedAt = time.Now().Unix()
}
return nil
2026-01-16 17:03:34 +08:00
}