From 2c138d06eea9d1fdc72612c77f91c952c5a901b4 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 1 Dec 2024 22:51:56 +0800 Subject: [PATCH] :sparkles: Allow filter post via tags --- pkg/internal/http/api/posts_api.go | 4 ++++ pkg/internal/services/posts.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/pkg/internal/http/api/posts_api.go b/pkg/internal/http/api/posts_api.go index 2f12192..0cf10bc 100644 --- a/pkg/internal/http/api/posts_api.go +++ b/pkg/internal/http/api/posts_api.go @@ -47,6 +47,10 @@ func universalPostFilter(c *fiber.Ctx, tx *gorm.DB) (*gorm.DB, error) { tx = services.FilterPostWithTag(tx, c.Query("tag")) } + if len(c.Query("type")) > 0 { + tx = services.FilterPostWithType(tx, c.Query("type")) + } + return tx, nil } diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 79a1ca5..826baa5 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -68,6 +68,10 @@ func FilterPostWithTag(tx *gorm.DB, alias string) *gorm.DB { Where("tags.alias = ?", alias) } +func FilterPostWithType(tx *gorm.DB, t string) *gorm.DB { + return tx.Where("type = ?", t) +} + func FilterPostWithRealm(tx *gorm.DB, id uint) *gorm.DB { if id > 0 { return tx.Where("realm_id = ?", id)