110 lines
4.5 KiB
Go
110 lines
4.5 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// 用户模型
|
|
type User struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
Username string `gorm:"uniqueIndex;size:50;not null" json:"username"`
|
|
Password string `gorm:"size:255;not null" json:"-"`
|
|
Nickname string `gorm:"size:100" json:"nickname"`
|
|
Email string `gorm:"size:100" json:"email"`
|
|
Avatar string `gorm:"size:255" json:"avatar"`
|
|
Role string `gorm:"size:20;default:'user'" json:"role"` // admin, user
|
|
Status int `gorm:"default:1" json:"status"` // 1:启用 0:禁用
|
|
}
|
|
|
|
// 文章模型
|
|
type Post struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
Title string `gorm:"size:200;not null" json:"title"`
|
|
Slug string `gorm:"uniqueIndex;size:200" json:"slug"`
|
|
Content string `gorm:"type:text" json:"content"`
|
|
Summary string `gorm:"size:500" json:"summary"`
|
|
Cover string `gorm:"size:255" json:"cover"`
|
|
AuthorID uint `json:"author_id"`
|
|
Author User `gorm:"foreignKey:AuthorID" json:"author"`
|
|
CategoryID uint `json:"category_id"`
|
|
Category Category `gorm:"foreignKey:CategoryID" json:"category"`
|
|
Tags []Tag `gorm:"many2many:post_tags;" json:"tags"`
|
|
Views int `gorm:"default:0" json:"views"`
|
|
Status string `gorm:"size:20;default:'draft'" json:"status"` // published, draft, private
|
|
IsTop bool `gorm:"default:false" json:"is_top"`
|
|
PublishedAt *time.Time `json:"published_at"`
|
|
Comments []Comment `json:"comments,omitempty"`
|
|
}
|
|
|
|
// 分类模型
|
|
type Category struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
Name string `gorm:"size:100;not null" json:"name"`
|
|
Slug string `gorm:"uniqueIndex;size:100" json:"slug"`
|
|
Description string `gorm:"size:255" json:"description"`
|
|
ParentID *uint `json:"parent_id"`
|
|
PostCount int `gorm:"default:0" json:"post_count"`
|
|
}
|
|
|
|
// 标签模型
|
|
type Tag struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
Name string `gorm:"size:100;not null" json:"name"`
|
|
Slug string `gorm:"uniqueIndex;size:100" json:"slug"`
|
|
PostCount int `gorm:"default:0" json:"post_count"`
|
|
}
|
|
|
|
// 评论模型
|
|
type Comment struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
PostID uint `json:"post_id"`
|
|
Post Post `gorm:"foreignKey:PostID" json:"-"`
|
|
ParentID *uint `json:"parent_id"`
|
|
Author string `gorm:"size:100" json:"author"`
|
|
Email string `gorm:"size:100" json:"email"`
|
|
Website string `gorm:"size:255" json:"website"`
|
|
Content string `gorm:"type:text;not null" json:"content"`
|
|
IP string `gorm:"size:50" json:"-"`
|
|
Status string `gorm:"size:20;default:'pending'" json:"status"` // approved, pending, spam
|
|
UserID *uint `json:"user_id"`
|
|
User *User `gorm:"foreignKey:UserID" json:"user,omitempty"`
|
|
Children []Comment `gorm:"foreignKey:ParentID" json:"children,omitempty"`
|
|
}
|
|
|
|
// 设置模型
|
|
type Option struct {
|
|
ID uint `gorm:"primarykey"`
|
|
Key string `gorm:"uniqueIndex;size:100"`
|
|
Value string `gorm:"type:text"`
|
|
}
|
|
|
|
// 页面模型(独立页面)
|
|
type Page struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
Title string `gorm:"size:200;not null" json:"title"`
|
|
Slug string `gorm:"uniqueIndex;size:200" json:"slug"`
|
|
Content string `gorm:"type:text" json:"content"`
|
|
AuthorID uint `json:"author_id"`
|
|
Author User `gorm:"foreignKey:AuthorID" json:"author"`
|
|
Status string `gorm:"size:20;default:'draft'" json:"status"`
|
|
Order int `gorm:"default:0" json:"order"`
|
|
}
|