🐛 Fix manifest won't filter by author

This commit is contained in:
LittleSheep 2024-08-06 15:29:38 +08:00
parent dc57261b2e
commit 8b3a7d6324
2 changed files with 17 additions and 4 deletions

View File

@ -17,12 +17,24 @@ func listStickerManifest(c *fiber.Ctx) error {
take = 100 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 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()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} }
stickers, err := services.ListStickerPackWithStickers(take, offset) stickers, err := services.ListStickerPackWithStickers(tx, take, offset)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} }

View File

@ -3,6 +3,7 @@ package services
import ( import (
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database" "git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models" "git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
"gorm.io/gorm"
) )
func GetStickerPackWithUser(id, userId uint) (models.StickerPack, error) { func GetStickerPackWithUser(id, userId uint) (models.StickerPack, error) {
@ -13,9 +14,9 @@ func GetStickerPackWithUser(id, userId uint) (models.StickerPack, error) {
return pack, nil 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 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, err
} }
return packs, nil return packs, nil