From 8b3a7d6324800351bd132da91cd5f46a202a233a Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 6 Aug 2024 15:29:38 +0800 Subject: [PATCH] :bug: Fix manifest won't filter by author --- pkg/internal/server/api/sticker_packs_api.go | 16 ++++++++++++++-- pkg/internal/services/sticker_packs.go | 5 +++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/pkg/internal/server/api/sticker_packs_api.go b/pkg/internal/server/api/sticker_packs_api.go index 90138e6..5b27a82 100644 --- a/pkg/internal/server/api/sticker_packs_api.go +++ b/pkg/internal/server/api/sticker_packs_api.go @@ -17,12 +17,24 @@ func listStickerManifest(c *fiber.Ctx) error { take = 100 } + tx := database.C + + if len(c.Query("author")) > 0 { + var author models.Account + if err := database.C.Where("name = ?", c.Query("author")).First(&author).Error; err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } else { + tx = tx.Where("account_id = ?", author.ID) + } + } + var count int64 - if err := database.C.Model(&models.StickerPack{}).Count(&count).Error; err != nil { + countTx := tx + if err := countTx.Model(&models.StickerPack{}).Count(&count).Error; err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } - stickers, err := services.ListStickerPackWithStickers(take, offset) + stickers, err := services.ListStickerPackWithStickers(tx, take, offset) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } diff --git a/pkg/internal/services/sticker_packs.go b/pkg/internal/services/sticker_packs.go index 474eeb0..a8f3cad 100644 --- a/pkg/internal/services/sticker_packs.go +++ b/pkg/internal/services/sticker_packs.go @@ -3,6 +3,7 @@ package services import ( "git.solsynth.dev/hydrogen/paperclip/pkg/internal/database" "git.solsynth.dev/hydrogen/paperclip/pkg/internal/models" + "gorm.io/gorm" ) func GetStickerPackWithUser(id, userId uint) (models.StickerPack, error) { @@ -13,9 +14,9 @@ func GetStickerPackWithUser(id, userId uint) (models.StickerPack, error) { return pack, nil } -func ListStickerPackWithStickers(take, offset int) ([]models.StickerPack, error) { +func ListStickerPackWithStickers(tx *gorm.DB, take, offset int) ([]models.StickerPack, error) { var packs []models.StickerPack - if err := database.C.Limit(take).Offset(offset).Preload("Stickers").Preload("Stickers.Attachment").Find(&packs).Error; err != nil { + if err := tx.Limit(take).Offset(offset).Preload("Stickers").Preload("Stickers.Attachment").Find(&packs).Error; err != nil { return packs, err } return packs, nil