From 3c7ae284ac95344ea59767a9f7516968d2c5a994 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 22 Dec 2024 15:49:02 +0800 Subject: [PATCH] :bug: Fix category filter --- pkg/internal/services/posts.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 7e393b0..1cef8ea 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -129,15 +129,12 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB { } func FilterPostWithCategory(tx *gorm.DB, alias string) *gorm.DB { - subQuery := tx.Table("posts"). - Select("DISTINCT posts.id"). - Joins("JOIN post_categories ON posts.id = post_categories.post_id"). + aliases := strings.Split(alias, ",") + return tx.Joins("JOIN post_categories ON posts.id = post_categories.post_id"). Joins("JOIN categories ON categories.id = post_categories.category_id"). - Where("categories.alias IN ?", strings.Split(alias, ",")) - - return tx.Table("(?) as cateogry_subquery", subQuery). - Joins("JOIN posts ON posts.id = category_subquery.id"). - Order("posts.published_at DESC") + Where("categories.alias IN ?", aliases). + Group("posts.id"). + Having("COUNT(DISTINCT categories.id) = ?", len(aliases)) } func FilterPostWithTag(tx *gorm.DB, alias string) *gorm.DB {