Preloading categories and tags

This commit is contained in:
LittleSheep 2024-07-07 11:34:37 +08:00
parent e836a97435
commit 15d8449d2e
3 changed files with 10 additions and 2 deletions

View File

@ -3,7 +3,7 @@ package models
type Tag struct {
BaseModel
Alias string `json:"alias" gorm:"uniqueIndex" validate:"lowercase,alphanum,min=4,max=24"`
Alias string `json:"alias" gorm:"uniqueIndex" validate:"lowercase,alphanum"`
Name string `json:"name"`
Description string `json:"description"`
Posts []Post `json:"posts" gorm:"many2many:post_tags"`
@ -13,7 +13,7 @@ type Tag struct {
type Category struct {
BaseModel
Alias string `json:"alias" gorm:"uniqueIndex" validate:"lowercase,alphanum,min=4,max=24"`
Alias string `json:"alias" gorm:"uniqueIndex" validate:"lowercase,alphanum"`
Name string `json:"name"`
Description string `json:"description"`
Posts []Post `json:"posts" gorm:"many2many:post_categories"`

View File

@ -54,6 +54,8 @@ func GetArticleWithAlias(tx *gorm.DB, alias string, ignoreLimitation ...bool) (m
var item models.Article
if err := tx.
Where("alias = ?", alias).
Preload("Tags").
Preload("Categories").
Preload("Realm").
Preload("Author").
First(&item).Error; err != nil {
@ -71,6 +73,8 @@ func GetArticle(tx *gorm.DB, id uint, ignoreLimitation ...bool) (models.Article,
var item models.Article
if err := tx.
Where("id = ?", id).
Preload("Tags").
Preload("Categories").
Preload("Realm").
Preload("Author").
First(&item).Error; err != nil {

View File

@ -62,6 +62,8 @@ func GetPostWithAlias(tx *gorm.DB, alias string, ignoreLimitation ...bool) (mode
var item models.Post
if err := tx.
Where("alias = ?", alias).
Preload("Tags").
Preload("Categories").
Preload("Realm").
Preload("Author").
Preload("ReplyTo").
@ -83,6 +85,8 @@ func GetPost(tx *gorm.DB, id uint, ignoreLimitation ...bool) (models.Post, error
var item models.Post
if err := tx.
Where("id = ?", id).
Preload("Tags").
Preload("Categories").
Preload("Realm").
Preload("Author").
Preload("ReplyTo").