From ed8afe83249b5ac52362b955641317e1d516aea8 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 20 Feb 2025 21:36:47 +0800 Subject: [PATCH] :sparkles: Filtering post with realm in querystring --- pkg/internal/http/api/posts_api.go | 4 ++++ pkg/internal/services/posts.go | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/pkg/internal/http/api/posts_api.go b/pkg/internal/http/api/posts_api.go index 308e744..37f2e5e 100644 --- a/pkg/internal/http/api/posts_api.go +++ b/pkg/internal/http/api/posts_api.go @@ -55,6 +55,10 @@ func UniversalPostFilter(c *fiber.Ctx, tx *gorm.DB) (*gorm.DB, error) { tx = services.FilterPostWithType(tx, c.Query("type")) } + if len(c.Query("realm")) > 0 { + tx = services.FilterPostWithRealm(tx, c.Query("realm")) + } + return tx, nil } diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 872ce4c..bdba083 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -159,6 +159,19 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB { return tx } +func FilterPostWithRealm(tx *gorm.DB, probe string) *gorm.DB { + if numericId, err := strconv.Atoi(probe); err == nil { + return tx.Where("realm_id = ?", uint(numericId)) + } + + realm, err := authkit.GetRealmByAlias(gap.Nx, probe) + if err != nil { + return tx + } + + return tx.Where("realm_id = ?", realm.ID) +} + func FilterPostWithCategory(tx *gorm.DB, alias string) *gorm.DB { aliases := strings.Split(alias, ",") return tx.Joins("JOIN post_categories ON posts.id = post_categories.post_id").