From fdadebbeab6d529e259a6adc56afb814b1552418 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 25 Sep 2024 22:55:15 +0800 Subject: [PATCH] :sparkles: Able to get sticker pack by id --- pkg/internal/server/api/index.go | 1 + pkg/internal/server/api/sticker_packs_api.go | 10 ++++++++++ pkg/internal/services/sticker_packs.go | 8 ++++++++ 3 files changed, 19 insertions(+) diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index dd7bb66..5ffd134 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -26,6 +26,7 @@ func MapAPIs(app *fiber.App, baseURL string) { api.Get("/stickers/lookup", lookupStickerBatch) api.Get("/stickers/lookup/:alias", lookupSticker) api.Get("/stickers/packs", listStickerPacks) + api.Get("/stickers/packs/:packId", getStickerPack) api.Post("/stickers/packs", createStickerPack) api.Put("/stickers/packs/:packId", updateStickerPack) api.Delete("/stickers/packs/:packId", deleteStickerPack) diff --git a/pkg/internal/server/api/sticker_packs_api.go b/pkg/internal/server/api/sticker_packs_api.go index 4c21494..951cb7b 100644 --- a/pkg/internal/server/api/sticker_packs_api.go +++ b/pkg/internal/server/api/sticker_packs_api.go @@ -44,6 +44,16 @@ func listStickerPacks(c *fiber.Ctx) error { }) } +func getStickerPack(c *fiber.Ctx) error { + id, _ := c.ParamsInt("packId", 0) + pack, err := services.GetStickerPack(uint(id)) + if err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + + return c.JSON(pack) +} + func createStickerPack(c *fiber.Ctx) error { if err := gap.H.EnsureAuthenticated(c); err != nil { return err diff --git a/pkg/internal/services/sticker_packs.go b/pkg/internal/services/sticker_packs.go index a8f3cad..8414ee1 100644 --- a/pkg/internal/services/sticker_packs.go +++ b/pkg/internal/services/sticker_packs.go @@ -6,6 +6,14 @@ import ( "gorm.io/gorm" ) +func GetStickerPack(id uint) (models.StickerPack, error) { + var pack models.StickerPack + if err := database.C.Where("id = ?", id).First(&pack).Error; err != nil { + return pack, err + } + return pack, nil +} + func GetStickerPackWithUser(id, userId uint) (models.StickerPack, error) { var pack models.StickerPack if err := database.C.Where("id = ? AND account_id = ?", id, userId).First(&pack).Error; err != nil {