refactor: 统一 BaseModel 嵌入,修复 primaryKey 大小写不一致
Some checks failed
CI / Lint + Build (push) Has been cancelled

变更模型:
- Post: 嵌入 BaseModel,移除手动 ID/CreatedAt/UpdatedAt/DeletedAt
- Comment: 嵌入 BaseModel,保留 is_deleted 自定义软删除
- Announcement: 嵌入 BaseModel
- Category: 嵌入 BaseModel
- Folder + FolderItem: 嵌入 BaseModel
- CommunityFund: primaryKey → primarykey (统一小写)
This commit is contained in:
2026-06-22 03:09:19 +08:00
parent 2449a27eb5
commit c3c7390498
6 changed files with 52 additions and 70 deletions

View File

@ -4,7 +4,8 @@ import "time"
// Announcement 公告
type Announcement struct {
ID uint `gorm:"primarykey" json:"id"`
BaseModel
Title string `gorm:"type:varchar(200);not null" json:"title"`
Content string `gorm:"type:text;not null" json:"content"`
Type string `gorm:"type:varchar(20);default:info" json:"type"` // info / warning / success / error
@ -12,6 +13,4 @@ type Announcement struct {
StartAt *time.Time `json:"start_at,omitempty"`
EndAt *time.Time `json:"end_at,omitempty"`
Sort int `gorm:"default:0" json:"sort"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

View File

@ -1,17 +1,14 @@
package model
import "time"
// Category 文章分类(两级树形结构)
type Category struct {
ID uint `gorm:"primarykey" json:"id"`
BaseModel
Name string `gorm:"type:varchar(50);not null" json:"name"`
Slug string `gorm:"type:varchar(50);not null;uniqueIndex" json:"slug"`
Description string `gorm:"type:varchar(200);default:''" json:"description,omitempty"`
ParentID *uint `gorm:"index" json:"parent_id,omitempty"`
Sort int `gorm:"default:0" json:"sort"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// 非 DB 字段(树形填充)
Children []Category `gorm:"-" json:"children,omitempty"`

View File

@ -4,7 +4,8 @@ import "time"
// Comment 评论模型
type Comment struct {
ID uint `gorm:"primarykey" json:"id"`
BaseModel
PostID uint `gorm:"index;not null" json:"post_id"`
RootID uint `gorm:"index;not null" json:"root_id"` // 根评论ID顶级评论指向自身
ParentID *uint `gorm:"index" json:"parent_id"` // 父评论IDNULL=顶级评论
@ -14,8 +15,6 @@ type Comment struct {
LikesCount int `gorm:"default:0" json:"likes_count"`
DislikesCount int `gorm:"default:0" json:"dislikes_count"` // 仅后台可见
UserID uint `gorm:"index;not null" json:"user_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// 非数据库字段(联表查询填充)
AuthorName string `gorm:"-:migration;<-:false;column:author_name" json:"author_name,omitempty"`
@ -26,7 +25,6 @@ type Comment struct {
// RepliesCount 回复数非DB字段查询填充
RepliesCount int `gorm:"-:migration;<-:false" json:"replies_count,omitempty"`
// Mentions 有效@提及映射 original_username -> {uid, 当前显示名}非DB字段批量查询填充
// key=创建时的原始@用户名value={uid,当前显示名},用户改名后链接仍有效且显示新名
Mentions map[string]MentionInfo `gorm:"-" json:"mentions,omitempty"`
}

View File

@ -1,10 +1,7 @@
package model
import "time"
// CommunityFund 公户余额单行表id=1
type CommunityFund struct {
ID uint `gorm:"primaryKey" json:"id"`
ID uint `gorm:"primarykey" json:"id"`
Balance int `gorm:"not null;default:0" json:"balance"` // 余额×10允许负数
UpdatedAt time.Time `json:"updated_at"`
}

View File

@ -1,17 +1,16 @@
package model
import "time"
// time imported by common.go
// Folder 收藏夹
type Folder struct {
ID uint `gorm:"primarykey" json:"id"`
BaseModel
UserID uint `gorm:"index;not null" json:"user_id"`
Name string `gorm:"type:varchar(50);not null" json:"name"`
Description string `gorm:"type:varchar(200);default:''" json:"description"`
IsPublic bool `gorm:"default:false" json:"is_public"`
IsDefault bool `gorm:"default:false" json:"is_default"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// 联表查询填充(非数据库字段)
ItemCount int64 `gorm:"-:migration;<-:false;column:item_count" json:"item_count,omitempty"`
@ -19,11 +18,11 @@ type Folder struct {
// FolderItem 收藏记录
type FolderItem struct {
ID uint `gorm:"primarykey" json:"id"`
BaseModel
FolderID uint `gorm:"index;not null" json:"folder_id"`
PostID uint `gorm:"index;not null" json:"post_id"`
UserID uint `gorm:"index;not null" json:"user_id"`
CreatedAt time.Time `json:"created_at"`
// 联表查询填充
PostTitle string `gorm:"-:migration;<-:false;column:post_title" json:"post_title,omitempty"`

View File

@ -1,14 +1,11 @@
package model
import (
"time"
"gorm.io/gorm"
)
import "time"
// Post 帖子/文章模型
type Post struct {
ID uint `gorm:"primarykey" json:"id"`
BaseModel
Title string `gorm:"type:varchar(200);not null" json:"title"`
Body string `gorm:"type:text" json:"body"` // Markdown content
Excerpt string `gorm:"type:varchar(500)" json:"excerpt"` // plain text summary for list
@ -38,13 +35,8 @@ type Post struct {
DislikesCount int `gorm:"default:0" json:"dislikes_count"` // 踩数(冗余计数器,仅后台可见)
FavoritesCount int `gorm:"default:0" json:"favorites_count"` // 收藏数(冗余计数器)
ViewsCount int `gorm:"default:0" json:"views_count"` // 阅读数(冗余计数器,已登录去重)
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// 非数据库字段(联表查询填充)
// gorm:"-:migration" 指定不创建/迁移该列,"<-:false" 禁止写入,"column:author_name" 允许
// 从 JOIN 查询的 users.username AS author_name 别名中读取
AuthorName string `gorm:"-:migration;<-:false;column:author_name" json:"author_name,omitempty"`
}