From b778822247dffdee69f74ecd5375da46dbfa638b Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 7 Jul 2024 13:57:31 +0800 Subject: [PATCH] :bug: Fix search with tag & category --- pkg/internal/services/articles.go | 14 ++++++++------ pkg/internal/services/posts.go | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/pkg/internal/services/articles.go b/pkg/internal/services/articles.go index f3bfb41..2d4abba 100644 --- a/pkg/internal/services/articles.go +++ b/pkg/internal/services/articles.go @@ -15,15 +15,17 @@ import ( ) func FilterArticleWithCategory(tx *gorm.DB, alias string) *gorm.DB { - return tx.Joins("JOIN article_categories ON articles.id = article_categories.article_id"). - Joins("JOIN article_categories ON article_categories.id = article_categories.category_id"). - Where("article_categories.alias = ?", alias) + prefix := viper.GetString("database.prefix") + return tx.Joins(fmt.Sprintf("JOIN %sarticle_categories ON %sarticles.id = %sarticle_categories.article_id", prefix, prefix, prefix)). + Joins(fmt.Sprintf("JOIN %sarticle_categories ON %sarticle_categories.id = %sarticle_categories.category_id", prefix, prefix, prefix)). + Where(fmt.Sprintf("%sarticle_categories.alias = ?", prefix), alias) } func FilterArticleWithTag(tx *gorm.DB, alias string) *gorm.DB { - return tx.Joins("JOIN article_tags ON articles.id = article_tags.article_id"). - Joins("JOIN article_tags ON article_tags.id = article_tags.category_id"). - Where("article_tags.alias = ?", alias) + prefix := viper.GetString("database.prefix") + return tx.Joins(fmt.Sprintf("JOIN %sarticle_tags ON %sarticles.id = %sarticle_tags.article_id", prefix, prefix, prefix)). + Joins(fmt.Sprintf("JOIN %sarticle_tags ON %sarticle_tags.id = %sarticle_tags.category_id", prefix, prefix, prefix)). + Where(fmt.Sprintf("%sarticle_tags.alias = ?", prefix), alias) } func FilterArticleWithRealm(tx *gorm.DB, id uint) *gorm.DB { diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 7aa7569..cab3c65 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -15,15 +15,17 @@ import ( ) func FilterPostWithCategory(tx *gorm.DB, alias string) *gorm.DB { - return tx.Joins("JOIN post_categories ON posts.id = post_categories.post_id"). - Joins("JOIN post_categories ON post_categories.id = post_categories.category_id"). - Where("post_categories.alias = ?", alias) + prefix := viper.GetString("database.prefix") + return tx.Joins(fmt.Sprintf("JOIN %spost_categories ON %sposts.id = %spost_categories.post_id", prefix, prefix, prefix)). + Joins(fmt.Sprintf("JOIN %spost_categories ON %spost_categories.id = %spost_categories.category_id", prefix, prefix, prefix)). + Where(fmt.Sprintf("%spost_categories.alias = ?", prefix), alias) } func FilterPostWithTag(tx *gorm.DB, alias string) *gorm.DB { - return tx.Joins("JOIN post_tags ON posts.id = post_tags.post_id"). - Joins("JOIN post_tags ON post_tags.id = post_tags.category_id"). - Where("post_tags.alias = ?", alias) + prefix := viper.GetString("database.prefix") + return tx.Joins(fmt.Sprintf("JOIN %spost_tags ON %sposts.id = %spost_tags.post_id", prefix, prefix, prefix)). + Joins(fmt.Sprintf("JOIN %spost_tags ON %spost_tags.id = %spost_tags.category_id", prefix, prefix, prefix)). + Where(fmt.Sprintf("%spost_tags.alias = ?", prefix), alias) } func FilterPostWithRealm(tx *gorm.DB, id uint) *gorm.DB {