goframe 博客文章模型文檔
模型結構 (BlogArticleInfoRes)
BlogArticleInfoRes
結構體代表系統中的一篇博客文章,包含完整的元數據和內容管理功能。
type BlogArticleInfoRes struct {Id uint `orm:"id,primary" json:"id"` // 唯一標識符Title string `orm:"title" json:"title"` // 文章標題Content string `orm:"content" json:"content"` // 文章內容Summary string `orm:"summary" json:"summary"` // 文章摘要CategoryId int `orm:"category_id" json:"categoryId"` // 分類IDTags string `orm:"tags" json:"tags"` // 文章標簽Cover string `orm:"cover" json:"cover"` // 封面圖片URLViewCount int `orm:"view_count" json:"viewCount"` // 瀏覽次數Status int `orm:"status" json:"status"` // 文章狀態CreatedAt *gtime.Time `orm:"created_at" json:"createdAt"` // 創建時間UpdatedAt *gtime.Time `orm:"updated_at" json:"updatedAt"` // 更新時間IsTop int `orm:"is_top" json:"isTop"` // 是否置頂Author string `orm:"author" json:"author"` // 文章作者Category *LinkedCategory `orm:"with:id=category_id" json:"category"` // 關聯分類
}
字段說明
字段名 | 類型 | JSON標簽 | ORM標簽 | 描述 |
---|---|---|---|---|
Id | uint | json:“id” | orm:“id,primary” | 文章唯一標識符 |
Title | string | json:“title” | orm:“title” | 文章標題 |
Content | string | json:“content” | orm:“content” | 文章主要內容 |
Summary | string | json:“summary” | orm:“summary” | 文章簡短摘要 |
CategoryId | int | json:“categoryId” | orm:“category_id” | 文章分類ID |
Tags | string | json:“tags” | orm:“tags” | 文章標簽(逗號分隔) |
Cover | string | json:“cover” | orm:“cover” | 文章封面圖片URL |
ViewCount | int | json:“viewCount” | orm:“view_count” | 文章瀏覽次數 |
Status | int | json:“status” | orm:“status” | 文章狀態 |
CreatedAt | *gtime.Time | json:“createdAt” | orm:“created_at” | 文章創建時間戳 |
UpdatedAt | *gtime.Time | json:“updatedAt” | orm:“updated_at” | 最后更新時間戳 |
IsTop | int | json:“isTop” | orm:“is_top” | 是否置頂文章 |
Author | string | json:“author” | orm:“author” | 文章作者名稱 |
關聯關系說明
該模型通過 Category
字段與分類模型建立一對一關系:
Category *LinkedCategory `orm:"with:id=category_id" json:"category"`
ORM關聯標簽解析
with:
指定關聯條件id=category_id
表示當前模型的category_id
字段與分類模型的id
字段關聯- 查詢時自動進行數據庫表連接
查詢示例
func (s *sBlogArticle) GetArticleWithCategory(ctx context.Context, articleId uint) (*model.BlogArticleInfoRes, error) {var articleInfo *model.BlogArticleInfoReserr := dao.BlogArticle.Ctx(ctx).WithAll().Where("id", articleId).Scan(&articleInfo)if err != nil {return nil, err}// articleInfo.Category 將自動包含關聯的分類信息return articleInfo, nil
}
狀態值說明
0
: 草稿1
: 已發布2
: 審核中3
: 已拒絕
置頂值說明
0
: 普通文章1
: 置頂文章
該模型結構為博客文章管理提供了強大的基礎,支持分類、標簽和元數據跟蹤等功能。
分類模型結構 (CmsCategoryInfoRes)
分類模型用于管理博客文章的分類信息,支持多級分類和SEO優化。
type CmsCategoryInfoRes struct {gmeta.Meta `orm:"table:cms_category"`Id uint `orm:"id,primary" json:"id"` // IDName string `orm:"name" json:"name"` // 名稱Type string `orm:"type" json:"type"` // 類型ParentId uint `orm:"parent_id" json:"parentId"` // 父IDSort int `orm:"sort" json:"sort"` // 排序Status string `orm:"status" json:"status"` // 狀態Alias string `orm:"alias" json:"alias"` // 別名CreatedAt *gtime.Time `orm:"created_at" json:"createdAt"` // 創建時間UpdatedAt *gtime.Time `orm:"updated_at" json:"updatedAt"` // 更新時間
}
分類字段說明
字段名 | 類型 | 描述 |
---|---|---|
Id | uint | 分類唯一標識符 |
Name | string | 分類名稱 |
Type | string | 分類類型 |
ParentId | uint | 父分類ID,用于構建分類層級 |
Sort | int | 排序權重 |
Status | string | 分類狀態 |
Alias | string | 分類別名,用于URL優化 |
CreatedAt | *gtime.Time | 創建時間 |
UpdatedAt | *gtime.Time | 更新時間 |
分類特性
-
多級分類
- 通過
ParentId
支持無限級分類 - 可以構建復雜的分類層級結構
- 通過
-
URL優化
- 支持別名設置,優化URL結構
- 更友好的SEO支持
-
狀態管理
- 可設置分類狀態
- 支持分類的啟用/禁用管理
查詢示例
// 獲取分類及其子分類
func (s *sCmsCategory) GetCategoryWithChildren(ctx context.Context, categoryId uint) (*model.CmsCategoryInfoRes, error) {var category *model.CmsCategoryInfoReserr := dao.CmsCategory.Ctx(ctx).Where("id", categoryId).Scan(&category)if err != nil {return nil, err}// 獲取子分類children, err := dao.CmsCategory.Ctx(ctx).Where("parent_id", categoryId).Order("sort asc").All()if err != nil {return nil, err}// 處理子分類...return category, nil
}// 獲取分類樹
func (s *sCmsCategory) GetCategoryTree(ctx context.Context) ([]*model.CmsCategoryInfoRes, error) {// 獲取所有分類categories, err := dao.CmsCategory.Ctx(ctx).Order("sort asc").All()if err != nil {return nil, err}// 構建分類樹return buildCategoryTree(categories), nil
}
與文章模型的關聯
文章模型通過 CategoryId
關聯到分類模型:
Category *CmsCategoryInfoRes `orm:"with:id=category_id" json:"category"`
這種關聯實現:
- 文章分類的快速查詢
- 分類文章的統計
- 分類導航的構建
- 文章的多級分類展示
狀態說明
0
: 禁用1
: 啟用2
: 待審核
該分類模型為博客系統提供了靈活的文章分類管理功能,支持多級分類結構和基本的分類屬性管理。